Class: Lutaml::KeyValue::Mapping
- Inherits:
-
Model::Mapping
- Object
- Model::Mapping
- Lutaml::KeyValue::Mapping
- Defined in:
- lib/lutaml/key_value/mapping.rb
Direct Known Subclasses
HashFormat::Adapter::Mapping, Json::Adapter::Mapping, Jsonl::Adapter::Mapping, Adapter::Hash::Mapping, Adapter::Json::Mapping, Adapter::Jsonl::Mapping, Adapter::Toml::Mapping, Adapter::Yaml::Mapping, Adapter::Yamls::Mapping, Toml::Adapter::Mapping, Yaml::Adapter::Mapping, Yamls::Adapter::Mapping
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#key_mapping ⇒ Object
readonly
Returns the value of attribute key_mapping.
-
#register_mappings ⇒ Object
writeonly
Writers for deep_dup in subclasses.
-
#value_mapping ⇒ Object
readonly
Returns the value of attribute value_mapping.
Instance Method Summary collapse
- #deep_dup ⇒ Object
- #dup_instance ⇒ Object
- #finalize(_mapper_class) ⇒ Object
- #finalized? ⇒ Boolean
- #find_by_name(name) ⇒ Object
- #find_by_to(to) ⇒ Object
- #find_by_to!(to) ⇒ Object
- #import_model_mappings(model, register_id = nil) ⇒ Object
-
#initialize(format = nil) ⇒ Mapping
constructor
A new instance of Mapping.
- #instance_mapping? ⇒ Boolean
-
#key(name = nil) ⇒ Object
Set the wrapper key for key-value serialization (JSON, YAML, TOML).
- #key_name ⇒ Object
- #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: {}, serialize: true) ⇒ Object (also: #map_element)
- #map_all(to: nil, render_nil: false, render_default: false, with: {}, delegate: nil) ⇒ Object (also: #map_all_content)
- #map_instances(to:, polymorphic: {}) ⇒ Object
- #map_key(to_instance: nil, as_attribute: nil) ⇒ Object
- #map_to_instance ⇒ Object
- #map_value(to_instance: nil, as_attribute: nil) ⇒ Object
- #mappings(register_id = nil) ⇒ Object
- #mappings_hash(register_id = nil) ⇒ Object
- #name_for_mapping(root_mappings, name) ⇒ Object
-
#no_key? ⇒ Boolean
Returns true when no wrapper key is set (instances serialized at top level).
-
#no_root ⇒ Object
deprecated
Deprecated.
Omit key call instead. Not calling key means no wrapper key.
-
#no_root? ⇒ Boolean
deprecated
Deprecated.
Use #no_key? instead.
- #polymorphic_mapping ⇒ Object
-
#root(name = nil) ⇒ Object
deprecated
Deprecated.
Use #key instead. In key-value formats, the wrapper is a key, not a root.
- #root_mapping ⇒ Object
-
#root_name ⇒ Object
deprecated
Deprecated.
Use #key_name instead.
- #validate!(key, to, with, render_nil, render_empty) ⇒ Object
- #validate_blank_mappings!(render_nil, render_empty) ⇒ Object
- #validate_mappings!(_type) ⇒ Object
- #validate_root_mappings!(name) ⇒ Object
- #validate_to_and_with_arguments!(key, to, with) ⇒ Object
- #validate_with_options!(to, key, with) ⇒ Object
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
#format ⇒ Object (readonly)
Returns the value of attribute format.
4 5 6 |
# File 'lib/lutaml/key_value/mapping.rb', line 4 def format @format end |
#key_mapping ⇒ Object (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
248 249 250 |
# File 'lib/lutaml/key_value/mapping.rb', line 248 def register_mappings=(value) @register_mappings = value end |
#value_mapping ⇒ Object (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_dup ⇒ Object
250 251 252 253 254 255 |
# File 'lib/lutaml/key_value/mapping.rb', line 250 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_instance ⇒ Object
257 258 259 |
# File 'lib/lutaml/key_value/mapping.rb', line 257 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
20 21 22 |
# File 'lib/lutaml/key_value/mapping.rb', line 20 def finalized? @finalized end |
#find_by_name(name) ⇒ Object
273 274 275 |
# File 'lib/lutaml/key_value/mapping.rb', line 273 def find_by_name(name) @mappings.find { |m| m.name.to_s == name.to_s } end |
#find_by_to(to) ⇒ Object
261 262 263 |
# File 'lib/lutaml/key_value/mapping.rb', line 261 def find_by_to(to) mappings.find { |m| m.to.to_s == to.to_s } end |
#find_by_to!(to) ⇒ Object
265 266 267 268 269 270 271 |
# File 'lib/lutaml/key_value/mapping.rb', line 265 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
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/lutaml/key_value/mapping.rb', line 178 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
148 149 150 |
# File 'lib/lutaml/key_value/mapping.rb', line 148 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 {"items": [...]}).
When not called (or called with nil), instances are serialized directly
at the top level (e.g., [...]).
32 33 34 |
# File 'lib/lutaml/key_value/mapping.rb', line 32 def key(name = nil) @key = name end |
#key_name ⇒ Object
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: {}, serialize: true) ⇒ 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 105 106 |
# 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: {}, serialize: true ) 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, serialize: serialize, ) end |
#map_all(to: nil, render_nil: false, render_default: false, with: {}, delegate: nil) ⇒ Object Also known as: map_all_content
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/lutaml/key_value/mapping.rb', line 110 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
132 133 134 135 136 |
# File 'lib/lutaml/key_value/mapping.rb', line 132 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
138 139 140 141 |
# File 'lib/lutaml/key_value/mapping.rb', line 138 def map_key(to_instance: nil, as_attribute: nil) @key_mapping = { "#{to_instance || as_attribute}": :key } map_to_instance end |
#map_to_instance ⇒ Object
152 153 154 155 156 157 |
# File 'lib/lutaml/key_value/mapping.rb', line 152 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
143 144 145 146 |
# File 'lib/lutaml/key_value/mapping.rb', line 143 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
165 166 167 168 |
# File 'lib/lutaml/key_value/mapping.rb', line 165 def mappings(register_id = nil) ensure_mappings_imported!(register_id) if finalized? mappings_hash(register_id).values end |
#mappings_hash(register_id = nil) ⇒ Object
170 171 172 173 174 175 176 |
# File 'lib/lutaml/key_value/mapping.rb', line 170 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
159 160 161 162 163 |
# File 'lib/lutaml/key_value/mapping.rb', line 159 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).
51 52 53 |
# File 'lib/lutaml/key_value/mapping.rb', line 51 def no_key? @key.nil? end |
#no_root ⇒ Object
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
Use #no_key? instead.
56 57 58 |
# File 'lib/lutaml/key_value/mapping.rb', line 56 def no_root? no_key? end |
#polymorphic_mapping ⇒ Object
277 278 279 |
# File 'lib/lutaml/key_value/mapping.rb', line 277 def polymorphic_mapping mappings.find(&:polymorphic_mapping?) end |
#root(name = nil) ⇒ Object
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_mapping ⇒ Object
281 282 283 |
# File 'lib/lutaml/key_value/mapping.rb', line 281 def root_mapping mappings.find(&:root_mapping?) end |
#root_name ⇒ Object
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
194 195 196 197 198 199 200 201 |
# File 'lib/lutaml/key_value/mapping.rb', line 194 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
231 232 233 234 235 236 237 238 |
# File 'lib/lutaml/key_value/mapping.rb', line 231 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
240 241 242 243 244 245 |
# File 'lib/lutaml/key_value/mapping.rb', line 240 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
223 224 225 226 227 228 229 |
# File 'lib/lutaml/key_value/mapping.rb', line 223 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
203 204 205 206 207 208 209 210 211 |
# File 'lib/lutaml/key_value/mapping.rb', line 203 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 (to, key, with) end |
#validate_with_options!(to, key, with) ⇒ Object
213 214 215 216 217 218 219 220 221 |
# File 'lib/lutaml/key_value/mapping.rb', line 213 def (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 |