Class: Foobara::RemoteGenerator::Services::TypescriptFromManifestBaseGenerator

Inherits:
FilesGenerator
  • Object
show all
Defined in:
foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/typescript_from_manifest_base_generator.rb

Constant Summary

Constants included from TruncatedInspect

TruncatedInspect::MAX_LENGTH

Instance Attribute Summary

Attributes inherited from FilesGenerator

#belongs_to_dependency_group, #relevant_manifest

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FilesGenerator

#absolute_template_path, #applicable?, #dependencies, #dependencies_to_generate, #eql?, #erb_template, #generate, generator_for, #generator_for, generators_for, #generators_for, #method_missing, #respond_to_missing?, #target_dir, #target_path, #template_path, #template_string

Methods included from TruncatedInspect

#inspect, truncating

Constructor Details

#initialize(relevant_manifest) ⇒ TypescriptFromManifestBaseGenerator

Returns a new instance of TypescriptFromManifestBaseGenerator.



99
100
101
102
103
104
105
106
107
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 99

def initialize(relevant_manifest)
  unless relevant_manifest.is_a?(Manifest::BaseManifest)
    # :nocov:
    raise ArgumentError, "Expected a Foobara::Manifest, got #{relevant_manifest.class}"
    # :nocov:
  end

  super
end

Dynamic Method Handling

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

Class Method Details

.manifest_to_generator_classes(manifest) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 28

def manifest_to_generator_classes(manifest)
  case manifest
  when Manifest::Command
    generator_classes = case manifest.full_command_name
                        when "Foobara::Auth::RefreshLogin"
                          Services::Auth::RefreshLoginGenerator
                        when "Foobara::Auth::Login"
                          Services::Auth::LoginGenerator
                        when "Foobara::Auth::Logout"
                          Services::Auth::LogoutGenerator
                        when /\bGetCurrentUser$/
                          [
                            Services::Auth::RequiresAuthGenerator,
                            Services::Auth::SetupGenerator
                          ]
                        else
                          if manifest.requires_authentication?
                            Services::Auth::RequiresAuthGenerator
                          else
                            Services::CommandGenerator
                          end
                        end

    [
      *generator_classes,
      Services::CommandInputsGenerator,
      Services::CommandResultGenerator,
      Services::CommandErrorsGenerator,
      Services::CommandErrorsIndexGenerator,
      Services::CommandManifestGenerator
    ]
  when Manifest::Domain
    [
      Services::DomainGenerator,
      Services::DomainConfigGenerator,
      Services::DomainManifestGenerator
    ]
  when Manifest::Organization
    [
      Services::OrganizationGenerator,
      Services::OrganizationConfigGenerator,
      Services::OrganizationManifestGenerator
    ]
  when Manifest::Entity, Manifest::DetachedEntity
    [
      Services::EntityGenerator,
      Services::EntityVariantsGenerator,
      Services::EntityManifestGenerator
    ]
  when Manifest::Model
    [
      Services::ModelGenerator,
      Services::ModelVariantsGenerator,
      Services::ModelManifestGenerator
    ]
  when Manifest::Error
    Services::ErrorGenerator
  when Manifest::ProcessorClass
    Services::ProcessorClassGenerator
  when Manifest::RootManifest
    Services::RootManifestGenerator
  when Manifest::Type
    Services::TypeGenerator
  else
    # :nocov:
    raise "Not sure how build a generator for a #{manifest}"
    # :nocov:
  end
end

Instance Method Details

#==(other) ⇒ Object



351
352
353
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 351

def ==(other)
  self.class == other.class && path == other.path && root_manifest == other.root_manifest
end

#attributes_to_ts_type(attributes, dependency_group:, association_depth: AssociationDepth::AMBIGUOUS, model_and_entity_free: false) ⇒ Object



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 298

def attributes_to_ts_type(
  attributes,
  dependency_group:,
  association_depth: AssociationDepth::AMBIGUOUS,
  model_and_entity_free: false
)
  guts = attributes.attribute_declarations.map do |attribute_name, attribute_declaration|
    "  #{attribute_name}#{"?" unless attributes.required?(attribute_name)}: #{
      foobara_type_to_ts_type(
        attribute_declaration,
        dependency_group:,
        association_depth:,
        initial: false,
        model_and_entity_free:
      )
    }"
  end.join("\n")

  "{\n#{guts}\n}"
end

#custom_type_to_ts_type_name(type) ⇒ Object



344
345
346
347
348
349
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 344

def custom_type_to_ts_type_name(type)
  type = type.to_type if type.is_a?(Manifest::TypeDeclaration)
  generator = TypeGenerator.new(type)

  dependency_group.non_colliding_type(generator)
end

#dependency_groupObject



119
120
121
122
123
124
125
126
127
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 119

def dependency_group
  @dependency_group ||= begin
    generators = dependencies.map do |dependency|
      generator_for(dependency)
    end

    DependencyGroup.new(generators, name: scoped_full_path.join("."))
  end
end

#dependency_rootsObject



129
130
131
132
133
134
135
136
137
138
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 129

def dependency_roots
  unless dependency_group
    # :nocov:
    raise "This generator was created without a " \
          "dependency_group and therefore cannot call #{__method__}"
    # :nocov:
  end

  dependency_group.non_colliding_dependency_roots.sort_by(&:scoped_full_name)
end

#foobara_type_to_ts_type(type_declaration, dependency_group: self.dependency_group, name: nil, association_depth: AssociationDepth::AMBIGUOUS, initial: true, model_and_entity_free: false) ⇒ Object

