Class: Lutaml::Model::Schema::XmlCompiler::ComplexType

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/model/schema/xml_compiler/complex_type.rb

Constant Summary collapse

SERIALIZABLE_BASE_CLASS =
"Lutaml::Model::Serializable".freeze
SIMPLE_CONTENT_ATTRIBUTE_TEMPLATE =
ERB.new(<<~TEMPLATE, trim_mode: "-")
  <%= @indent %>attribute :content, :<%= simple_content_type %>
  <%= simple_content.to_attributes(@indent) if simple_content? -%>
TEMPLATE
TEMPLATE =
ERB.new(<<~TEMPLATE, trim_mode: "-")
  # frozen_string_literal: true

  <%= required_files.uniq.join("\n") + "\n" -%>
  class <%= Utils.camel_case(name) %> < <%= base_class_name %>
  <%= instances.flat_map { |instance| instance.to_attributes(@indent) }.join + "\n" -%>
  <%= simple_content_attribute -%>
  <%= @indent %>xml do
  <%= extended_indent %>root "<%= name %>"<%= root_options %>
  <%= namespace_and_prefix %>
  <%= "\#{extended_indent}map_content to: :content" if simple_content? || mixed %>
  <%= instances.flat_map { |instance| instance.to_xml_mapping(extended_indent) }.join -%>
  <%= simple_content.to_xml_mapping(extended_indent) if simple_content? -%>
  <%= @indent %>end

  <%= @indent %>def self.register
  <%= extended_indent %>Lutaml::Model::GlobalRegister.lookup(Lutaml::Model::Config.default_register)
  <%= @indent %>end

  <%= @indent %>def self.register_class_with_id
  <%= extended_indent %>register.register_model(self, id: :<%= Utils.snake_case(name) %>)
  <%= @indent %>end
  end

  <%= Utils.camel_case(name) %>.register_class_with_id
TEMPLATE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_class: SERIALIZABLE_BASE_CLASS) ⇒ ComplexType

Returns a new instance of ComplexType.



47
48
49
50
# File 'lib/lutaml/model/schema/xml_compiler/complex_type.rb', line 47

def initialize(base_class: SERIALIZABLE_BASE_CLASS)
  @base_class = base_class
  @instances = []
end

Instance Attribute Details

#base_classObject

Returns the value of attribute base_class.



6
7
8
# File 'lib/lutaml/model/schema/xml_compiler/complex_type.rb', line 6

def base_class
  @base_class
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/lutaml/model/schema/xml_compiler/complex_type.rb', line 6

def id
  @id
end

#instancesObject

Returns the value of attribute instances.



6
7
8
# File 'lib/lutaml/model/schema/xml_compiler/complex_type.rb', line 6

def instances
  @instances
end

#mixedObject

Returns the value of attribute mixed.



6
7
8
# File 'lib/lutaml/model/schema/xml_compiler/complex_type.rb', line 6

def mixed
  @mixed
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/lutaml/model/schema/xml_compiler/complex_type.rb', line 6

def name
  @name
end

#simple_contentObject

Returns the value of attribute simple_content.



6
7
8
# File 'lib/lutaml/model/schema/xml_compiler/complex_type.rb', line 6

def simple_content
  @simple_content
end

Instance Method Details

#<<(instance) ⇒ Object



52
53
54
55
56
# File 'lib/lutaml/model/schema/xml_compiler/complex_type.rb', line 52

def <<(instance)
  return if instance.nil?

  @instances << instance
end

#required_filesObject



67
68
69
70
71
72
# File 'lib/lutaml/model/schema/xml_compiler/complex_type.rb', line 67

def required_files
  files = [base_class_require]
  files.concat(@instances.map(&:required_files).flatten.compact.uniq)
  files.concat(simple_content.required_files) if simple_content?
  files
end

#simple_content?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/lutaml/model/schema/xml_compiler/complex_type.rb', line 58

def simple_content?
  Utils.present?(@simple_content)
end

#to_class(options: {}) ⇒ Object



62
63
64
65
# File 'lib/lutaml/model/schema/xml_compiler/complex_type.rb', line 62

def to_class(options: {})
  setup_options(options)
  TEMPLATE.result(binding)
end