Class: Lutaml::Model::MappingRule
- Inherits:
-
Object
- Object
- Lutaml::Model::MappingRule
- Includes:
- DeepDupable
- Defined in:
- lib/lutaml/model/mapping/mapping_rule.rb
Direct Known Subclasses
Constant Summary collapse
- ALLOWED_OPTIONS =
{ render_nil: %i[ omit as_nil as_blank as_empty ], render_empty: %i[ omit as_empty as_blank as_nil ], }.freeze
- EMPTY_RENDER_OPTIONS =
Frozen default: render? only reads options (render_nil?/render_empty?/ render_omitted? policies); a fresh {} per call is pure churn on the serialization hot path.
{}.freeze
- EMPTY_TRANSFORMERS =
Frozen empty array for the common case of no transformers
[].freeze
- TRANSFORM_DISPATCH =
Verdict for handle_transform_method's dispatch, computed once per (rule, attribute) — both are static after definition, but rules may be frozen, so the memo lives at class level keyed on identity. :assign — no transformer runs on deserialize (fast path); :import — hash/proc transformers go through ImportTransformer.
{}.compare_by_identity
- PARSED_ASSIGN_WRITERS =
[model class, target] -> the parsed-assign writer compiled for that class's attribute, or nil when only a casting writer exists (custom writers, reflective names, enum shorthands). Class-level because mapping rules may be frozen (see the name-string note above TRANSFORM_DISPATCH). Concurrent::Map under threaded MRI, plain identity Hash under Opal — writes are idempotent (the same verdict is recomputed).
if Lutaml::Model.opal? {}.compare_by_identity else Lutaml::Model::RuntimeCompatibility .require_native("concurrent") Concurrent::Map.new end
Instance Attribute Summary collapse
-
#as_attribute ⇒ Object
readonly
Returns the value of attribute as_attribute.
-
#attribute ⇒ Object
(also: #attribute?)
readonly
Returns the value of attribute attribute.
-
#custom_methods ⇒ Object
readonly
Returns the value of attribute custom_methods.
-
#delegate ⇒ Object
readonly
Returns the value of attribute delegate.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#name ⇒ Object
(also: #from)
readonly
Returns the value of attribute name.
-
#polymorphic ⇒ Object
readonly
Returns the value of attribute polymorphic.
-
#polymorphic_map ⇒ Object
readonly
Returns the value of attribute polymorphic_map.
-
#render_default ⇒ Object
(also: #render_default?)
readonly
Returns the value of attribute render_default.
-
#render_empty ⇒ Object
readonly
Returns the value of attribute render_empty.
-
#render_nil ⇒ Object
readonly
Returns the value of attribute render_nil.
-
#to ⇒ Object
readonly
Returns the value of attribute to.
-
#to_instance ⇒ Object
readonly
Returns the value of attribute to_instance.
-
#transform ⇒ Object
readonly
Returns the value of attribute transform.
-
#treat_empty ⇒ Object
readonly
Returns the value of attribute treat_empty.
-
#treat_nil ⇒ Object
readonly
Returns the value of attribute treat_nil.
-
#treat_omitted ⇒ Object
readonly
Returns the value of attribute treat_omitted.
Class Method Summary collapse
Instance Method Summary collapse
- #can_transform_to?(attribute, format) ⇒ Boolean
- #custom_method_only? ⇒ Boolean
- #deep_dup ⇒ Object
- #default_value_map(options = {}) ⇒ Object
- #deserialize(model, value, attributes, mapper_class = nil, context = nil, pre_cast: false) ⇒ Object
- #eql?(other) ⇒ Boolean (also: #==)
- #get_transformers(attribute) ⇒ Object
- #has_custom_method_for_deserialization? ⇒ Boolean
- #has_custom_method_for_serialization? ⇒ Boolean
-
#has_items?(value) ⇒ Boolean
Check if value is a non-empty collection.
-
#initialize(name, to:, to_instance: nil, as_attribute: nil, render_nil: false, render_default: false, render_empty: false, treat_nil: :nil, treat_empty: :empty, treat_omitted: :nil, with: {}, attribute: false, delegate: nil, root_mappings: nil, polymorphic: {}, polymorphic_map: {}, transform: {}, value_map: {}, serialize: true, when_attribute: {}) ⇒ MappingRule
constructor
A new instance of MappingRule.
- #multiple_mappings? ⇒ Boolean
- #mutated_collection?(value, instance) ⇒ Boolean
-
#name_strings ⇒ Object
Normalized name spellings for matching, computed once.
- #polymorphic_mapping? ⇒ Boolean
- #raw_mapping? ⇒ Boolean
-
#raw_value_map ⇒ Object
Raw value_map hash access (for compiled rules).
- #render?(value, instance = nil, options = EMPTY_RENDER_OPTIONS) ⇒ Boolean
- #render_as(key, default_value, options = {}) ⇒ Object
- #render_empty?(options = {}) ⇒ Boolean
- #render_nil?(options = {}) ⇒ Boolean
- #render_omitted?(options = {}) ⇒ Boolean
- #render_value_for(value) ⇒ Object
- #serialize(model, parent = nil, doc = nil, context = nil) ⇒ Object
-
#serialize? ⇒ Boolean
Whether this rule participates in serialization output.
- #to_value_for(model) ⇒ Object
- #transform_value(attribute, value, read_method, format) ⇒ Object
- #treat?(value) ⇒ Boolean
- #treat_as(key, default_value, options = {}) ⇒ Object
- #treat_empty?(options = {}) ⇒ Boolean
- #treat_nil?(options = {}) ⇒ Boolean
- #treat_omitted?(options = {}) ⇒ Boolean
- #value_for_option(option, empty_value = nil) ⇒ Object
- #value_map(key, options = {}) ⇒ Object
-
#when_attribute ⇒ Object
Static per rule: placeholder
__name__targets and inferred custom-method targets without an attribute type never change — the applier asked this per element and paid two to_s strings each time (240k allocations on the ISO-13849 serialize profile). - #when_attribute? ⇒ Boolean
Constructor Details
#initialize(name, to:, to_instance: nil, as_attribute: nil, render_nil: false, render_default: false, render_empty: false, treat_nil: :nil, treat_empty: :empty, treat_omitted: :nil, with: {}, attribute: false, delegate: nil, root_mappings: nil, polymorphic: {}, polymorphic_map: {}, transform: {}, value_map: {}, serialize: true, when_attribute: {}) ⇒ MappingRule
Returns a new instance of MappingRule.
55 56 57 58 59 60 61 62 63 64 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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 55 def initialize( name, to:, to_instance: nil, as_attribute: nil, render_nil: false, render_default: false, render_empty: false, treat_nil: :nil, treat_empty: :empty, treat_omitted: :nil, with: {}, attribute: false, delegate: nil, root_mappings: nil, polymorphic: {}, polymorphic_map: {}, transform: {}, value_map: {}, serialize: true, when_attribute: {} ) @name = name @to = to # Dynamic symbol built per call allocated ~10% of parse objects on # attribute-heavy documents; `to` is immutable, so build it once. @setter_name = :"#{to}=" @to_instance = to_instance @as_attribute = as_attribute @render_nil = render_nil @render_default = render_default @render_empty = render_empty @treat_nil = treat_nil @treat_empty = treat_empty @treat_omitted = treat_omitted @custom_methods = with @attribute = attribute @delegate = delegate @root_mappings = root_mappings @polymorphic = polymorphic @polymorphic_map = polymorphic_map @transform = transform @serialize_mapping = serialize # Attribute-value discriminator (lutaml-model#88): select this # rule's wire occurrences by a sibling attribute value, e.g. # `when_attribute: { "type" => "guidance" }`. @when_attribute = (when_attribute || {}).freeze # Cache whether this rule needs the full deserialize chain. # Over 95% of rules are "simple" (no custom method, no delegate). @needs_full_deserialize = has_custom_method_for_deserialization? || !!delegate # Only calculate default_value_map if value_map is not fully provided if value_map.empty? || !value_map[:from] || !value_map[:to] # Build value_map by starting with defaults from render_nil/render_empty, # then overlaying user-provided value_map entries on top. # User-provided value_map entries take PRECEDENCE over computed defaults. # This ensures that value_map: { to: { empty: :empty } } overrides # the default render_empty: false → :omitted behavior. vm = { from: (value_map[:from] || {}).dup, to: (value_map[:to] || {}).dup, } defaults = default_value_map vm[:from] = defaults[:from].merge(vm[:from]) vm[:to] = defaults[:to].merge(vm[:to]) @value_map = vm else # Complete value_map provided (e.g., from deep_dup), use it directly. @value_map = value_map end # Freeze value_map and its inner hashes — they are never mutated after # construction, and downstream code reads them on every serialization. @value_map[:from].freeze @value_map[:to].freeze @value_map.freeze # The :from treatment flags are read on every rule application of # every parse; they are static once @value_map is final. The three # Hash#[] chains per value used to cost 1.4 M value_map calls per # ISO-13849 parse. from_map = @value_map[:from] @treat_nil_flag = from_map[:nil] != :omitted @treat_empty_flag = from_map[:empty] != :omitted @treat_omitted_flag = from_map[:omitted] != :omitted end |
Instance Attribute Details
#as_attribute ⇒ Object (readonly)
Returns the value of attribute as_attribute.
6 7 8 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 6 def as_attribute @as_attribute end |
#attribute ⇒ Object (readonly) Also known as: attribute?
Returns the value of attribute attribute.
6 7 8 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 6 def attribute @attribute end |
#custom_methods ⇒ Object (readonly)
Returns the value of attribute custom_methods.
6 7 8 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 6 def custom_methods @custom_methods end |
#delegate ⇒ Object (readonly)
Returns the value of attribute delegate.
6 7 8 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 6 def delegate @delegate end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
6 7 8 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 6 def format @format end |
#name ⇒ Object (readonly) Also known as: from
Returns the value of attribute name.
6 7 8 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 6 def name @name end |
#polymorphic ⇒ Object (readonly)
Returns the value of attribute polymorphic.
6 7 8 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 6 def polymorphic @polymorphic end |
#polymorphic_map ⇒ Object (readonly)
Returns the value of attribute polymorphic_map.
6 7 8 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 6 def polymorphic_map @polymorphic_map end |
#render_default ⇒ Object (readonly) Also known as: render_default?
Returns the value of attribute render_default.
6 7 8 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 6 def render_default @render_default end |
#render_empty ⇒ Object (readonly)
Returns the value of attribute render_empty.
6 7 8 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 6 def render_empty @render_empty end |
#render_nil ⇒ Object (readonly)
Returns the value of attribute render_nil.
6 7 8 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 6 def render_nil @render_nil end |
#to ⇒ Object (readonly)
Returns the value of attribute to.
6 7 8 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 6 def to @to end |
#to_instance ⇒ Object (readonly)
Returns the value of attribute to_instance.
6 7 8 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 6 def to_instance @to_instance end |
#transform ⇒ Object (readonly)
Returns the value of attribute transform.
6 7 8 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 6 def transform @transform end |
#treat_empty ⇒ Object (readonly)
Returns the value of attribute treat_empty.
6 7 8 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 6 def treat_empty @treat_empty end |
#treat_nil ⇒ Object (readonly)
Returns the value of attribute treat_nil.
6 7 8 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 6 def treat_nil @treat_nil end |
#treat_omitted ⇒ Object (readonly)
Returns the value of attribute treat_omitted.
6 7 8 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 6 def treat_omitted @treat_omitted end |
Class Method Details
.parsed_assign_writer(model_class, target) ⇒ Object
456 457 458 459 460 461 462 463 464 465 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 456 def self.parsed_assign_writer(model_class, target) key = [model_class, target] writer = PARSED_ASSIGN_WRITERS[key] return writer unless writer.nil? name = :"__assign_parsed_#{target}=" writer = model_class.method_defined?(name) ? name : false PARSED_ASSIGN_WRITERS[key] = writer writer end |
.transform_dispatch(rule, attr) ⇒ Object
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 482 def self.transform_dispatch(rule, attr) per_attr = TRANSFORM_DISPATCH[rule] if per_attr.nil? per_attr = {}.compare_by_identity TRANSFORM_DISPATCH[rule] = per_attr end verdict = per_attr[attr] return verdict if verdict rule_transform = rule.transform # NB: `::Hash` — inside Lutaml::Model the bare constant `Hash` is # the hash-format adapter class, so a plain is_a?(Hash) is always # false here (the original dispatch shared that shadow and never # took the hash-transform branch). rule_has = rule_transform.is_a?(Class) || (rule_transform.is_a?(::Hash) && !rule_transform.empty?) attr_transform = attr&.transform attr_has = attr_transform && (attr_transform.is_a?(Class) || (attr_transform.is_a?(::Hash) && !attr_transform.empty?)) verdict = if !rule_has && !attr_has :assign elsif rule.get_transformers(attr).any? do |t| t.is_a?(Class) && t < Lutaml::Model::ValueTransformer end :assign else :import end per_attr[attr] = verdict verdict end |
Instance Method Details
#can_transform_to?(attribute, format) ⇒ Boolean
442 443 444 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 442 def can_transform_to?(attribute, format) get_transformers(attribute).any? { |t| t.can_transform?(:to, format) } end |
#custom_method_only? ⇒ Boolean
360 361 362 363 364 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 360 def custom_method_only? name = attribute_name.to_s (name.start_with?("__") && name.end_with?("__")) || (has_custom_methods? && attribute_type.nil?) end |
#deep_dup ⇒ Object
390 391 392 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 390 def deep_dup raise NotImplementedError, "Subclasses must implement `deep_dup`." end |
#default_value_map(options = {}) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 143 def default_value_map( = {}) render_nil_as = render_as(:render_nil, :omitted, ) render_empty_as = render_as(:render_empty, :empty, ) treat_nil_as = treat_as(:treat_nil, :nil, ) treat_empty_as = treat_as(:treat_empty, :empty, ) treat_omitted_as = treat_as(:treat_omitted, :nil, ) { from: { omitted: treat_omitted_as, nil: treat_nil_as, empty: treat_empty_as }, to: { omitted: :omitted, nil: render_nil_as, empty: render_empty_as }, } end |
#deserialize(model, value, attributes, mapper_class = nil, context = nil, pre_cast: false) ⇒ Object
331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 331 def deserialize(model, value, attributes, mapper_class = nil, context = nil, pre_cast: false) if @needs_full_deserialize handle_custom_method(model, value, mapper_class, context) || handle_delegate(model, value, attributes) || handle_transform_method(model, value, attributes, context, pre_cast: pre_cast) else handle_transform_method(model, value, attributes, context, pre_cast: pre_cast) end end |
#eql?(other) ⇒ Boolean Also known as: ==
382 383 384 385 386 387 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 382 def eql?(other) other.class == self.class && instance_variables.all? do |var| instance_variable_get(var) == other.instance_variable_get(var) end end |
#get_transformers(attribute) ⇒ Object
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 516 def get_transformers(attribute) # Fast path: most rules have no transforms at all rule_transform = transform attr_transform = attribute&.transform if !rule_transform && !attr_transform return EMPTY_TRANSFORMERS end # Build transformer list only when needed transformers = [] transformers << rule_transform if rule_transform transformers << attr_transform if attr_transform transformers.select! { |t| t.is_a?(Class) } transformers.freeze end |
#has_custom_method_for_deserialization? ⇒ Boolean
370 371 372 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 370 def has_custom_method_for_deserialization? !custom_methods.empty? && custom_methods[:from] end |
#has_custom_method_for_serialization? ⇒ Boolean
366 367 368 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 366 def has_custom_method_for_serialization? !custom_methods.empty? && custom_methods[:to] end |
#has_items?(value) ⇒ Boolean
Check if value is a non-empty collection
245 246 247 248 249 250 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 245 def has_items?(value) return false if value.nil? || Utils.uninitialized?(value) return false unless value.is_a?(String) || value.is_a?(Array) || value.is_a?(Hash) !value.empty? end |
#multiple_mappings? ⇒ Boolean
374 375 376 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 374 def multiple_mappings? name.is_a?(Array) end |
#mutated_collection?(value, instance) ⇒ Boolean
235 236 237 238 239 240 241 242 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 235 def mutated_collection?(value, instance) return false if value.nil? || Utils.uninitialized?(value) return false unless value.is_a?(Array) || value.is_a?(Lutaml::Model::Collection) return false if value.empty? # Empty collection is still default # If it's a non-empty collection and marked as using_default, it was mutated instance.is_a?(Lutaml::Model::Serialize) && instance.using_default?(to) end |
#name_strings ⇒ Object
Normalized name spellings for matching, computed once. The name is fixed after construction (attr_reader only), so the memo is safe. Hot loops used to redo Array(name).map(&:to_s) per element.
51 52 53 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 51 def name_strings @name_strings ||= Array(name).map(&:to_s).freeze end |
#polymorphic_mapping? ⇒ Boolean
289 290 291 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 289 def polymorphic_mapping? polymorphic_map && !polymorphic_map.empty? end |
#raw_mapping? ⇒ Boolean
378 379 380 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 378 def raw_mapping? name == Constants::RAW_MAPPING_KEY end |
#raw_value_map ⇒ Object
Raw value_map hash access (for compiled rules). Use value_map(key, options) for individual lookups with overrides.
396 397 398 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 396 def raw_value_map @value_map end |
#render?(value, instance = nil, options = EMPTY_RENDER_OPTIONS) ⇒ Boolean
196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 196 def render?(value, instance = nil, = EMPTY_RENDER_OPTIONS) if invalid_value?(value, ) false # FIXED: Check if collection was mutated after initialization # A non-empty collection initialized with default [] should render if mutated # This handles the case where collection is mutated with << or custom methods elsif mutated_collection?(value, instance) true elsif instance.is_a?(Lutaml::Model::Serialize) && instance.using_default?(to) render_default? || RenderPolicy.derived_attribute_for?(instance, to) else true end end |
#render_as(key, default_value, options = {}) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 158 def render_as(key, default_value, = {}) value = public_send(key) value = [key] if value.nil? if value == true key.to_s.split("_").last.to_sym elsif value == false :omitted elsif value { as_empty: :empty, as_blank: :blank, as_nil: :nil, omit: :omitted, }[value] else default_value end end |
#render_empty?(options = {}) ⇒ Boolean
263 264 265 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 263 def render_empty?( = {}) value_map(:to, )[:empty] != :omitted end |
#render_nil?(options = {}) ⇒ Boolean
259 260 261 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 259 def render_nil?( = {}) value_map(:to, )[:nil] != :omitted end |
#render_omitted?(options = {}) ⇒ Boolean
267 268 269 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 267 def render_omitted?( = {}) value_map(:to, )[:omitted] != :omitted end |
#render_value_for(value) ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 223 def render_value_for(value) if value.nil? value_for_option(value_map(:to)[:nil]) elsif Utils.empty?(value) value_for_option(value_map(:to)[:empty], value) elsif Utils.uninitialized?(value) value_for_option(value_map(:to)[:omitted]) else value end end |
#serialize(model, parent = nil, doc = nil, context = nil) ⇒ Object
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 303 def serialize(model, parent = nil, doc = nil, context = nil) if custom_methods[:to] to_method = custom_methods[:to] # lutaml-model#550: custom methods may declare a fourth # context parameter to receive the options passed to `to_*`. if model.method(to_method).parameters.size >= 4 model.public_send(to_method, model, parent, doc, context) else model.public_send(to_method, model, parent, doc) end else value = to_value_for(model) # Handle Reference types - extract the key for serialization # This ensures references serialize as their key, not the resolved object if value.is_a?(Lutaml::Model::Type::Reference) value = value.key elsif value.is_a?(Array) # Handle collection of references value = value.map do |v| v.is_a?(Lutaml::Model::Type::Reference) ? v.key : v end end value end end |
#serialize? ⇒ Boolean
Whether this rule participates in serialization output. False marks a hydrate-only mapping: consumed by from_* but never rendered by to_*. (The value callback #serialize is separate.)
193 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 193 def serialize? = @serialize_mapping |
#to_value_for(model) ⇒ Object
293 294 295 296 297 298 299 300 301 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 293 def to_value_for(model) if delegate model.public_send(delegate).public_send(to) else return if to.nil? model.public_send(to) end end |
#transform_value(attribute, value, read_method, format) ⇒ Object
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 417 def transform_value(attribute, value, read_method, format) # Fast path: no transforms at all (covers 95%+ of rules) # transform defaults to {}, so check for actual content, not just truthiness has_rule_transform = transform.is_a?(Class) has_attr_transform = attribute&.transform.is_a?(Class) return value unless has_rule_transform || has_attr_transform transformers = get_transformers(attribute) transformers = transformers.reverse if read_method == :to return value if transformers.empty? || transformers.none? do |t| t.can_transform?(read_method, format) end # Apply transformers in sequence transformers.reduce(value) do |v, transformer| if transformer.is_a?(Class) && transformer < Lutaml::Model::ValueTransformer # Call class method directly: NameTransformer.from(value, :json) else # Hash/proc transformer end transformer.public_send(read_method, v, format) end end |
#treat?(value) ⇒ Boolean
211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 211 def treat?(value) if value.nil? treat_nil? elsif Utils.uninitialized?(value) treat_omitted? elsif Utils.empty?(value) treat_empty? else true end end |
#treat_as(key, default_value, options = {}) ⇒ Object
178 179 180 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 178 def treat_as(key, default_value, = {}) public_send(key) || [key] || default_value end |
#treat_empty?(options = {}) ⇒ Boolean
277 278 279 280 281 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 277 def treat_empty?( = {}) return @treat_empty_flag unless [:nil] || [:empty] || [:omitted] value_map(:from, )[:empty] != :omitted end |
#treat_nil?(options = {}) ⇒ Boolean
271 272 273 274 275 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 271 def treat_nil?( = {}) return @treat_nil_flag unless [:nil] || [:empty] || [:omitted] value_map(:from, )[:nil] != :omitted end |
#treat_omitted?(options = {}) ⇒ Boolean
283 284 285 286 287 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 283 def treat_omitted?( = {}) return @treat_omitted_flag unless [:nil] || [:empty] || [:omitted] value_map(:from, )[:omitted] != :omitted end |
#value_for_option(option, empty_value = nil) ⇒ Object
252 253 254 255 256 257 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 252 def value_for_option(option, empty_value = nil) return nil if option == :nil return empty_value || "" if option == :empty Lutaml::Model::UninitializedClass.instance end |
#value_map(key, options = {}) ⇒ Object
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 400 def value_map(key, = {}) # Fast path: when no overrides, return cached value directly # Callers MUST NOT mutate the returned hash if ![:nil] && ![:empty] && ![:omitted] return @value_map[key] end # Slow path: merge overrides (only when options have actual values) overrides = { nil: [:nil], empty: [:empty], omitted: [:omitted], }.compact @value_map[key].merge(overrides) end |
#when_attribute ⇒ Object
Static per rule: placeholder __name__ targets and inferred
custom-method targets without an attribute type never change —
the applier asked this per element and paid two to_s strings
each time (240k allocations on the ISO-13849 serialize profile).
Rules may be frozen, so no per-instance memo: one to_s instead
of two is the frozen-safe diet.
Discriminator pairs this rule selects (and re-emits) on, empty
for an ordinary rule.
352 353 354 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 352 def when_attribute @when_attribute ||= {}.freeze end |
#when_attribute? ⇒ Boolean
356 357 358 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 356 def when_attribute? !when_attribute.empty? end |