Class: Foobara::RemoteGenerator::Services::TypescriptFromManifestBaseGenerator
- Inherits:
-
FilesGenerator
- Object
- FilesGenerator
- Foobara::RemoteGenerator::Services::TypescriptFromManifestBaseGenerator
- Defined in:
- foobara-typescript-remote-command-generator-1.2.0/src/remote_generator/services/typescript_from_manifest_base_generator.rb
Direct Known Subclasses
Auth::AccessTokensGenerator, Auth::LoginCommandGenerator, Auth::LogoutCommandGenerator, Auth::RequiresAuthCommandGenerator, CommandGenerator, CommandInputsGenerator, DomainGenerator, ErrorGenerator, ManifestGenerator, OrganizationGenerator, ProcessorClassGenerator, TypeGenerator
Constant Summary
Constants included from TruncatedInspect
Instance Attribute Summary
Attributes inherited from FilesGenerator
#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, is_output: false, parent: nil) ⇒ 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, is_output: false, parent: nil) ⇒ Object
is_output means the value came from elsewhere and is fully formed.
- #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
- #path_to_root ⇒ 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
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
Constructor Details
#initialize(relevant_manifest) ⇒ TypescriptFromManifestBaseGenerator
Returns a new instance of TypescriptFromManifestBaseGenerator.
97 98 99 100 101 102 103 104 105 |
# File 'src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 97 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
Instance Method Details
#==(other) ⇒ Object
397 398 399 |
# File 'src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 397 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, is_output: false, parent: nil) ⇒ 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 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 319 def attributes_to_ts_type( attributes, dependency_group:, association_depth: AssociationDepth::AMBIGUOUS, model_and_entity_free: false, is_output: false, parent: nil ) # TODO: if we don't actually have attribute_declarations because we # are trying to express attributes of any type, then we want Record<string, any> # or something. if attributes.has_attribute_declarations? guts = attributes.attribute_declarations.map do |attribute_name, attribute_declaration| is_required = attributes.required?(attribute_name) if !is_required && is_output default = attributes.default_for(attribute_name) if default || default == false || (parent&.detached_entity? && attribute_name == parent.primary_key_name.to_sym) is_required = true end end " #{attribute_name}#{"?" unless is_required}: #{ ( attribute_declaration, dependency_group:, association_depth:, initial: false, model_and_entity_free:, is_output: ) }" end.join("\n") "{\n#{guts}\n}" else # TODO: test this path with an :attributes type (not extended from :attributes but # the built-in type itself directly) # :nocov: "Record<string, any>" # :nocov: end end |
#custom_type_to_ts_type_name(type) ⇒ Object
390 391 392 393 394 395 |
# File 'src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 390 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
117 118 119 120 121 122 123 124 125 |
# File 'src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 117 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
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 127 def dependency_roots return @dependency_roots if defined?(@dependency_roots) unless dependency_group # :nocov: raise "This generator was created without a " \ "dependency_group and therefore cannot call #{__method__}" # :nocov: end @dependency_roots = 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, is_output: false, parent: nil) ⇒ Object
is_output means the value came from elsewhere and is fully formed. This is helpful for specifying what is expected to be present. If this is provided, then things like attribute properties that have defaults will be considered required and present.
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 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 201 def ( type_declaration, dependency_group: self.dependency_group, name: nil, association_depth: AssociationDepth::AMBIGUOUS, initial: true, model_and_entity_free: false, is_output: false, parent: nil ) 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_output:, parent: ) if type_declaration.has_attribute_declarations? 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 else # TODO: test this path with an :attributes type (not extended from :attributes but # the built-in type itself directly) # :nocov: ts_type # :nocov: end elsif type_declaration.is_a?(Manifest::Array) # TODO: which association_depth do we pass here? ts_type = ( type_declaration.element_type, association_depth:, dependency_group:, initial: false, model_and_entity_free:, is_output: ) "#{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" "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 ( translated_type, association_depth:, dependency_group:, initial:, model_and_entity_free:, is_output:, parent: model_type ) 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 unless type_declaration.reference? 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 end # TODO: Add description as a comment? name ? "type #{name} = #{type_string}" : type_string else # :nocov: raise "Not sure how to convert #{type_declaration} to a TS type" # :nocov: end end |
#hash ⇒ Object
401 402 403 |
# File 'src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 401 def hash path.hash end |
#import_destructure ⇒ Object
168 169 170 |
# File 'src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 168 def import_destructure "{ #{scoped_short_name} }" end |
#import_path ⇒ Object
160 161 162 163 164 165 166 |
# File '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_array ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 |
# File '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
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 365 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 |
#parent ⇒ Object
111 112 113 114 115 |
# File 'src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 111 def parent if relevant_manifest.parent generator_for(relevant_manifest.parent) end end |
#path_to_root ⇒ Object
405 406 407 408 |
# File 'src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 405 def path_to_root path = super path.empty? ? "./" : path end |
#templates_dir ⇒ Object
107 108 109 |
# File 'src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 107 def templates_dir "#{__dir__}/../../../templates" end |
#ts_instance_full_name ⇒ Object
144 145 146 |
# File '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_path ⇒ Object
148 149 150 |
# File 'src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 148 def ts_instance_full_path scoped_full_path end |
#ts_instance_path ⇒ Object
140 141 142 |
# File 'src/remote_generator/services/typescript_from_manifest_base_generator.rb', line 140 def ts_instance_path scoped_path end |
#ts_type_full_path ⇒ Object
152 153 154 |
# File '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 '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 |