Class: Lutaml::KeyValue::Mapping

Inherits:
Model::Mapping show all
Defined in:
lib/lutaml/key_value/mapping.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Model::Mapping

#add_listener, #all_listeners, #ensure_mappings_imported!, #inherit_from, #listeners_for, #omit_element, #omit_listener, #parent_mapping

Constructor Details

#initialize(format = nil) ⇒ Mapping

Returns a new instance of Mapping.



6
7
8
9
10
11
12
13
14
# File 'lib/lutaml/key_value/mapping.rb', line 6

def initialize(format = nil)
  super()
  @mappings = {}
  @key_mapping = {}
  @value_mapping = {}
  @register_mappings = ::Hash.new { |h, k| h[k] = {} }
  @format = format
  @finalized = false
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



4
5
6
# File 'lib/lutaml/key_value/mapping.rb', line 4

def format
  @format
end

#key_mappingObject (readonly)

Returns the value of attribute key_mapping.



4
5
6
# File 'lib/lutaml/key_value/mapping.rb', line 4

def key_mapping
  @key_mapping
end

#mappings(register_id = nil) ⇒ Object



138
139
140
141
# File 'lib/lutaml/key_value/mapping.rb', line 138

def mappings(register_id = nil)
  ensure_mappings_imported!(register_id) if finalized?
  mappings_hash(register_id).values
end

#register_mappings=(value) ⇒ Object (writeonly)

Writers for deep_dup in subclasses



221
222
223
# File 'lib/lutaml/key_value/mapping.rb', line 221

def register_mappings=(value)
  @register_mappings = value
end

#value_mappingObject (readonly)

Returns the value of attribute value_mapping.



4
5
6
# File 'lib/lutaml/key_value/mapping.rb', line 4

def value_mapping
  @value_mapping
end

Instance Method Details

#deep_dupObject



223
224
225
226
227
228
# File 'lib/lutaml/key_value/mapping.rb', line 223

def deep_dup
  self.class.new(@format).tap do |new_mapping|
    new_mapping.mappings = duplicate_mappings
    new_mapping.register_mappings = Lutaml::Model::Utils.deep_dup(@register_mappings)
  end
end

#duplicate_mappingsObject



230
231
232
# File 'lib/lutaml/key_value/mapping.rb', line 230

def duplicate_mappings
  Lutaml::Model::Utils.deep_dup(@mappings)
end

#finalize(_mapper_class) ⇒ Object



16
17
18
# File 'lib/lutaml/key_value/mapping.rb', line 16

def finalize(_mapper_class)
  @finalized = true
end

#finalized?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/lutaml/key_value/mapping.rb', line 20

def finalized?
  @finalized
end

#find_by_name(name) ⇒ Object



246
247
248
# File 'lib/lutaml/key_value/mapping.rb', line 246

def find_by_name(name)
  @mappings.find { |m| m.name.to_s == name.to_s }
end

#find_by_to(to) ⇒ Object



234
235
236
# File 'lib/lutaml/key_value/mapping.rb', line 234

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

#find_by_to!(to) ⇒ Object



238
239
240
241
242
243
244
# File 'lib/lutaml/key_value/mapping.rb', line 238

def find_by_to!(to)
  mapping = find_by_to(to)

  return mapping if !!mapping

  raise Lutaml::Model::NoMappingFoundError.new(to.to_s)
end

#import_model_mappings(model, register_id = nil) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/lutaml/key_value/mapping.rb', line 151

def import_model_mappings(model, register_id = nil)
  reg_id = register(register_id).id
  return import_mappings_later(model, reg_id) if model_importable?(model)

  if reg_id == :default
    @mappings.merge!(::Lutaml::Model::Utils.deep_dup(model.mappings_for(
      @format, reg_id
    ).mappings_hash))
  else
    @register_mappings[register_id].merge!(
      ::Lutaml::Model::Utils.deep_dup(model.mappings_for(@format,
                                                         register_id).mappings_hash),
    )
  end
end

#instance_mapping?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/lutaml/key_value/mapping.rb', line 121

def instance_mapping?
  @instance && (!@key_mapping.empty? || !@value_mapping.empty?)
end

#map(name = nil, to: nil, render_nil: false, render_default: false, render_empty: false, treat_nil: nil, treat_empty: nil, treat_omitted: nil, with: {}, delegate: nil, child_mappings: nil, root_mappings: nil, polymorphic: {}, polymorphic_map: {}, transform: {}, value_map: {}) ⇒ Object Also known as: map_element



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/lutaml/key_value/mapping.rb', line 40

def map(
  name = nil,
  to: nil,
  render_nil: false,
  render_default: false,
  render_empty: false,
  treat_nil: nil,
  treat_empty: nil,
  treat_omitted: nil,
  with: {},
  delegate: nil,
  child_mappings: nil,
  root_mappings: nil,
  polymorphic: {},
  polymorphic_map: {},
  transform: {},
  value_map: {}
)
  mapping_name = name_for_mapping(root_mappings, name)
  validate!(mapping_name, to, with, render_nil, render_empty)

  @mappings[mapping_name] = MappingRule.new(
    mapping_name,
    to: to,
    render_nil: render_nil,
    render_default: render_default,
    render_empty: render_empty,
    treat_nil: treat_nil,
    treat_empty: treat_empty,
    treat_omitted: treat_omitted,
    with: with,
    delegate: delegate,
    child_mappings: child_mappings,
    root_mappings: root_mappings,
    polymorphic: polymorphic,
    polymorphic_map: polymorphic_map,
    transform: transform,
    value_map: value_map,
  )
