Class: Lutaml::Model::Schema::XmlCompiler::Group

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

Constant Summary collapse

GROUP_TEMPLATE =
ERB.new(<<~TEMPLATE, trim_mode: "-")
  # frozen_string_literal: true

  require "lutaml/model"
  <%=  "\n" + required_files.uniq.join("\n") -%>

  class <%= Utils.camel_case(base_name) %> < Lutaml::Model::Serializable
  <%= definitions_content %>
  <%= xml_mapping_block -%>

  <%= @indent %>def self.register
  <%= extended_indent %>@register ||= Lutaml::Model::Config.default_register
  <%= @indent %>end

  <%= @indent %>def self.register_class_with_id
  <%= extended_indent %>context = Lutaml::Model::GlobalContext.context(Lutaml::Model::Config.default_register)
  <%= extended_indent %>context.registry.register(:<%= Utils.snake_case(base_name) %>, self)
  <%= @indent %>end
  end

  <%= Utils.camel_case(base_name) %>.register_class_with_id
TEMPLATE
XML_MAPPING_TEMPLATE =
ERB.new(<<~TEMPLATE, trim_mode: "-")
  <%= @indent %>xml do
  <%= extended_indent %>type_name "<%= base_name %>"
  <%= xml_mapping_content -%>
  <%= @indent %>end
TEMPLATE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, ref = nil) ⇒ Group

Returns a new instance of Group.



40
41
42
43
# File 'lib/lutaml/model/schema/xml_compiler/group.rb', line 40

def initialize(name = nil, ref = nil)
  @name = name
  @ref = ref
end

Instance Attribute Details

#instanceObject

Returns the value of attribute instance.



8
9
10
# File 'lib/lutaml/model/schema/xml_compiler/group.rb', line 8

def instance
  @instance
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/lutaml/model/schema/xml_compiler/group.rb', line 8

def name
  @name
end

#refObject

Returns the value of attribute ref.



8
9
10
# File 'lib/lutaml/model/schema/xml_compiler/group.rb', line 8

def ref
  @ref
end

Instance Method Details

#base_nameObject



74
75
76
# File 'lib/lutaml/model/schema/xml_compiler/group.rb', line 74

def base_name
  (name || ref)&.split(":")&.last
end

#required_filesObject



58
59
60
61
62
63
64
# File 'lib/lutaml/model/schema/xml_compiler/group.rb', line 58

def required_files
  if Utils.blank?(name) && Utils.present?(ref)
    "require_relative \"#{Utils.snake_case(ref.split(':').last)}\""
  else
    @instance&.required_files
  end
end

#to_attributes(indent = @indent) ⇒ Object



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

def to_attributes(indent = @indent)
  if Utils.present?(@ref)
    "#{indent}import_model_attributes :#{Utils.snake_case(base_name)}\n"
  else
    @instance.to_attributes(indent)
  end
end

#to_class(options: {}) ⇒ Object



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

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

#to_xml_mapping(indent = @indent) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/lutaml/model/schema/xml_compiler/group.rb', line 45

def to_xml_mapping(indent = @indent)
  if Utils.present?(@ref)
    "#{indent}import_model_mappings :#{Utils.snake_case(base_name)}\n"
  else
    @instance.to_xml_mapping(indent * 2)
  end
end