Class: Generator::Type
- Inherits:
-
Object
- Object
- Generator::Type
- Includes:
- FileWriter, SchemaHelpers
- Defined in:
- lib/generator/type.rb
Constant Summary
Constants included from Formatter
Instance Attribute Summary collapse
-
#api_name ⇒ Object
readonly
Returns the value of attribute api_name.
-
#circular_dependencies ⇒ Object
Returns the value of attribute circular_dependencies.
-
#cycle_edges ⇒ Object
Returns the value of attribute cycle_edges.
-
#definition ⇒ Object
readonly
Returns the value of attribute definition.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#specification ⇒ Object
readonly
Returns the value of attribute specification.
Instance Method Summary collapse
- #class_name ⇒ Object
- #generate ⇒ Object
-
#initialize(name, definition, api_name, specification = nil) ⇒ Type
constructor
A new instance of Type.
- #library_name ⇒ Object
- #needs_money? ⇒ Boolean
- #properties ⇒ Object
-
#rbs_class_definition ⇒ Object
Returns just the class definition without module wrapping Used for consolidated RBS generation.
- #required_properties ⇒ Object
- #ruby_type_for(prop_def, for_comment: false, for_rbs: false, prop_name: nil) ⇒ Object
- #type_dependencies ⇒ Object
- #uses_string_class_names? ⇒ Boolean
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
Constructor Details
#initialize(name, definition, api_name, specification = nil) ⇒ Type
Returns a new instance of Type.
18 19 20 21 22 23 |
# File 'lib/generator/type.rb', line 18 def initialize(name, definition, api_name, specification = nil) @name = name @definition = definition @api_name = api_name @specification = specification end |
Instance Attribute Details
#api_name ⇒ Object (readonly)
Returns the value of attribute api_name.
15 16 17 |
# File 'lib/generator/type.rb', line 15 def api_name @api_name end |
#circular_dependencies ⇒ Object
Returns the value of attribute circular_dependencies.
16 17 18 |
# File 'lib/generator/type.rb', line 16 def circular_dependencies @circular_dependencies end |
#cycle_edges ⇒ Object
Returns the value of attribute cycle_edges.
16 17 18 |
# File 'lib/generator/type.rb', line 16 def cycle_edges @cycle_edges end |
#definition ⇒ Object (readonly)
Returns the value of attribute definition.
15 16 17 |
# File 'lib/generator/type.rb', line 15 def definition @definition end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
15 16 17 |
# File 'lib/generator/type.rb', line 15 def name @name end |
#specification ⇒ Object (readonly)
Returns the value of attribute specification.
15 16 17 |
# File 'lib/generator/type.rb', line 15 def specification @specification end |
Instance Method Details
#class_name ⇒ Object
29 30 31 32 33 34 |
# File 'lib/generator/type.rb', line 29 def class_name # Use ActiveSupport's camelize and apply Peddler inflector acronyms camelized = name.camelize # Apply the same acronym rules as Peddler::Inflector Peddler::Acronyms.apply(camelized) end |
#generate ⇒ Object
25 26 27 |
# File 'lib/generator/type.rb', line 25 def generate write_file(file_path, render) end |
#library_name ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/generator/type.rb', line 55 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, and feed nested types, use peddler/notifications, peddler/reports, or peddler/feeds if api_name.start_with?("notifications/", "reports/", "feeds/") "peddler/#{api_name}/#{filename}" else "peddler/apis/#{api_name}/#{filename}" end end |
#needs_money? ⇒ Boolean
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/generator/type.rb', line 117 def needs_money? properties.any? do |_prop_name, prop_def| if prop_def["$ref"] type_name = prop_def["$ref"].split("/").last TypeResolver::MONEY_TYPES.include?(type_name) else false end end end |
#properties ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/generator/type.rb', line 36 def properties props = if definition["allOf"] merge_from_all_of("properties") { |props| props || {} } else definition["properties"] || {} end sorted_properties(props) end |
#rbs_class_definition ⇒ Object
Returns just the class definition without module wrapping Used for consolidated RBS generation
159 160 161 162 163 164 165 |
# File 'lib/generator/type.rb', line 159 def rbs_class_definition if array_type? array_rbs_class_definition else structure_rbs_class_definition end end |
#required_properties ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/generator/type.rb', line 46 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
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 100 |
# File 'lib/generator/type.rb', line 68 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_dependencies ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/generator/type.rb', line 102 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
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 155 |
# File 'lib/generator/type.rb', line 128 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 |