end

#map_all(to: nil, render_nil: false, render_default: false, with: {}, delegate: nil) ⇒ Object Also known as: map_all_content



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/lutaml/key_value/mapping.rb', line 83

def map_all(
  to: nil,
  render_nil: false,
  render_default: false,
  with: {},
  delegate: nil
)
  @raw_mapping = true
  validate!(Lutaml::Model::Constants::RAW_MAPPING_KEY, to, with,
            render_nil, nil)
  @mappings[Lutaml::Model::Constants::RAW_MAPPING_KEY] = MappingRule.new(
    Lutaml::Model::Constants::RAW_MAPPING_KEY,
    to: to,
    render_nil: render_nil,
    render_default: render_default,
    with: with,
    delegate: delegate,
  )
end

#map_instances(to:, polymorphic: {}) ⇒ Object



105
106
107
108
109
# File 'lib/lutaml/key_value/mapping.rb', line 105

def map_instances(to:, polymorphic: {})
  @instance = to
  map(root_name || to, to: to, polymorphic: polymorphic)
  map_to_instance
end

#map_key(to_instance: nil, as_attribute: nil) ⇒ Object



111
112
113
114
# File 'lib/lutaml/key_value/mapping.rb', line 111

def map_key(to_instance: nil, as_attribute: nil)
  @key_mapping = { "#{to_instance || as_attribute}": :key }
  map_to_instance
end

#map_to_instanceObject



125
126
127
128
129
130
# File 'lib/lutaml/key_value/mapping.rb', line 125

def map_to_instance
  return if !instance_mapping?

  mapping_name = name_for_mapping(nil, root_name || @instance)
  @mappings[mapping_name].child_mappings = @key_mapping.merge(@value_mapping)
end

#map_value(to_instance: nil, as_attribute: nil) ⇒ Object



116
117
118
119
# File 'lib/lutaml/key_value/mapping.rb', line 116

def map_value(to_instance: nil, as_attribute: nil)
  @value_mapping = { "#{to_instance || as_attribute}": :value }
  map_to_instance
end

#mappings_hash(register_id = nil) ⇒ Object



143
144
145
146
147
148
149
# File 'lib/lutaml/key_value/mapping.rb', line 143

def mappings_hash(register_id = nil)
  if register_id.nil? || register_id == :default
    @mappings
  else
    @mappings.merge(@register_mappings[register_id])
  end
end

#name_for_mapping(root_mappings, name) ⇒ Object



132
133
134
135
136
# File 'lib/lutaml/key_value/mapping.rb', line 132

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

  name
end

#no_rootObject



28
29
30
# File 'lib/lutaml/key_value/mapping.rb', line 28

def no_root
  @root = nil
end

#no_root?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/lutaml/key_value/mapping.rb', line 32

def no_root?
  @root.nil?
end

#polymorphic_mappingObject



250
251
252
# File 'lib/lutaml/key_value/mapping.rb', line 250

def polymorphic_mapping
  mappings.find(&:polymorphic_mapping?)
end

#root(name = nil) ⇒ Object



24
25
26
# File 'lib/lutaml/key_value/mapping.rb', line 24

def root(name = nil)
  @root = name
end

#root_mappingObject



254
255
256
# File 'lib/lutaml/key_value/mapping.rb', line 254

def root_mapping
  mappings.find(&:root_mapping?)
end

#root_nameObject



36
37
38
# File 'lib/lutaml/key_value/mapping.rb', line 36

def root_name
  @root
end

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



167
168
169
170
171
172
173
174
# File 'lib/lutaml/key_value/mapping.rb', line 167

def validate!(key, to, with, render_nil, render_empty)
  validate_mappings!(key)
  validate_to_and_with_arguments!(key, to, with)

  # Validate `render_nil` for unsupported value
  validate_blank_mappings!(render_nil, render_empty)
  validate_root_mappings!(key)
end

#validate_blank_mappings!(render_nil, render_empty) ⇒ Object



204
205
206
207
208
209
210
211
# File 'lib/lutaml/key_value/mapping.rb', line 204

def validate_blank_mappings!(render_nil, render_empty)
  if render_nil == :as_blank || render_empty == :as_blank
    raise Lutaml::Model::IncorrectMappingArgumentsError.new(
      ":as_blank is not supported for key-value mappings. " \
      "Use :as_empty instead to create explicit empty values.",
    )
  end
end

#validate_mappings!(_type) ⇒ Object



213
214
215
216
217
218
# File 'lib/lutaml/key_value/mapping.rb', line 213

def validate_mappings!(_type)
  if (@raw_mapping && Lutaml::Model::Utils.present?(@mappings)) ||
      (!@raw_mapping && mappings.any?(&:raw_mapping?))
    raise StandardError, "map_all is not allowed with other mappings"
  end
end

#validate_root_mappings!(name) ⇒ Object



196
197
198
199
200
201
202
# File 'lib/lutaml/key_value/mapping.rb', line 196

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

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



176
177
178
179
180
181
182
183
184
# File 'lib/lutaml/key_value/mapping.rb', line 176

def validate_to_and_with_arguments!(key, to, with)
  if to.nil? && with.empty? && !@raw_mapping
    raise Lutaml::Model::IncorrectMappingArgumentsError.new(
      ":to or :with argument is required for mapping '#{key}'",
    )
  end

  validate_with_options!(to, key, with)
end

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



186
187
188
189
190
191
192
193
194
# File 'lib/lutaml/key_value/mapping.rb', line 186

def validate_with_options!(to, key, with)
  return true if to

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