Class: Foobara::RemoteGenerator::Services::TypescriptFromManifestBaseGenerator
- Inherits:
-
FilesGenerator
- Object
- FilesGenerator
- Foobara::RemoteGenerator::Services::TypescriptFromManifestBaseGenerator
show all
- Defined in:
- foobara-typescript-remote-command-generator-0.0.18/src/remote_generator/services/typescript_from_manifest_base_generator.rb
Direct Known Subclasses
Auth::AccessTokensGenerator, Auth::LoginCommandGenerator, Auth::LogoutCommandGenerator, Auth::RequiresAuthCommandGenerator, CommandGenerator, CommandInputsGenerator, CommandResultGenerator, DomainGenerator, ErrorGenerator, ManifestGenerator, OrganizationGenerator, ProcessorClassGenerator, TypeGenerator
Constant Summary
TruncatedInspect::MAX_LENGTH
Instance Attribute Summary
#belongs_to_dependency_group, #relevant_manifest
Class Method Summary
collapse
Instance Method Summary
collapse
-
#==(other) ⇒ Object
-
#attributes_to_ts_type(attributes, dependency_group:, association_depth: AssociationDepth::AMBIGUOUS, model_and_entity_free: false) ⇒ Object
-
#custom_type_to_ts_type_name(type) ⇒ Object
-
#dependency_group ⇒ Object
-
#dependency_roots ⇒ Object
-
#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.
-
#hash ⇒ Object
-
#import_destructure ⇒ Object
-
#import_path ⇒ Object
-
#import_path_array ⇒ Object
-
#initialize(relevant_manifest) ⇒ TypescriptFromManifestBaseGenerator
constructor
A new instance of TypescriptFromManifestBaseGenerator.
-
#model_to_ts_model_name(model, association_depth: AssociationDepth::AMBIGUOUS, initial: true) ⇒ Object
-
#parent ⇒ Object
-
#templates_dir ⇒ Object
-
#ts_instance_full_name ⇒ Object
-
#ts_instance_full_path ⇒ Object
-
#ts_instance_path ⇒ Object
-
#ts_type_full_path ⇒ Object
-
#value_to_ts_value(value) ⇒ Object
#absolute_template_path, #applicable?, #dependencies, #dependencies_to_generate, #eql?, #erb_template, #generate, generator_for, #generator_for, generators_for, #generators_for, #method_missing, #path_to_root, #respond_to_missing?, #target_dir, #target_path, #template_path, #template_string
#inspect, truncating
Constructor Details
Returns a new instance of TypescriptFromManifestBaseGenerator.
94
95
96
97
98
99
100
101
102
|
# File 'foobara-typescript-remote-command-generator-0.0.18/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 94
def initialize(relevant_manifest)
unless relevant_manifest.is_a?(Manifest::BaseManifest)
raise ArgumentError, "Expected a Foobara::Manifest, got #{relevant_manifest.class}"
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
|
# File 'foobara-typescript-remote-command-generator-0.0.18/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
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
raise "Not sure how build a generator for a #{manifest}"
end
end
|
Instance Method Details
#==(other) ⇒ Object
346
347
348
|
# File 'foobara-typescript-remote-command-generator-0.0.18/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 346
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
# File 'foobara-typescript-remote-command-generator-0.0.18/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 293
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
339
340
341
342
343
344
|
# File 'foobara-typescript-remote-command-generator-0.0.18/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 339
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_group ⇒ Object
114
115
116
117
118
119
120
121
122
|
# File 'foobara-typescript-remote-command-generator-0.0.18/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 114
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_roots ⇒ Object
124
125
126
127
128
129
130
131
132
133
|
# File 'foobara-typescript-remote-command-generator-0.0.18/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 124
def dependency_roots
unless dependency_group
raise "This generator was created without a " \
"dependency_group and therefore cannot call #{__method__}"
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
193
194
195
196
197
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
|
# File 'foobara-typescript-remote-command-generator-0.0.18/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 193
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
return is_empty ? "undefined" : "interface #{name} #{ts_type}"
else
is_empty ? "undefined" : ts_type
end
elsif type_declaration.is_a?(Manifest::Array)
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"
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.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
raise "Not sure how to convert #{type_declaration} to a TS type"
end
end
|
#hash ⇒ Object
350
351
352
|
# File 'foobara-typescript-remote-command-generator-0.0.18/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 350
def hash
path.hash
end
|
#import_destructure ⇒ Object
163
164
165
|
# File 'foobara-typescript-remote-command-generator-0.0.18/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 163
def import_destructure
"{ #{scoped_short_name} }"
end
|
#import_path ⇒ Object
155
156
157
158
159
160
161
|
# File 'foobara-typescript-remote-command-generator-0.0.18/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 155
def import_path
if import_path_array.size == 1
"./#{import_path_array.first}"
else
import_path_array.join("/")
end
end
|
#import_path_array ⇒ Object
167
168
169
170
171
172
173
174
175
176
177
|
# File 'foobara-typescript-remote-command-generator-0.0.18/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 167
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
#parent ⇒ Object
108
109
110
111
112
|
# File 'foobara-typescript-remote-command-generator-0.0.18/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 108
def parent
if relevant_manifest.parent
generator_for(relevant_manifest.parent)
end
end
|
#templates_dir ⇒ Object
104
105
106
|
# File 'foobara-typescript-remote-command-generator-0.0.18/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 104
def templates_dir
"#{__dir__}/../../../templates"
end
|
#ts_instance_full_name ⇒ Object
139
140
141
|
# File 'foobara-typescript-remote-command-generator-0.0.18/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 139
def ts_instance_full_name
ts_instance_full_path.join(".")
end
|
#ts_instance_full_path ⇒ Object
143
144
145
|
# File 'foobara-typescript-remote-command-generator-0.0.18/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 143
def ts_instance_full_path
scoped_full_path
end
|
#ts_instance_path ⇒ Object
135
136
137
|
# File 'foobara-typescript-remote-command-generator-0.0.18/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 135
def ts_instance_path
scoped_path
end
|
#ts_type_full_path ⇒ Object
147
148
149
|
# File 'foobara-typescript-remote-command-generator-0.0.18/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 147
def ts_type_full_path
ts_instance_full_path
end
|
#value_to_ts_value(value) ⇒ Object
179
180
181
182
183
184
185
186
187
188
189
190
|
# File 'foobara-typescript-remote-command-generator-0.0.18/src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 179
def value_to_ts_value(value)
case value
when ::String, Numeric
value.inspect
when ::Symbol
value.to_s.inspect
else
raise "Not sure how to represent #{value} in typescript. Maybe implement it."
end
end
|