Class: Generator::Type

Inherits:
Object
  • Object
show all
Includes:
FileWriter, SchemaHelpers
Defined in:
lib/generator/builders/type.rb

Constant Summary

Constants included from Formatter

Formatter::MAX_LINE_LENGTH

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SchemaHelpers

#api_name_for_type_resolver, #attribute_name_for, #generate_nested_types!, #generate_rbs!, #generic_placeholder?, #nested_type_files

Methods included from Formatter

#convert_doc_links_to_full_url, #convert_html_links_to_yard, #format_method_definition, #split_long_comment_line

Methods included from FileWriter

#format_files, #write_file

Constructor Details

#initialize(name, definition, api_name, specification = nil) ⇒ Type

Returns a new instance of Type.



20
21
22
23
24
25
# File 'lib/generator/builders/type.rb', line 20

def initialize(name, definition, api_name, specification = nil)
  @name = name
  @definition = definition
  @api_name = api_name
  @specification = specification
end

Instance Attribute Details

#api_nameObject (readonly)

Returns the value of attribute api_name.



17
18
19
# File 'lib/generator/builders/type.rb', line 17

def api_name
  @api_name
end

#circular_dependenciesObject

Returns the value of attribute circular_dependencies.



18
19
20
# File 'lib/generator/builders/type.rb', line 18

def circular_dependencies
  @circular_dependencies
end

#cycle_edgesObject

Returns the value of attribute cycle_edges.



18
19
20
# File 'lib/generator/builders/type.rb', line 18

def cycle_edges
  @cycle_edges
end

#definitionObject (readonly)

Returns the value of attribute definition.



17
18
19
# File 'lib/generator/builders/type.rb', line 17

def definition
  @definition
end

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/generator/builders/type.rb', line 17

def name
  @name
end

#specificationObject (readonly)

Returns the value of attribute specification.



17
18
19
# File 'lib/generator/builders/type.rb', line 17

def specification
  @specification
end

Instance Method Details

#class_nameObject



31
32
33
# File 'lib/generator/builders/type.rb', line 31

def class_name
  Naming.class_name(name)
end

#generateObject



27
28
29
# File 'lib/generator/builders/type.rb', line 27

def generate
  write_file(file_path, render)
end

#library_nameObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/generator/builders/type.rb', line 54

def library_name
  # Use the acronym-transformed class name for filename to match Zeitwerk expectations
  # This ensures filenames like "claim_proof_urls.rb" instead of "claim_proof_ur_ls.rb"
  filename = class_name.underscore

  # For notification, report, feed, and data_kiosk nested types, use peddler/...
  if api_name.start_with?("notifications/", "reports/", "feeds/", "data_kiosk/")
    "peddler/#{api_name}/#{filename}"
  else
    "peddler/apis/#{api_name}/#{filename}"
  end
end

#needs_money?Boolean

Returns:

  • (Boolean)


116
117
118
119
120
121
122
123
124
125
# File 'lib/generator/builders/type.rb', line 116

def needs_money?
  properties.any? do |_prop_name, prop_def|
    if prop_def["$ref"]
      type_name = prop_def["$ref"].split("/").last
      MoneyDetector.money_type?(type_name)
    else
      false
    end
  end
end

#propertiesObject



35
36
37
38
39
40
41
42
43
# File 'lib/generator/builders/type.rb', line 35

def properties
  props = if definition["allOf"]
    merge_from_all_of("properties") { |props| props || {} }
  else
    definition["properties"] || {}
  end

  sorted_properties(props)
end

#rbs_class_definitionObject

Returns just the class definition without module wrapping Used for consolidated RBS generation



158
159
160
161
162
163
164
# File 'lib/generator/builders/type.rb', line 158

def rbs_class_definition
  if array_type?
    array_rbs_class_definition
  else
    structure_rbs_class_definition
  end
end

#required_propertiesObject



45
46
47
48
49
50
51
52
# File 'lib/generator/builders/type.rb', line 45

def required_properties
  if definition["allOf"]
    result = merge_from_all_of("required") { |req| req || [] }
    result.is_a?(Array) ? result.uniq : result
  else
    definition["required"] || []
  end
end

#ruby_type_for(prop_def, for_comment: false, for_rbs: false, prop_name: nil) ⇒ Object



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
97
98
99
# File 'lib/generator/builders/type.rb', line 67

def ruby_type_for(prop_def, for_comment: false, for_rbs: false, prop_name: nil)
  resolved_type = type_resolver.resolve(prop_def, for_comment: for_comment, for_rbs: for_rbs, prop_name: prop_name)

  # Handle self-references properly
  return resolved_type if resolved_type == ":self"

  # Only use string class names for the specific edges that cause cycles
  unless for_comment
    # Handle direct references
    if resolved_type.is_a?(String) &&
        resolved_type !~ /^[:\[]/ && resolved_type != "String" && resolved_type != "Integer" &&
        resolved_type != "Float" && !resolved_type.include?("Hash") && !resolved_type.include?("Array") &&
        resolved_type != "Money" && resolved_type != ":boolean"
      # Check if THIS specific edge causes a cycle
      if cycle_edges&.include?([name, resolved_type])
        # Return as a string for lazy loading
        return "\"#{resolved_type}\""
      end
    end

    # Handle arrays containing cycle-causing types
    if resolved_type.is_a?(String) && resolved_type =~ /^\[(.+)\]$/
      inner_type = ::Regexp.last_match(1)
      # Only use string class for actual cycles
      if inner_type && cycle_edges&.include?([name, inner_type])
        # Return as array with string class name for lazy loading
        return "[\"#{inner_type}\"]"
      end
    end
  end

  resolved_type
end

#type_dependenciesObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/generator/builders/type.rb', line 101

def type_dependencies
  dependencies = []
  properties.each do |_prop_name, prop_def|
    dependencies.concat(extract_dependencies_from_property(prop_def))
  end
  # Only include dependencies that actually get generated as type files
  # Exclude self-references to avoid requiring ourselves
  # Only exclude the specific edges that cause cycles (not all edges between circular types!)
  dependencies.select do |dep|
    generated_type?(dep) &&
      dep != name &&
      !(cycle_edges && cycle_edges.include?([name, dep]))
  end.uniq
end

#uses_string_class_names?Boolean

Returns:

  • (Boolean)


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/generator/builders/type.rb', line 127

def uses_string_class_names?
  properties.any? do |_prop_name, prop_def|
    resolved_type = type_resolver.resolve(prop_def)

    # Check for self-references
    if resolved_type == class_name || resolved_type == "[#{class_name}]"
      return true
    end

    # Check for cycle-causing edges
    if resolved_type.is_a?(String)
      # Handle direct types
      if resolved_type !~ /^[:\[]/ && resolved_type != "String" && resolved_type != "Integer" &&
          resolved_type != "Float" && resolved_type != "Hash" && resolved_type != "Array" &&
          resolved_type != "Money" && resolved_type != ":boolean"
        return true if cycle_edges&.include?([name, resolved_type])
      end

      # Handle arrays
      if resolved_type =~ /^\[(.+)\]$/
        inner_type = ::Regexp.last_match(1)
        return true if inner_type && cycle_edges&.include?([name, inner_type])
      end
    end

    false
  end
end