Class: Foobara::RemoteGenerator::Generators::CommandGenerator

Inherits:
TypescriptFromManifestBaseGenerator show all
Defined in:
foobara-typescript-remote-command-generator-1.3.1/src/generators/command_generator.rb

Constant Summary

Constants included from TruncatedInspect

TruncatedInspect::MAX_LENGTH

Instance Attribute Summary

Attributes inherited from FilesGenerator

#belongs_to_dependency_group, #relevant_manifest

Instance Method Summary collapse

Methods inherited from TypescriptFromManifestBaseGenerator

#==, #attributes_to_ts_type, #auto_dirty_queries?, #collision_winners, #custom_type_to_ts_type_name, #dependency_group, #dependency_roots, #foobara_type_to_ts_type, #hash, #import_destructure, #import_path, #import_path_array, #initialize, manifest_to_generator_classes, #model_to_ts_model_name, #parent, #path_to_root, #templates_dir, #ts_instance_full_name, #ts_instance_full_path, #ts_instance_path, #value_to_ts_value

Methods inherited from FilesGenerator

#==, #absolute_template_path, #applicable?, #dependencies_to_generate, #eql?, #erb_template, #generate, generator_for, #generator_for, generators_for, #generators_for, #hash, #initialize, manifest_to_generator_classes, #method_missing, #path_to_root, #respond_to_missing?, #target_dir, #template_string, #templates_dir

Methods included from TruncatedInspect

#inspect, truncating

Constructor Details

This class inherits a constructor from Foobara::RemoteGenerator::Generators::TypescriptFromManifestBaseGenerator

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Foobara::FilesGenerator

Instance Method Details

#base_class_nameObject



52
53
54
# File 'src/generators/command_generator.rb', line 52

def base_class_name
  base_class_path.split("/").last
end

#base_class_pathObject



48
49
50
# File 'src/generators/command_generator.rb', line 48

def base_class_path
  "base/RemoteCommand"
end

#command_errors_index_generatorObject



44
45
46
# File 'src/generators/command_generator.rb', line 44

def command_errors_index_generator
  CommandErrorsIndexGenerator.new(command_manifest)
end

#dependenciesObject



33
34
35
36
37
38
# File 'src/generators/command_generator.rb', line 33

def dependencies
  [
    RemoteCommandGenerator.new(Manifest::RootManifest.new(root_manifest)),
    *queries_that_are_dirtied_by_this_command.keys
  ]
end

#domain_generatorObject



17
18
19
# File 'src/generators/command_generator.rb', line 17

def domain_generator
  @domain_generator ||= DomainGenerator.new(domain)
end

#domain_nameObject



22
# File 'src/generators/command_generator.rb', line 22

def domain_name = domain_generator.domain_name

#errors_in_this_namespaceObject



25
26
27
28
29
30
31
# File 'src/generators/command_generator.rb', line 25

def errors_in_this_namespace
  @errors_in_this_namespace ||= possible_errors.values.map(&:error).uniq.sort_by(&:error_name).select do |error|
    error.parent&.manifest_path&.map(&:to_s) == manifest_path.map(&:to_s)
  end.map do |error_manifest|
    ErrorGenerator.new(error_manifest)
  end
end

#organization_generatorObject



21
# File 'src/generators/command_generator.rb', line 21

def organization_generator = domain_generator.organization_generator

#organization_nameObject



23
# File 'src/generators/command_generator.rb', line 23

def organization_name = domain_generator.organization_name

#queries_dirtied_with_inputsObject



163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'src/generators/command_generator.rb', line 163

def queries_dirtied_with_inputs
  return @queries_dirtied_with_inputs if defined?(@queries_dirtied_with_inputs)

  dirtied_queries = {}

  queries_that_are_dirtied_by_this_command.each_pair do |query, value|
    if value != true
      dirtied_queries[query] = value
    end
  end

  @queries_dirtied_with_inputs = dirtied_queries
end

#queries_dirtied_without_inputsObject



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'src/generators/command_generator.rb', line 149

def queries_dirtied_without_inputs
  return @queries_dirtied_without_inputs if defined?(@queries_dirtied_without_inputs)

  queries_dirtied = []

  queries_that_are_dirtied_by_this_command.each_pair do |query, value|
    if value == true
      queries_dirtied << query
    end
  end

  @queries_dirtied_without_inputs = queries_dirtied
end

#queries_that_are_dirtied_by_this_commandObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'src/generators/command_generator.rb', line 62

def queries_that_are_dirtied_by_this_command
  return {} if query? || !auto_dirty_queries?

  return @queries_that_are_dirtied_by_this_command if defined?(@queries_that_are_dirtied_by_this_command)

  command_result_type = result_type

  paths_to_data = nil

  if command_result_type
    command_result_type = command_result_type.to_type if command_result_type.is_a?(Manifest::TypeDeclaration)

    if command_result_type.detached_entity?
      paths_to_data = { command_result_type => [:outcome, :result, command_result_type.primary_key_name] }
    else
      result_type_associations = Manifest::Model.associations(command_result_type)

      unless result_type_associations.empty?
        data_path, entity_type = result_type_associations.to_a.first
        paths_to_data = { entity_type => [:outcome, :result, data_path, entity_type.primary_key_name] }
      end
    end
  end

  if paths_to_data.nil?
    if inputs_type
      inputs_associations = Manifest::Model.associations(inputs_type)

      unless inputs_associations.empty?
        data_path, entity_type = inputs_associations.to_a.first
        paths_to_data = { entity_type => [:inputs, *data_path] }
      end
    end
  end

  dirties = {}

  unless paths_to_data.nil?
    all_queries = Manifest::RootManifest.new(root_manifest).queries.map do |query|
      generator_for(query)
    end

    paths_to_data.each_pair do |entity_type, path|
      all_queries.each do |query|
        query_inputs_type = query.inputs_type

        if query_inputs_type
          query_associations = Manifest::Model.associations(query_inputs_type)
          query_associations.each_pair do |query_association_path, query_entity_class|
            query_entity_class = query_entity_class.to_type if query_entity_class.is_a?(Manifest::TypeDeclaration)

            if query_entity_class == entity_type
              dirties[query] = [path, query_association_path]
            end
          end

          next if dirties.key?(query)
        end

        query_result_type = query.result_type
        next unless query_result_type

        if query_result_type.is_a?(Manifest::TypeDeclaration) && query_result_type.reference?
          query_result_type = query_result_type.to_type
        end

        if query_result_type == entity_type
          dirties[query] = true
        else
          entity_classes = Manifest::Model.associations(query_result_type).values.uniq

          entity_classes.each do |query_entity_class|
            query_entity_class = query_entity_class.to_type if query_entity_class.is_a?(Manifest::TypeDeclaration)

            if query_entity_class == entity_type
              dirties[query] = true
              break
            end
          end
        end
      end
    end
  end

  @queries_that_are_dirtied_by_this_command = dirties
end

#result_json_requires_cast?Boolean

Returns:

  • (Boolean)


56
57
58
59
60
# File 'src/generators/command_generator.rb', line 56

def result_json_requires_cast?
  # What types require a cast?
  # :date and :datetime, :model, custom type declaration (check #custom?)
  result_type && type_requires_cast?(result_type)
end

#target_pathObject



9
10
11
# File 'src/generators/command_generator.rb', line 9

def target_path
  [*scoped_full_path, "index.ts"]
end

#template_pathObject



13
14
15
# File 'src/generators/command_generator.rb', line 13

def template_path
  "Command.ts.erb"
end

#will_defineObject



40
41
42
# File 'src/generators/command_generator.rb', line 40

def will_define
  ts_instance_path
end