Class: Uniword::Schema::ModelGenerator
- Inherits:
-
Object
- Object
- Uniword::Schema::ModelGenerator
- Defined in:
- lib/uniword/schema/model_generator.rb
Overview
ModelGenerator generates lutaml-model classes from YAML schema definitions
This generator creates complete, proper lutaml-model classes that follow ALL architectural rules, especially Pattern 0 (attributes BEFORE xml blocks).
Instance Attribute Summary collapse
-
#namespace_info ⇒ Object
readonly
Returns the value of attribute namespace_info.
-
#output_dir ⇒ Object
readonly
Returns the value of attribute output_dir.
-
#schema_name ⇒ Object
readonly
Returns the value of attribute schema_name.
Instance Method Summary collapse
-
#generate_all ⇒ Hash
Generate all classes for this schema.
-
#generate_class_code(element_name, element_def) ⇒ String
Generate complete class code.
-
#generate_element_class(element_name) ⇒ String
Generate a single element class.
-
#initialize(schema_name, output_dir: nil) ⇒ ModelGenerator
constructor
A new instance of ModelGenerator.
Constructor Details
#initialize(schema_name, output_dir: nil) ⇒ ModelGenerator
Returns a new instance of ModelGenerator.
18 19 20 21 22 23 24 |
# File 'lib/uniword/schema/model_generator.rb', line 18 def initialize(schema_name, output_dir: nil) @schema_name = schema_name @loader = SchemaLoader.instance @namespace_info = @loader.namespace(schema_name) @output_dir = output_dir || File.join(__dir__, "../../generated", schema_name) end |
Instance Attribute Details
#namespace_info ⇒ Object (readonly)
Returns the value of attribute namespace_info.
16 17 18 |
# File 'lib/uniword/schema/model_generator.rb', line 16 def namespace_info @namespace_info end |
#output_dir ⇒ Object (readonly)
Returns the value of attribute output_dir.
16 17 18 |
# File 'lib/uniword/schema/model_generator.rb', line 16 def output_dir @output_dir end |
#schema_name ⇒ Object (readonly)
Returns the value of attribute schema_name.
16 17 18 |
# File 'lib/uniword/schema/model_generator.rb', line 16 def schema_name @schema_name end |
Instance Method Details
#generate_all ⇒ Hash
Generate all classes for this schema
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/uniword/schema/model_generator.rb', line 29 def generate_all element_names = @loader.element_names(schema_name) # Ensure output directory exists FileUtils.mkdir_p(@output_dir) results = {} element_names.each do |element_name| file_path = generate_element_class(element_name) results[element_name] = file_path end results end |
#generate_class_code(element_name, element_def) ⇒ String
Generate complete class code
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 |
# File 'lib/uniword/schema/model_generator.rb', line 70 def generate_class_code(element_name, element_def) class_name = element_def["class_name"] description = element_def["description"] attributes = element_def["attributes"] || [] <<~RUBY # frozen_string_literal: true require 'lutaml/model' module Uniword module Generated module #{camelize(schema_name)} # #{description} # # Generated from OOXML schema: #{schema_name}.yml # Element: <#{@namespace_info['prefix']}:#{element_name}> class #{class_name} < Lutaml::Model::Serializable #{generate_attributes(attributes)} #{generate_xml_mapping(element_name, attributes)} end end end end RUBY end |
#generate_element_class(element_name) ⇒ String
Generate a single element class
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/uniword/schema/model_generator.rb', line 48 def generate_element_class(element_name) element_def = @loader.element_definition(schema_name, element_name) raise "Element '#{element_name}' not found in schema '#{schema_name}'" unless element_def # Ensure output directory exists FileUtils.mkdir_p(@output_dir) class_name = element_def["class_name"] file_name = underscore(class_name) file_path = File.join(@output_dir, "#{file_name}.rb") code = generate_class_code(element_name, element_def) File.write(file_path, code) file_path end |