Module: Generator::SchemaGenerator

Included in:
DataKiosk, Feed, Notification, Report
Defined in:
lib/generator/support/schema_generator.rb

Overview

Shared functionality for schema-based generators (Notification, Report, Feed) Provides common patterns for file discovery, cleanup, and generation

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



10
11
12
# File 'lib/generator/support/schema_generator.rb', line 10

def included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#generateObject

Template method: generate every file for a single schema. Per-type specifics are supplied via the generate_schema_types and generate_supplementary_files hooks.



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
# File 'lib/generator/support/schema_generator.rb', line 60

def generate
  written_files = []
  all_types = []

  # Generate nested types first
  nested_results = generate_nested_types!
  written_files.concat(nested_results[:files])
  all_types.concat(nested_results[:types])

  generate_schema_types(written_files, all_types)

  # Generate main convenience file
  written_files << generate_main_file!

  generate_supplementary_files(written_files, all_types)

  # Reload to pick up newly generated files for RBS introspection
  IntrospectionLoader.reload
  written_files << generate_rbs!(all_types)

  # Batch format all written files
  format_files(written_files)

  singular = self.class.schema_type.singularize
  Generator.logger.info("Generated #{singular} #{send("#{singular}_name").underscore}")
end

#generate_main_file!Object

Generate main convenience file using ERB template



95
96
97
98
# File 'lib/generator/support/schema_generator.rb', line 95

def generate_main_file!
  content = ERB.new(main_template, trim_mode: "-").result(binding)
  write_file(output_file_path, content)
end

#generate_schema_types(_written_files, _all_types) ⇒ Object

Hook: append the primary schema type(s) to written_files/all_types. Default no-op (Data Kiosk has no standalone main type).



89
# File 'lib/generator/support/schema_generator.rb', line 89

def generate_schema_types(_written_files, _all_types); end

#generate_supplementary_files(_written_files, _all_types) ⇒ Object

Hook: generate extra files after the main convenience file. Default no-op.



92
# File 'lib/generator/support/schema_generator.rb', line 92

def generate_supplementary_files(_written_files, _all_types); end

#main_templateObject

Get main template content



107
108
109
110
# File 'lib/generator/support/schema_generator.rb', line 107

def main_template
  template_name = self.class.name.split("::").last.underscore
  File.read(Config.template_path(template_name))
end

#needs_money?Boolean

Check if any types use Money (shared implementation)

Returns:

  • (Boolean)


123
124
125
126
127
# File 'lib/generator/support/schema_generator.rb', line 123

def needs_money?
  extractor = JsonSchemaExtractor.new(schema, send(:name))
  extracted_types = extractor.extract_types
  super(extracted_types)
end

#output_file_pathObject

Get output file path for main file



113
114
115
116
117
118
119
120
# File 'lib/generator/support/schema_generator.rb', line 113

def output_file_path
  schema_name_method = "#{self.class.schema_type.singularize}_name"
  schema_name = send(schema_name_method)
  File.join(
    Config::BASE_PATH,
    "lib/peddler/#{self.class.schema_type}/#{schema_name.underscore}.rb",
  )
end

#sorted_properties(props) ⇒ Object

Sort properties (natural alphabetical order by default) Can be overridden for custom sorting



102
103
104
# File 'lib/generator/support/schema_generator.rb', line 102

def sorted_properties(props)
  props.sort.to_h
end