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

Inherits:
TypescriptFromManifestBaseGenerator show all
Defined in:
foobara-typescript-remote-command-generator-0.0.24/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

.new(relevant_manifest) ⇒ Object

[View source]

8
9
10
11
12
13
14
15
16
17
18
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/type_generator.rb', line 8

def new(relevant_manifest)
  return super unless self == TypeGenerator

  if relevant_manifest.detached_entity?
    EntityGenerator.new(relevant_manifest)
  elsif relevant_manifest.model?
    ModelGenerator.new(relevant_manifest)
  else
    super
  end
end

Instance Method Details

#aggregate_attributes_ts_typeObject

[View source]

119
120
121
122
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/type_generator.rb', line 119

def aggregate_attributes_ts_type
  association_depth = AssociationDepth::AGGREGATE
  foobara_type_to_ts_type(attributes_type, association_depth:, dependency_group:)
end

#atom_attributes_ts_typeObject

[View source]

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

def atom_attributes_ts_type
  association_depth = AssociationDepth::ATOM
  foobara_type_to_ts_type(attributes_type, association_depth:, dependency_group:)
end

#attribute_namesObject

[View source]

124
125
126
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/type_generator.rb', line 124

def attribute_names
  attributes_type.attribute_names
end

#attributes_type_ts_typeObject

[View source]

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

def attributes_type_ts_type
  association_depth = AssociationDepth::AMBIGUOUS
  foobara_type_to_ts_type(attributes_type, association_depth:, dependency_group:)
end

#custom_type_generatorsObject

[View source]

95
96
97
98
99
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/type_generator.rb', line 95

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

#dependenciesObject

[View source]

101
102
103
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/type_generator.rb', line 101

def dependencies
  custom_type_generators + model_generators
end

#model_generatorsObject

[View source]

89
90
91
92
93
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/type_generator.rb', line 89

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

[View source]

49
50
51
52
53
54
55
56
57
58
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/type_generator.rb', line 49

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

[View source]

23
24
25
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/type_generator.rb', line 23

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

#template_pathObject

[View source]

31
32
33
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/type_generator.rb', line 31

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

#type_gutsObject

[View source]

68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/type_generator.rb', line 68

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

[View source]

60
61
62
63
64
65
66
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/type_generator.rb', line 60

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

#type_name_downcaseObject

[View source]

105
106
107
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/type_generator.rb', line 105

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

#type_prefixObject

[View source]

35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/type_generator.rb', line 35

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

[View source]

27
28
29
# File 'foobara-typescript-remote-command-generator-0.0.24/src/remote_generator/services/type_generator.rb', line 27

def type_short_name
  scoped_short_name
end