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 %><%= ", collection: true" if mixed && !simple_content? %>
  <%= simple_content.to_attributes(@indent) if simple_content? -%>
TEMPLATE
TEMPLATE =
ERB.new(<<~TEMPLATE, trim_mode: "-")
  # frozen_string_literal: true

  require "lutaml/model"
  <%= required_files.uniq.join("\n") + "\n" -%>
  <%= module_opening -%>
  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 %>element "<%= name %>"
  <%= extended_indent %><%= mixed_content? %>
  <%= 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
  <%= registration_methods -%>
  end
  <%= module_closing -%>
  <%= registration_execution -%>
TEMPLATE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_class: SERIALIZABLE_BASE_CLASS) ⇒ ComplexType

Returns a new instance of ComplexType.



43
44
45
46
47
# File 'lib/lutaml/model/schema/xml_compiler/complex_type.rb', line 43

def initialize(base_class: SERIALIZABLE_BASE_CLASS)
  @base_class = base_class
  @instances = []
  @module_namespace = nil
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



49
50
51
52
53
# File 'lib/lutaml/model/schema/xml_compiler/complex_type.rb', line 49

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

  @instances << instance
end

#required_filesObject



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

def required_files
  files = []
  # Only include external gem requires, not schema class requires
  # Schema class dependencies are handled via autoload registry
  unless @module_namespace
    files.concat(@instances.map(&:required_files).flatten.compact.uniq)
    files.concat(simple_content.required_files) if simple_content?
  end
  files
end

#simple_content?Boolean

Returns:

  • (Boolean)


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

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

#to_class(options: {}) ⇒ Object



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

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