Class: Lutaml::Model::KeyValueMapping
- Defined in:
- lib/lutaml/model/mapping/key_value_mapping.rb
Direct Known Subclasses
HashAdapter::Mapping, Json::Mapping, Toml::Mapping, Yaml::Mapping
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#mappings ⇒ Object
readonly
Returns the value of attribute mappings.
Instance Method Summary collapse
- #deep_dup ⇒ Object
- #duplicate_mappings ⇒ Object
- #find_by_to(to) ⇒ Object
-
#initialize(format = nil) ⇒ KeyValueMapping
constructor
A new instance of KeyValueMapping.
- #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: #map_element)
- #map_all(to: nil, render_nil: false, render_default: false, with: {}, delegate: nil) ⇒ Object (also: #map_all_content)
- #name_for_mapping(root_mappings, name) ⇒ Object
- #polymorphic_mapping ⇒ Object
- #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) ⇒ KeyValueMapping
Returns a new instance of KeyValueMapping.
9 10 11 12 13 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 9 def initialize(format = nil) super() @format = format end |
Instance Attribute Details
#format ⇒ Object (readonly)
Returns the value of attribute format.
7 8 9 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 7 def format @format end |
#mappings ⇒ Object (readonly)
Returns the value of attribute mappings.
7 8 9 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 7 def mappings @mappings end |
Instance Method Details
#deep_dup ⇒ Object
134 135 136 137 138 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 134 def deep_dup self.class.new(@format).tap do |new_mapping| new_mapping.instance_variable_set(:@mappings, duplicate_mappings) end end |
#duplicate_mappings ⇒ Object
140 141 142 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 140 def duplicate_mappings @mappings.map(&:deep_dup) end |
#find_by_to(to) ⇒ Object
144 145 146 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 144 def find_by_to(to) @mappings.find { |m| m.to.to_s == to.to_s } 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
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 15 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 << KeyValueMappingRule.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
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 58 def map_all( to: nil, render_nil: false, render_default: false, with: {}, delegate: nil ) @raw_mapping = true validate!(Constants::RAW_MAPPING_KEY, to, with, render_nil, nil) @mappings << KeyValueMappingRule.new( Constants::RAW_MAPPING_KEY, to: to, render_nil: render_nil, render_default: render_default, with: with, delegate: delegate, ) end |
#name_for_mapping(root_mappings, name) ⇒ Object
79 80 81 82 83 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 79 def name_for_mapping(root_mappings, name) return "root_mapping" if root_mappings name end |
#polymorphic_mapping ⇒ Object
148 149 150 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 148 def polymorphic_mapping @mappings.find(&:polymorphic_mapping?) end |
#validate!(key, to, with, render_nil, render_empty) ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 85 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
120 121 122 123 124 125 126 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 120 def validate_blank_mappings!(render_nil, render_empty) if render_nil == :as_blank || render_empty == :as_blank raise IncorrectMappingArgumentsError.new( ":as_blank is not supported for key-value mappings", ) end end |
#validate_mappings!(_type) ⇒ Object
128 129 130 131 132 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 128 def validate_mappings!(_type) if (@raw_mapping && 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
114 115 116 117 118 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 114 def validate_root_mappings!(name) if @mappings.any?(&:root_mapping?) || (name == "root_mapping" && @mappings.any?) raise MultipleMappingsError.new("root_mappings cannot be used with other mappings") end end |
#validate_to_and_with_arguments!(key, to, with) ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 94 def validate_to_and_with_arguments!(key, to, with) if to.nil? && with.empty? && !@raw_mapping raise IncorrectMappingArgumentsError.new( ":to or :with argument is required for mapping '#{key}'", ) end (to, key, with) end |
#validate_with_options!(to, key, with) ⇒ Object
104 105 106 107 108 109 110 111 112 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 104 def (to, key, with) return true if to if !with.empty? && (with[:from].nil? || with[:to].nil?) && !@raw_mapping raise IncorrectMappingArgumentsError.new( ":with argument for mapping '#{key}' requires :to and :from keys", ) end end |