Module: Lutaml::Lml::DataProcessor::InstanceProcessing

Included in:
Lutaml::Lml::DataProcessor
Defined in:
lib/lutaml/lml/data_processor/instance_processing.rb

Constant Summary collapse

INSTANCE_KEY_HANDLERS =
{
  instance: :process_instance,
  collections: :process_collections,
  imports: :process_imports,
  exports: :process_exports
}.freeze
INSTANCE_FIELD_HANDLERS =
{
  instance_type: :handle_instance_type,
  instance: :handle_instance_nested,
  attributes: :handle_instance_attributes,
  template: :handle_instance_template
}.freeze

Instance Method Summary collapse

Instance Method Details

#handle_instance_attributes(value, result) ⇒ Object



53
54
55
# File 'lib/lutaml/lml/data_processor/instance_processing.rb', line 53

def handle_instance_attributes(value, result)
  result[:attributes] = process_attributes(value)
end

#handle_instance_nested(value, result) ⇒ Object



49
50
51
# File 'lib/lutaml/lml/data_processor/instance_processing.rb', line 49

def handle_instance_nested(value, result)
  result[:instance] = process_instance(value)
end

#handle_instance_template(value, result) ⇒ Object



57
58
59
# File 'lib/lutaml/lml/data_processor/instance_processing.rb', line 57

def handle_instance_template(value, result)
  result[:template] = process_attributes(value[:attributes])
end

#handle_instance_type(value, result) ⇒ Object



45
46
47
# File 'lib/lutaml/lml/data_processor/instance_processing.rb', line 45

def handle_instance_type(value, result)
  result[:type] = process_value(value).last
end

#process_instance(hash) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/lutaml/lml/data_processor/instance_processing.rb', line 34

def process_instance(hash)
  hash.each_with_object({}) do |(key, value), result|
    handler = INSTANCE_FIELD_HANDLERS[key]
    if handler
      public_send(handler, value, result)
    else
      result[key] = process_value(value).last
    end
  end
end

#process_instances(obj) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lutaml/lml/data_processor/instance_processing.rb', line 21

def process_instances(obj)
  return [] unless obj.is_a?(Array)

  obj.each_with_object({}) do |instance, acc|
    acc[:instances] ||= []
    key = INSTANCE_KEY_HANDLERS.keys.find { |k| instance.key?(k) }
    next unless key

    result = public_send(INSTANCE_KEY_HANDLERS[key], instance[key])
    key == :instance ? (acc[:instances] << result) : (acc[key] = result)
  end
end