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



47
48
49
50
51
# File 'lib/lutaml/model/key_value_mapping.rb', line 47

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

#duplicate_mappingsObject



53
54
55
# File 'lib/lutaml/model/key_value_mapping.rb', line 53

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

#map(name, to: nil, render_nil: false, render_default: false, with: {}, delegate: nil, child_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
# File 'lib/lutaml/model/key_value_mapping.rb', line 12

def map(
  name,
  to: nil,
  render_nil: false,
  render_default: false,
  with: {},
  delegate: nil,
  child_mappings: nil
)
  validate!(name, to, with)
  @mappings << KeyValueMappingRule.new(
    name,
    to: to,
    render_nil: render_nil,
    render_default: render_default,
    with: with,
    delegate: delegate,
    child_mappings: child_mappings,
  )
end

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



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lutaml/model/key_value_mapping.rb', line 35

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
end