TODO: relocate this to a more reusable place



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 198

def foobara_type_to_ts_type(
  type_declaration,
  dependency_group: self.dependency_group,
  name: nil,
  association_depth: AssociationDepth::AMBIGUOUS,
  initial: true,
  model_and_entity_free: false
)
  if type_declaration.is_a?(Manifest::Error)
    error_generator = generator_for(type_declaration)
    return dependency_group.non_colliding_type(error_generator)
  end

  type_string = if type_declaration.is_a?(Manifest::Attributes)
                  ts_type = attributes_to_ts_type(
                    type_declaration,
                    association_depth:,
                    dependency_group:,
                    model_and_entity_free:
                  )

                  is_empty = type_declaration.attribute_declarations.empty?

                  if name
                    # TODO: test this code path or delete it
                    # :nocov:
                    return is_empty ? "undefined" : "interface #{name} #{ts_type}"
                    # :nocov:
                  else
                    is_empty ? "undefined" : ts_type
                  end
                elsif type_declaration.is_a?(Manifest::Array)
                  # TODO: which association_depth do we pass here?
                  ts_type = foobara_type_to_ts_type(
                    type_declaration.element_type,
                    association_depth:,
                    dependency_group:,
                    initial: false,
                    model_and_entity_free:
                  )
                  "#{ts_type}[]"
                else
                  type_symbol = type_declaration.type

                  case type_symbol
                  when "string", "boolean"
                    type_symbol
                  when "number", "integer", "float"
                    "number"
                  # TODO: should apply relevant processors to make email a real email type instead of "string"
                  when "symbol", "email"
                    "string"
                  when "duck"
                    "any"
                  when "datetime"
                    "Date"
                  else
                    if type_declaration.model?
                      if model_and_entity_free
                        model_type = type_declaration.to_type

                        translated_type = if type_declaration.detached_entity?
                                            model_type.primary_key_type
                                          else
                                            model_type.attributes_type
                                          end

                        foobara_type_to_ts_type(
                          translated_type,
                          association_depth:,
                          dependency_group:,
                          initial:,
                          model_and_entity_free:
                        )
                      else
                        model_to_ts_model_name(type_declaration, association_depth:, initial:)
                      end
                    elsif type_declaration.custom?
                      custom_type_to_ts_type_name(type_declaration)
                    end
                  end
                end

  if type_string
    if type_declaration.one_of
      type_string = type_declaration.one_of.map(&:inspect).join(" | ")
    end

    if type_declaration.allows_nil?
      type_string = "#{type_string} | null"
    end

    name ? "#{name} = #{type_string}" : type_string
  else
    # :nocov:
    raise "Not sure how to convert #{type_declaration} to a TS type"
    # :nocov:
  end
end

#hashObject



355
356
357
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 355

def hash
  path.hash
end

#import_destructureObject



168
169
170
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 168

def import_destructure
  "{ #{scoped_short_name} }"
end

#import_pathObject



160
161
162
163
164
165
166
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 160

def import_path
  if import_path_array.size == 1
    "./#{import_path_array.first}"
  else
    import_path_array.join("/")
  end
end

#import_path_arrayObject



172
173
174
175
176
177
178
179
180
181
182
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 172

def import_path_array
  path = if target_path.last == "index.ts"
           target_path[0..-2]
         else
           target_path
         end

  path[-1] = path.last.gsub(/\.ts$/, "")

  path
end

#model_to_ts_model_name(model, association_depth: AssociationDepth::AMBIGUOUS, initial: true) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 319

def model_to_ts_model_name(model, association_depth: AssociationDepth::AMBIGUOUS, initial: true)
  model = model.to_type if model.is_a?(Manifest::TypeDeclaration)

  generator_class = case association_depth
                    when AssociationDepth::AMBIGUOUS
                      Services::ModelGenerator
                    when AssociationDepth::ATOM
                      if !initial && model.detached_entity?
                        Services::UnloadedEntityGenerator
                      else
                        Services::AtomModelGenerator
                      end
                    when AssociationDepth::AGGREGATE
                      Services::AggregateModelGenerator
                    else
                      # :nocov:
                      raise "Bad association_depth: #{association_depth}"
                      # :nocov:
                    end

  generator = generator_class.new(model)

  dependency_group.non_colliding_type(generator)
end

#parentObject



113
114
115
116
117
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 113

def parent
  if relevant_manifest.parent
    generator_for(relevant_manifest.parent)
  end
end

#path_to_rootObject



359
360
361
362
363
364
365
366
367
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 359

def path_to_root
  path = super

  if path.empty?
    "./"
  else
    path
  end
end

#templates_dirObject



109
110
111
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 109

def templates_dir
  "#{__dir__}/../../../templates"
end

#ts_instance_full_nameObject



144
145
146
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 144

def ts_instance_full_name
  ts_instance_full_path.join(".")
end

#ts_instance_full_pathObject



148
149
150
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 148

def ts_instance_full_path
  scoped_full_path
end

#ts_instance_pathObject



140
141
142
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 140

def ts_instance_path
  scoped_path
end

#ts_type_full_pathObject



152
153
154
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 152

def ts_type_full_path
  ts_instance_full_path
end

#value_to_ts_value(value) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 184

def value_to_ts_value(value)
  case value
  when ::String, Numeric
    value.inspect
  when ::Symbol
    value.to_s.inspect
  else
    # :nocov:
    raise "Not sure how to represent #{value} in typescript. Maybe implement it."
    # :nocov:
  end
end