Class: Lutaml::Model::Xml::Mapping
- Defined in:
- lib/lutaml/model/xml/mapping.rb
Constant Summary collapse
- TYPES =
{ attribute: :map_attribute, element: :map_element, content: :map_content, all_content: :map_all, }.freeze
Instance Attribute Summary collapse
-
#element_sequence ⇒ Object
readonly
Returns the value of attribute element_sequence.
-
#mixed_content ⇒ Object
(also: #mixed_content?)
readonly
Returns the value of attribute mixed_content.
-
#namespace_prefix ⇒ Object
readonly
Returns the value of attribute namespace_prefix.
-
#namespace_uri ⇒ Object
readonly
Returns the value of attribute namespace_uri.
-
#ordered ⇒ Object
(also: #ordered?)
readonly
Returns the value of attribute ordered.
-
#root_element ⇒ Object
readonly
Returns the value of attribute root_element.
Instance Method Summary collapse
- #attribute(name) ⇒ Object
- #attributes ⇒ Object
- #attributes_to_dup ⇒ Object
- #content_mapping ⇒ Object
- #deep_dup ⇒ Object
- #dup_mappings(mappings) ⇒ Object
- #element(name) ⇒ Object
- #elements ⇒ Object
- #finalize(mapper_class) ⇒ Object
- #find_by_name(name) ⇒ Object
- #import_model_mappings(model) ⇒ Object
-
#initialize ⇒ Mapping
constructor
A new instance of Mapping.
- #map_all(to:, render_nil: false, render_default: false, delegate: nil, with: {}, namespace: (namespace_set = false nil), prefix: (prefix_set = false nil), render_empty: false) ⇒ Object (also: #map_all_content)
- #map_attribute(name, to: nil, render_nil: false, render_default: false, render_empty: false, with: {}, delegate: nil, polymorphic_map: {}, namespace: (namespace_set = false nil), prefix: (prefix_set = false nil), transform: {}, value_map: {}) ⇒ Object
-
#map_content(to: nil, render_nil: false, render_default: false, render_empty: false, with: {}, delegate: nil, mixed: false, cdata: false, transform: {}, value_map: {}) ⇒ Object
rubocop:enable Metrics/ParameterLists.
-
#map_element(name, to: nil, render_nil: false, render_default: false, render_empty: false, treat_nil: :nil, treat_empty: :empty, treat_omitted: :nil, with: {}, delegate: nil, cdata: false, polymorphic: {}, namespace: (namespace_set = false nil), prefix: (prefix_set = false nil), transform: {}, value_map: {}) ⇒ Object
rubocop:disable Metrics/ParameterLists.
- #mapping_attributes_hash ⇒ Object
- #mapping_elements_hash ⇒ Object
- #mappings ⇒ Object
- #merge_elements_sequence(mapping) ⇒ Object
- #merge_mapping_attributes(mapping) ⇒ Object
- #merge_mapping_elements(mapping) ⇒ Object
- #namespace(uri, prefix = nil) ⇒ Object
- #no_root ⇒ Object
- #no_root? ⇒ Boolean
- #polymorphic_mapping ⇒ Object
- #prefixed_root ⇒ Object
- #raw_mapping ⇒ Object
- #root(name, mixed: false, ordered: false) ⇒ Object
- #root? ⇒ Boolean
- #sequence(&block) ⇒ Object
- #validate!(key, to, with, render_nil, render_empty, type: nil) ⇒ Object
- #validate_raw_mappings!(type) ⇒ Object
- #validate_to_and_with_arguments!(key, to, with) ⇒ Object
- #validate_with_options!(key, to, with) ⇒ Object
Constructor Details
#initialize ⇒ Mapping
Returns a new instance of Mapping.
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/lutaml/model/xml/mapping.rb', line 22 def initialize super @elements = {} @attributes = {} @element_sequence = [] @content_mapping = nil @raw_mapping = nil @mixed_content = false @format = :xml end |
Instance Attribute Details
#element_sequence ⇒ Object (readonly)
Returns the value of attribute element_sequence.
15 16 17 |
# File 'lib/lutaml/model/xml/mapping.rb', line 15 def element_sequence @element_sequence end |
#mixed_content ⇒ Object (readonly) Also known as: mixed_content?
Returns the value of attribute mixed_content.
15 16 17 |
# File 'lib/lutaml/model/xml/mapping.rb', line 15 def mixed_content @mixed_content end |
#namespace_prefix ⇒ Object (readonly)
Returns the value of attribute namespace_prefix.
15 16 17 |
# File 'lib/lutaml/model/xml/mapping.rb', line 15 def namespace_prefix @namespace_prefix end |
#namespace_uri ⇒ Object (readonly)
Returns the value of attribute namespace_uri.
15 16 17 |
# File 'lib/lutaml/model/xml/mapping.rb', line 15 def namespace_uri @namespace_uri end |
#ordered ⇒ Object (readonly) Also known as: ordered?
Returns the value of attribute ordered.
15 16 17 |
# File 'lib/lutaml/model/xml/mapping.rb', line 15 def ordered @ordered end |
#root_element ⇒ Object (readonly)
Returns the value of attribute root_element.
15 16 17 |
# File 'lib/lutaml/model/xml/mapping.rb', line 15 def root_element @root_element end |
Instance Method Details
#attribute(name) ⇒ Object
329 330 331 332 333 |
# File 'lib/lutaml/model/xml/mapping.rb', line 329 def attribute(name) attributes.detect do |rule| name == rule.to end end |
#attributes ⇒ Object
307 308 309 |
# File 'lib/lutaml/model/xml/mapping.rb', line 307 def attributes @attributes.values end |
#attributes_to_dup ⇒ Object
388 389 390 391 392 393 394 395 396 |
# File 'lib/lutaml/model/xml/mapping.rb', line 388 def attributes_to_dup @attributes_to_dup ||= %i[ @content_mapping @raw_mapping @element_sequence @attributes @elements ] end |
#content_mapping ⇒ Object
311 312 313 |
# File 'lib/lutaml/model/xml/mapping.rb', line 311 def content_mapping @content_mapping end |
#deep_dup ⇒ Object
371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/lutaml/model/xml/mapping.rb', line 371 def deep_dup self.class.new.tap do |xml_mapping| xml_mapping.root(@root_element.dup, mixed: @mixed_content, ordered: @ordered) xml_mapping.namespace(@namespace_uri.dup, @namespace_prefix.dup) attributes_to_dup.each do |var_name| value = instance_variable_get(var_name) xml_mapping.instance_variable_set(var_name, Utils.deep_dup(value)) end end end |
#dup_mappings(mappings) ⇒ Object
398 399 400 401 402 403 404 405 406 |
# File 'lib/lutaml/model/xml/mapping.rb', line 398 def dup_mappings(mappings) new_mappings = {} mappings.each do |key, mapping_rule| new_mappings[key] = mapping_rule.deep_dup end new_mappings end |
#element(name) ⇒ Object
323 324 325 326 327 |
# File 'lib/lutaml/model/xml/mapping.rb', line 323 def element(name) elements.detect do |rule| name == rule.to end end |
#elements ⇒ Object
303 304 305 |
# File 'lib/lutaml/model/xml/mapping.rb', line 303 def elements @elements.values end |
#finalize(mapper_class) ⇒ Object
34 35 36 37 38 |
# File 'lib/lutaml/model/xml/mapping.rb', line 34 def finalize(mapper_class) if !root_element && !no_root? root(mapper_class.model.to_s) end end |
#find_by_name(name) ⇒ Object
335 336 337 338 339 340 341 342 343 |
# File 'lib/lutaml/model/xml/mapping.rb', line 335 def find_by_name(name) if ["text", "#cdata-section"].include?(name.to_s) content_mapping else mappings.detect do |rule| rule.name == name.to_s || rule.name == name.to_sym end end end |
#import_model_mappings(model) ⇒ Object
252 253 254 255 256 257 258 259 |
# File 'lib/lutaml/model/xml/mapping.rb', line 252 def import_model_mappings(model) raise Lutaml::Model::ImportModelWithRootError.new(model) if model.root? mappings = model.mappings_for(:xml) @elements.merge!(mappings.instance_variable_get(:@elements)) @attributes.merge!(mappings.instance_variable_get(:@attributes)) (@element_sequence << mappings.element_sequence).flatten! end |
#map_all(to:, render_nil: false, render_default: false, delegate: nil, with: {}, namespace: (namespace_set = false nil), prefix: (prefix_set = false nil), render_empty: false) ⇒ Object Also known as: map_all_content
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/lutaml/model/xml/mapping.rb', line 206 def map_all( to:, render_nil: false, render_default: false, delegate: nil, with: {}, namespace: (namespace_set = false nil), prefix: (prefix_set = false nil), render_empty: false ) validate!( Constants::RAW_MAPPING_KEY, to, with, render_nil, render_empty, type: TYPES[:all_content], ) rule = MappingRule.new( Constants::RAW_MAPPING_KEY, to: to, render_nil: render_nil, render_default: render_default, with: with, delegate: delegate, namespace: namespace, prefix: prefix, default_namespace: namespace_uri, namespace_set: namespace_set != false, prefix_set: prefix_set != false, ) @raw_mapping = rule end |
#map_attribute(name, to: nil, render_nil: false, render_default: false, render_empty: false, with: {}, delegate: nil, polymorphic_map: {}, namespace: (namespace_set = false nil), prefix: (prefix_set = false nil), transform: {}, value_map: {}) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/lutaml/model/xml/mapping.rb', line 125 def map_attribute( name, to: nil, render_nil: false, render_default: false, render_empty: false, with: {}, delegate: nil, polymorphic_map: {}, namespace: (namespace_set = false nil), prefix: (prefix_set = false nil), transform: {}, value_map: {} ) validate!( name, to, with, render_nil, render_empty, type: TYPES[:attribute] ) if name == "schemaLocation" Logger.warn_auto_handling( name: name, caller_file: File.basename(caller_locations(1, 1)[0].path), caller_line: caller_locations(1, 1)[0].lineno, ) end rule = MappingRule.new( name, to: to, render_nil: render_nil, render_default: render_default, with: with, delegate: delegate, namespace: namespace, prefix: prefix, attribute: true, polymorphic_map: polymorphic_map, default_namespace: namespace_uri, namespace_set: namespace_set != false, prefix_set: prefix_set != false, transform: transform, value_map: value_map, ) @attributes[rule.namespaced_name] = rule end |
#map_content(to: nil, render_nil: false, render_default: false, render_empty: false, with: {}, delegate: nil, mixed: false, cdata: false, transform: {}, value_map: {}) ⇒ Object
rubocop:enable Metrics/ParameterLists
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/lutaml/model/xml/mapping.rb', line 175 def map_content( to: nil, render_nil: false, render_default: false, render_empty: false, with: {}, delegate: nil, mixed: false, cdata: false, transform: {}, value_map: {} ) validate!( "content", to, with, render_nil, render_empty, type: TYPES[:content] ) @content_mapping = MappingRule.new( nil, to: to, render_nil: render_nil, render_default: render_default, render_empty: render_empty, with: with, delegate: delegate, mixed_content: mixed, cdata: cdata, transform: transform, value_map: value_map, ) end |
#map_element(name, to: nil, render_nil: false, render_default: false, render_empty: false, treat_nil: :nil, treat_empty: :empty, treat_omitted: :nil, with: {}, delegate: nil, cdata: false, polymorphic: {}, namespace: (namespace_set = false nil), prefix: (prefix_set = false nil), transform: {}, value_map: {}) ⇒ Object
rubocop:disable Metrics/ParameterLists
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/lutaml/model/xml/mapping.rb', line 77 def map_element( name, to: nil, render_nil: false, render_default: false, render_empty: false, treat_nil: :nil, treat_empty: :empty, treat_omitted: :nil, with: {}, delegate: nil, cdata: false, polymorphic: {}, namespace: (namespace_set = false nil), prefix: (prefix_set = false nil), transform: {}, value_map: {} ) validate!( name, to, with, render_nil, render_empty, type: TYPES[:element] ) rule = MappingRule.new( 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, cdata: cdata, namespace: namespace, default_namespace: namespace_uri, prefix: prefix, polymorphic: polymorphic, namespace_set: namespace_set != false, prefix_set: prefix_set != false, transform: transform, value_map: value_map, ) @elements[rule.namespaced_name] = rule end |
#mapping_attributes_hash ⇒ Object
345 346 347 |
# File 'lib/lutaml/model/xml/mapping.rb', line 345 def mapping_attributes_hash @attributes end |
#mapping_elements_hash ⇒ Object
349 350 351 |
# File 'lib/lutaml/model/xml/mapping.rb', line 349 def mapping_elements_hash @elements end |
#mappings ⇒ Object
319 320 321 |
# File 'lib/lutaml/model/xml/mapping.rb', line 319 def mappings elements + attributes + [content_mapping, raw_mapping].compact end |
#merge_elements_sequence(mapping) ⇒ Object
361 362 363 364 365 366 367 368 369 |
# File 'lib/lutaml/model/xml/mapping.rb', line 361 def merge_elements_sequence(mapping) mapping.element_sequence.each do |sequence| element_sequence << Lutaml::Model::Sequence.new(self).tap do |instance| sequence.attributes.each do |attr| instance.attributes << attr.deep_dup end end end end |
#merge_mapping_attributes(mapping) ⇒ Object
353 354 355 |
# File 'lib/lutaml/model/xml/mapping.rb', line 353 def merge_mapping_attributes(mapping) mapping_attributes_hash.merge!(mapping.mapping_attributes_hash) end |
#merge_mapping_elements(mapping) ⇒ Object
357 358 359 |
# File 'lib/lutaml/model/xml/mapping.rb', line 357 def merge_mapping_elements(mapping) mapping_elements_hash.merge!(mapping.mapping_elements_hash) end |
#namespace(uri, prefix = nil) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/lutaml/model/xml/mapping.rb', line 69 def namespace(uri, prefix = nil) raise Lutaml::Model::NoRootNamespaceError if no_root? @namespace_uri = uri @namespace_prefix = prefix end |
#no_root ⇒ Object
53 54 55 |
# File 'lib/lutaml/model/xml/mapping.rb', line 53 def no_root @no_root = true end |
#no_root? ⇒ Boolean
57 58 59 |
# File 'lib/lutaml/model/xml/mapping.rb', line 57 def no_root? !!@no_root end |
#polymorphic_mapping ⇒ Object
384 385 386 |
# File 'lib/lutaml/model/xml/mapping.rb', line 384 def polymorphic_mapping mappings.find(&:polymorphic_mapping?) end |
#prefixed_root ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/lutaml/model/xml/mapping.rb', line 61 def prefixed_root if namespace_uri && namespace_prefix "#{namespace_prefix}:#{root_element}" else root_element end end |
#raw_mapping ⇒ Object
315 316 317 |
# File 'lib/lutaml/model/xml/mapping.rb', line 315 def raw_mapping @raw_mapping end |
#root(name, mixed: false, ordered: false) ⇒ Object
43 44 45 46 47 |
# File 'lib/lutaml/model/xml/mapping.rb', line 43 def root(name, mixed: false, ordered: false) @root_element = name @mixed_content = mixed @ordered = ordered || mixed # mixed contenet will always be ordered end |
#root? ⇒ Boolean
49 50 51 |
# File 'lib/lutaml/model/xml/mapping.rb', line 49 def root? !!root_element end |
#sequence(&block) ⇒ Object
246 247 248 249 250 |
# File 'lib/lutaml/model/xml/mapping.rb', line 246 def sequence(&block) @element_sequence << Sequence.new(self).tap do |s| s.instance_eval(&block) end end |
#validate!(key, to, with, render_nil, render_empty, type: nil) ⇒ Object
261 262 263 264 265 266 267 268 269 270 |
# File 'lib/lutaml/model/xml/mapping.rb', line 261 def validate!(key, to, with, render_nil, render_empty, type: nil) validate_raw_mappings!(type) validate_to_and_with_arguments!(key, to, with) if render_nil == :as_empty || render_empty == :as_empty raise IncorrectMappingArgumentsError.new( ":as_empty is not supported for XML mappings", ) end end |
#validate_raw_mappings!(type) ⇒ Object
292 293 294 295 296 297 298 299 300 301 |
# File 'lib/lutaml/model/xml/mapping.rb', line 292 def validate_raw_mappings!(type) if !@raw_mapping.nil? && type != TYPES[:attribute] raise StandardError, "#{type} is not allowed, only #{TYPES[:attribute]} " \ "is allowed with #{TYPES[:all_content]}" end if !(elements.empty? && content_mapping.nil?) && type == TYPES[:all_content] raise StandardError, "#{TYPES[:all_content]} is not allowed with other mappings" end end |
#validate_to_and_with_arguments!(key, to, with) ⇒ Object
272 273 274 275 276 277 278 279 280 |
# File 'lib/lutaml/model/xml/mapping.rb', line 272 def validate_to_and_with_arguments!(key, to, with) if to.nil? && with.empty? raise IncorrectMappingArgumentsError.new( ":to or :with argument is required for mapping '#{key}'", ) end (key, to, with) end |
#validate_with_options!(key, to, with) ⇒ Object
282 283 284 285 286 287 288 289 290 |
# File 'lib/lutaml/model/xml/mapping.rb', line 282 def (key, to, with) return true if to if !with.empty? && (with[:from].nil? || with[:to].nil?) raise IncorrectMappingArgumentsError.new( ":with argument for mapping '#{key}' requires :to and :from keys", ) end end |