Class: Lutaml::Model::KeyValueMapping
- Defined in:
- lib/lutaml/model/mapping/key_value_mapping.rb
Direct Known Subclasses
HashAdapter::Mapping, Json::Mapping, Jsonl::Mapping, Toml::Mapping, Yaml::Mapping, Yamls::Mapping
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#key_mappings ⇒ Object
readonly
Returns the value of attribute key_mappings.
-
#mappings ⇒ Object
readonly
Returns the value of attribute mappings.
-
#value_mappings ⇒ Object
readonly
Returns the value of attribute value_mappings.
Instance Method Summary collapse
- #deep_dup ⇒ Object
- #duplicate_mappings ⇒ Object
- #find_by_name(name) ⇒ Object
- #find_by_to(to) ⇒ Object
-
#initialize(format = nil) ⇒ KeyValueMapping
constructor
A new instance of KeyValueMapping.
- #key_value_mappings ⇒ Object
- #key_value_mappings? ⇒ Boolean
- #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)
- #map_instances(to:) ⇒ Object
- #map_key(to_instance: nil, as_attribute: nil) ⇒ Object
- #map_value(to_instance: nil, as_attribute: nil) ⇒ Object
- #name_for_mapping(root_mappings, name) ⇒ Object
- #no_root ⇒ Object
- #no_root? ⇒ Boolean
- #polymorphic_mapping ⇒ Object
- #root(name = nil) ⇒ Object
- #root_mapping ⇒ Object
- #root_name ⇒ 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 |
#key_mappings ⇒ Object (readonly)
Returns the value of attribute key_mappings.
7 8 9 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 7 def key_mappings @key_mappings 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 |
#value_mappings ⇒ Object (readonly)
Returns the value of attribute value_mappings.
7 8 9 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 7 def value_mappings @value_mappings end |
Instance Method Details
#deep_dup ⇒ Object
186 187 188 189 190 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 186 def deep_dup self.class.new(@format).tap do |new_mapping| new_mapping.instance_variable_set(:@mappings, duplicate_mappings) end end |
#duplicate_mappings ⇒ Object
192 193 194 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 192 def duplicate_mappings @mappings.map(&:deep_dup) end |
#find_by_name(name) ⇒ Object
200 201 202 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 200 def find_by_name(name) @mappings.find { |m| m.name.to_s == name.to_s } end |
#find_by_to(to) ⇒ Object
196 197 198 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 196 def find_by_to(to) @mappings.find { |m| m.to.to_s == to.to_s } end |
#key_value_mappings ⇒ Object
117 118 119 120 121 122 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 117 def key_value_mappings { key: @key_mappings, value: @value_mappings, }.compact end |
#key_value_mappings? ⇒ Boolean
124 125 126 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 124 def key_value_mappings? Utils.present?(@key_mappings) || Utils.present?(@value_mappings) 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
31 32 33 34 35 36 37 38 39 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 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 31 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
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 74 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 |
#map_instances(to:) ⇒ Object
95 96 97 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 95 def map_instances(to:) map(root_name || to, to: to) end |
#map_key(to_instance: nil, as_attribute: nil) ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 99 def map_key(to_instance: nil, as_attribute: nil) @key_mappings = KeyValueMappingRule.new( Constants::KEY_MAPPING_KEY, to: nil, to_instance: to_instance, as_attribute: as_attribute, ) end |
#map_value(to_instance: nil, as_attribute: nil) ⇒ Object
108 109 110 111 112 113 114 115 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 108 def map_value(to_instance: nil, as_attribute: nil) @value_mappings = KeyValueMappingRule.new( Constants::VALUE_MAPPING_KEY, to: nil, to_instance: to_instance, as_attribute: as_attribute, ) end |
#name_for_mapping(root_mappings, name) ⇒ Object
128 129 130 131 132 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 128 def name_for_mapping(root_mappings, name) return "root_mapping" if root_mappings name end |
#no_root ⇒ Object
19 20 21 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 19 def no_root @root = nil end |
#no_root? ⇒ Boolean
23 24 25 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 23 def no_root? @root.nil? end |
#polymorphic_mapping ⇒ Object
204 205 206 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 204 def polymorphic_mapping @mappings.find(&:polymorphic_mapping?) end |
#root(name = nil) ⇒ Object
15 16 17 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 15 def root(name = nil) @root = name end |
#root_mapping ⇒ Object
208 209 210 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 208 def root_mapping @mappings.find(&:root_mapping?) end |
#root_name ⇒ Object
27 28 29 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 27 def root_name @root end |
#validate!(key, to, with, render_nil, render_empty) ⇒ Object
134 135 136 137 138 139 140 141 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 134 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
171 172 173 174 175 176 177 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 171 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
179 180 181 182 183 184 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 179 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
163 164 165 166 167 168 169 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 163 def validate_root_mappings!(name) if 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
143 144 145 146 147 148 149 150 151 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 143 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
153 154 155 156 157 158 159 160 161 |
# File 'lib/lutaml/model/mapping/key_value_mapping.rb', line 153 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 |