Class: Lutaml::Model::Transformer
- Inherits:
-
Object
- Object
- Lutaml::Model::Transformer
- Defined in:
- lib/lutaml/model/services/transformer.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attribute ⇒ Object
readonly
Returns the value of attribute attribute.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#rule ⇒ Object
readonly
Returns the value of attribute rule.
Class Method Summary collapse
Instance Method Summary collapse
- #apply_class_transformer(value, transformer_class, format) ⇒ Object
- #call(value) ⇒ Object
- #get_transform(obj, direction) ⇒ Object
-
#initialize(rule, attribute, format = nil) ⇒ Transformer
constructor
A new instance of Transformer.
Constructor Details
#initialize(rule, attribute, format = nil) ⇒ Transformer
Returns a new instance of Transformer.
12 13 14 15 16 |
# File 'lib/lutaml/model/services/transformer.rb', line 12 def initialize(rule, attribute, format = nil) @rule = rule @attribute = attribute @format = format end |
Instance Attribute Details
#attribute ⇒ Object (readonly)
Returns the value of attribute attribute.
10 11 12 |
# File 'lib/lutaml/model/services/transformer.rb', line 10 def attribute @attribute end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
10 11 12 |
# File 'lib/lutaml/model/services/transformer.rb', line 10 def format @format end |
#rule ⇒ Object (readonly)
Returns the value of attribute rule.
10 11 12 |
# File 'lib/lutaml/model/services/transformer.rb', line 10 def rule @rule end |
Class Method Details
.call(value, rule, attribute, format: nil) ⇒ Object
5 6 7 |
# File 'lib/lutaml/model/services/transformer.rb', line 5 def call(value, rule, attribute, format: nil) new(rule, attribute, format).call(value) end |
Instance Method Details
#apply_class_transformer(value, transformer_class, format) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/lutaml/model/services/transformer.rb', line 48 def apply_class_transformer(value, transformer_class, format) if instance_of?(ExportTransformer) transformer_class.to(value, format) else transformer_class.from(value, format) end end |
#call(value) ⇒ Object
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 |
# File 'lib/lutaml/model/services/transformer.rb', line 18 def call(value) # Collect all class-based and hash/proc-based transformers # Apply them in the correct order based on transformation_methods # Get ordered transformation methods (already in correct precedence) methods = transformation_methods # Also check for class-based transformers and add them in correct order class_transformers = [] if instance_of?(ExportTransformer) # Export order: attribute first, then rule class_transformers << attribute.transform if attribute&.transform.is_a?(Class) && attribute.transform < Lutaml::Model::ValueTransformer class_transformers << rule.transform if rule&.transform.is_a?(Class) && rule.transform < Lutaml::Model::ValueTransformer else # Import order: rule first, then attribute class_transformers << rule.transform if rule&.transform.is_a?(Class) && rule.transform < Lutaml::Model::ValueTransformer class_transformers << attribute.transform if attribute&.transform.is_a?(Class) && attribute.transform < Lutaml::Model::ValueTransformer end # Apply class transformers first, then hash/proc transformers result = class_transformers.reduce(value) do |v, transformer_class| apply_class_transformer(v, transformer_class, format) end # Then apply hash/proc transformers methods.reduce(result) do |transformed_value, method| method.call(transformed_value) end end |
#get_transform(obj, direction) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/lutaml/model/services/transformer.rb', line 56 def get_transform(obj, direction) transform = obj&.transform return nil if transform.is_a?(Class) transform.is_a?(::Hash) ? transform[direction] : transform end |