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, #duplicate_mappings, #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

#register_mappings=(value) ⇒ Object (writeonly)

Writers for deep_dup in subclasses



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

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



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

def deep_dup
  dup_instance.tap do |new_mapping|
    new_mapping.mappings = duplicate_mappings
    new_mapping.register_mappings = Lutaml::Model::Utils.deep_dup(@register_mappings)
  end
end

#dup_instanceObject



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

def dup_instance
  self.class.new(@format)
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



271
272
273
# File 'lib/lutaml/key_value/mapping.rb', line 271

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

#find_by_to(to) ⇒ Object



259
260
261
# File 'lib/lutaml/key_value/mapping.rb', line 259

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

#find_by_to!(to) ⇒ Object



263
264
265
266
267
268
269
# File 'lib/lutaml/key_value/mapping.rb', line 263

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



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/lutaml/key_value/mapping.rb', line 176

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)


146
147
148
# File 'lib/lutaml/key_value/mapping.rb', line 146

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

#key(name = nil) ⇒ Object

Set the wrapper key for key-value serialization (JSON, YAML, TOML).

When set, serialized output wraps all instances under this key (e.g., ‘key “items”` produces `[…]`). When not called (or called with nil), instances are serialized directly at the top level (e.g., `[…]`).

Parameters:

  • name (String, nil) (defaults to: nil)

    the wrapper key name



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

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

#key_nameObject



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

def key_name
  @key
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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/lutaml/key_value/mapping.rb', line 65

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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/lutaml/key_value/mapping.rb', line 108

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



130
131
132
133
134
# File 'lib/lutaml/key_value/mapping.rb', line 130

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

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



136
137
138
139
# File 'lib/lutaml/key_value/mapping.rb', line 136

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

#map_to_instanceObject



150
151
152
153
154
155
# File 'lib/lutaml/key_value/mapping.rb', line 150

def map_to_instance
  return if !instance_mapping?

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

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



141
142
143
144
# File 'lib/lutaml/key_value/mapping.rb', line 141

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

#mappings(register_id = nil) ⇒ Object



163
164
165
166
# File 'lib/lutaml/key_value/mapping.rb', line 163

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

#mappings_hash(register_id = nil) ⇒ Object



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

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



157
158
159
160
161
# File 'lib/lutaml/key_value/mapping.rb', line 157

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

  name
end

#no_key?Boolean

Returns true when no wrapper key is set (instances serialized at top level).

Returns:

  • (Boolean)


51
52
53
# File 'lib/lutaml/key_value/mapping.rb', line 51

def no_key?
  @key.nil?
end

#no_rootObject

Deprecated.

Omit key call instead. Not calling key means no wrapper key.



46
47
48
# File 'lib/lutaml/key_value/mapping.rb', line 46

def no_root
  @key = nil
end

#no_root?Boolean

Deprecated.

Use #no_key? instead.

Returns:

  • (Boolean)


56
57
58
# File 'lib/lutaml/key_value/mapping.rb', line 56

def no_root?
  no_key?
end

#polymorphic_mappingObject



275
276
277
# File 'lib/lutaml/key_value/mapping.rb', line 275

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

#root(name = nil) ⇒ Object

Deprecated.

Use #key instead. In key-value formats, the wrapper is a key, not a root.



41
42
43
# File 'lib/lutaml/key_value/mapping.rb', line 41

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

#root_mappingObject



279
280
281
# File 'lib/lutaml/key_value/mapping.rb', line 279

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

#root_nameObject

Deprecated.

Use #key_name instead.



61
62
63
# File 'lib/lutaml/key_value/mapping.rb', line 61

def root_name
  @key
end

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



192
193
194
195
196
197
198
199
# File 'lib/lutaml/key_value/mapping.rb', line 192

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



229
230
231
232
233
234
235
236
# File 'lib/lutaml/key_value/mapping.rb', line 229

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



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

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



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

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



201
202
203
204
205
206
207
208
209
# File 'lib/lutaml/key_value/mapping.rb', line 201

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



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

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