Class: Foobara::RemoteGenerator::Services::TypeGenerator

Inherits:
TypescriptFromManifestBaseGenerator show all
Defined in:
foobara-typescript-remote-command-generator-1.2.0/src/remote_generator/services/type_generator.rb

Direct Known Subclasses

ModelGenerator

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 TypescriptFromManifestBaseGenerator

#==, #attributes_to_ts_type, #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, #ts_type_full_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::Services::TypescriptFromManifestBaseGenerator

Dynamic Method Handling

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

Class Method Details

.lru_cacheObject



8
9
10
# File 'src/remote_generator/services/type_generator.rb', line 8

def lru_cache
  @lru_cache ||= LruCache.new(1000)
end

.new(relevant_manifest) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'src/remote_generator/services/type_generator.rb', line 12

def new(relevant_manifest)
  unless self == TypeGenerator
    generator = lru_cache.cached([self, relevant_manifest]) do
      super
    end

    return generator
  end

  if relevant_manifest.detached_entity?
    EntityGenerator.new(relevant_manifest)
  elsif relevant_manifest.model?
    ModelGenerator.new(relevant_manifest)
  else
    lru_cache.cached([self, relevant_manifest]) do
      super
    end
  end
end

Instance Method Details

#aggregate_attributes_ts_typeObject



139
140
141
142
143
144
145
146
# File 'src/remote_generator/services/type_generator.rb', line 139

def aggregate_attributes_ts_type
  association_depth = AssociationDepth::AGGREGATE
  foobara_type_to_ts_type(attributes_type,
                          association_depth:,
                          dependency_group:,
                          is_output: true,
                          parent: relevant_manifest)
end

#atom_attributes_ts_typeObject



130
131
132
133
134
135
136
137
# File 'src/remote_generator/services/type_generator.rb', line 130

def atom_attributes_ts_type
  association_depth = AssociationDepth::ATOM
  foobara_type_to_ts_type(attributes_type,
                          association_depth:,
                          dependency_group:,
                          is_output: true,
                          parent: relevant_manifest)
end

#attribute_namesObject



148
149
150
# File 'src/remote_generator/services/type_generator.rb', line 148

def attribute_names
  attributes_type.attribute_names
end

#attributes_type_ts_typeObject



121
122
123
124
125
126
127
128
# File 'src/remote_generator/services/type_generator.rb', line 121

def attributes_type_ts_type
  association_depth = AssociationDepth::AMBIGUOUS
  foobara_type_to_ts_type(attributes_type,
                          association_depth:,
                          dependency_group:,
                          is_output: true,
                          parent: relevant_manifest)
end

#custom_type_generatorsObject



107
108
109
110
111
# File 'src/remote_generator/services/type_generator.rb', line 107

def custom_type_generators
  @custom_type_generators ||= types_depended_on.reject(&:builtin?).reject(&:model?).map do |type|
    Services::TypeGenerator.new(type)
  end
end

#dependenciesObject



113
114
115
# File 'src/remote_generator/services/type_generator.rb', line 113

def dependencies
  @dependencies ||= custom_type_generators + model_generators
end

#model_generatorsObject



101
102
103
104
105
# File 'src/remote_generator/services/type_generator.rb', line 101

def model_generators
  @model_generators ||= types_depended_on.select(&:model?).reject(&:builtin?).map do |model|
    Services::ModelGenerator.new(model)
  end
end

#scoped_full_path(points = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'src/remote_generator/services/type_generator.rb', line 61

def scoped_full_path(points = nil)
  full_path = type_manifest.scoped_full_path

  if points
    start_at = full_path.size - points - 1
    full_path[start_at..]
  else
    full_path
  end
end

#target_pathObject



35
36
37
# File 'src/remote_generator/services/type_generator.rb', line 35

def target_path
  [*domain.scoped_full_path, "Types", *type_prefix, "#{type_short_name}.ts"]
end

#template_pathObject



43
44
45
# File 'src/remote_generator/services/type_generator.rb', line 43

def template_path
  ["Type", "Type.ts.erb"]
end

#type_gutsObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'src/remote_generator/services/type_generator.rb', line 80

def type_guts
  guts = if declaration_data.key?("one_of")
           declaration_data["one_of"].map do |enum_item|
             value_to_ts_value(enum_item)
           end.join(" | ")
         else
           # :nocov:
           raise "Haven't implemented other types yet"
           # :nocov:
         end

  if declaration_data["allow_nil"]
    # TODO: add a custom type to the fixture manifest that includes allows_nil
    # :nocov:
    guts += " | undefined"
    # :nocov:
  end

  guts
end

#type_name(points = nil) ⇒ Object



72
73
74
75
76
77
78
# File 'src/remote_generator/services/type_generator.rb', line 72

def type_name(points = nil)
  if points
    scoped_full_path(points).join(".")
  else
    scoped_path.join(".")
  end
end

#type_name_downcaseObject



117
118
119
# File 'src/remote_generator/services/type_generator.rb', line 117

def type_name_downcase
  type_short_name[0].downcase + type_short_name[1..]
end

#type_prefixObject



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'src/remote_generator/services/type_generator.rb', line 47

def type_prefix
  path = scoped_prefix

  if path && !path.empty?
    if path.first == "Types"
      path[1..]
    else
      path
    end
  else
    []
  end
end

#type_short_nameObject



39
40
41
# File 'src/remote_generator/services/type_generator.rb', line 39

def type_short_name
  scoped_short_name
end