Class: Lutaml::Model::KeyValueMapping

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/model/key_value_mapping.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeKeyValueMapping

Returns a new instance of KeyValueMapping.



8
9
10
# File 'lib/lutaml/model/key_value_mapping.rb', line 8

def initialize
  @mappings = []
end

Instance Attribute Details

#mappingsObject (readonly)

Returns the value of attribute mappings.



6
7
8
# File 'lib/lutaml/model/key_value_mapping.rb', line 6

def mappings
  @mappings
end

Instance Method Details

#deep_dupObject



65
66
67
68
69
# File 'lib/lutaml/model/key_value_mapping.rb', line 65

def deep_dup
  self.class.new.tap do |new_mapping|
    new_mapping.instance_variable_set(:@mappings, duplicate_mappings)
  end
end

#duplicate_mappingsObject



71
72
73
# File 'lib/lutaml/model/key_value_mapping.rb', line 71

def duplicate_mappings
  @mappings.map(&:deep_dup)
end

#find_by_to(to) ⇒ Object



75
76
77
# File 'lib/lutaml/model/key_value_mapping.rb', line 75

def find_by_to(to)
  @mappings.find { |m| m.to.to_s == to.to_s }
end

#map(name = nil, to: nil, render_nil: false, render_default: false, with: {}, delegate: nil, child_mappings: nil, root_mappings: nil) ⇒ Object Also known as: map_element



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/lutaml/model/key_value_mapping.rb', line 12

def map(
  name = nil,
  to: nil,
  render_nil: false,
  render_default: false,
  with: {},
  delegate: nil,
  child_mappings: nil,
  root_mappings: nil
)
  mapping_name = name_for_mapping(root_mappings, name)
  validate!(mapping_name, to, with)

  @mappings << KeyValueMappingRule.new(
    mapping_name,
    to: to,
    render_nil: render_nil,
    render_default: render_default,
    with: with,
    delegate: delegate,
    child_mappings: child_mappings,
    root_mappings: root_mappings,
  )
end

#name_for_mapping(root_mappings, name) ⇒ Object



39
40
41
42
43
# File 'lib/lutaml/model/key_value_mapping.rb', line 39

def name_for_mapping(root_mappings, name)
  return "root_mapping" if root_mappings

  name
end

#validate!(key, to, with) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/lutaml/model/key_value_mapping.rb', line 45

def validate!(key, to, with)
  if to.nil? && with.empty?
    msg = ":to or :with argument is required for mapping '#{key}'"
    raise IncorrectMappingArgumentsError.new(msg)
  end

  if !with.empty? && (with[:from].nil? || with[:to].nil?)
    msg = ":with argument for mapping '#{key}' requires :to and :from keys"
    raise IncorrectMappingArgumentsError.new(msg)
  end

  validate_mappings(key)
end

#validate_mappings(name) ⇒ Object



59
60
61
62
63
# File 'lib/lutaml/model/key_value_mapping.rb', line 59

def validate_mappings(name)
  if @mappings.any?(&:root_mapping?) || (name == "root_mapping" && @mappings.any?)
    raise MultipleMappingsError.new("root_mappings cannot be used with other mappings")
  end
end