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.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#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
- #export_direction? ⇒ Boolean
- #get_transform(obj, direction) ⇒ Object
-
#initialize(rule, attribute, format = nil, context = nil) ⇒ Transformer
constructor
A new instance of Transformer.
-
#invoke_transform(method, value) ⇒ Object
lutaml-model#550:
with:custom methods opt into the options passed tofrom_*/to_*by declaring a second parameter (exact arity 2); one-parameter methods keep the historical single-argument call.
Constructor Details
#initialize(rule, attribute, format = nil, context = nil) ⇒ Transformer
Returns a new instance of Transformer.
49 50 51 52 53 54 |
# File 'lib/lutaml/model/services/transformer.rb', line 49 def initialize(rule, attribute, format = nil, context = nil) @rule = rule @attribute = attribute @format = format @context = context end |
Instance Attribute Details
#attribute ⇒ Object (readonly)
Returns the value of attribute attribute.
47 48 49 |
# File 'lib/lutaml/model/services/transformer.rb', line 47 def attribute @attribute end |
#context ⇒ Object (readonly)
Returns the value of attribute context.
47 48 49 |
# File 'lib/lutaml/model/services/transformer.rb', line 47 def context @context end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
47 48 49 |
# File 'lib/lutaml/model/services/transformer.rb', line 47 def format @format end |
#rule ⇒ Object (readonly)
Returns the value of attribute rule.
47 48 49 |
# File 'lib/lutaml/model/services/transformer.rb', line 47 def rule @rule end |
Class Method Details
.call(value, rule, attribute, format: nil, context: nil) ⇒ Object
5 6 7 |
# File 'lib/lutaml/model/services/transformer.rb', line 5 def call(value, rule, attribute, format: nil, context: nil) new(rule, attribute, format, context).call(value) end |
Instance Method Details
#apply_class_transformer(value, transformer_class, format) ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/lutaml/model/services/transformer.rb', line 87 def apply_class_transformer(value, transformer_class, format) if export_direction? transformer_class.to(value, format) else transformer_class.from(value, format) end end |
#call(value) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/lutaml/model/services/transformer.rb', line 56 def call(value) methods = transformation_methods class_transformers = ordered_sources.filter_map do |obj| next unless obj&.transform.is_a?(Class) && obj.transform < Lutaml::Model::ValueTransformer obj.transform end result = class_transformers.reduce(value) do |v, transformer_class| apply_class_transformer(v, transformer_class, format) end methods.reduce(result) do |transformed_value, method| invoke_transform(method, transformed_value) end end |
#export_direction? ⇒ Boolean
95 96 97 |
# File 'lib/lutaml/model/services/transformer.rb', line 95 def export_direction? false end |
#get_transform(obj, direction) ⇒ Object
99 100 101 102 103 104 |
# File 'lib/lutaml/model/services/transformer.rb', line 99 def get_transform(obj, direction) transform = obj&.transform return nil if transform.is_a?(Class) transform.is_a?(::Hash) ? transform[direction] : transform end |
#invoke_transform(method, value) ⇒ Object
lutaml-model#550: with: custom methods opt into the options
passed to from_* / to_* by declaring a second parameter
(exact arity 2); one-parameter methods keep the historical
single-argument call.
79 80 81 82 83 84 85 |
# File 'lib/lutaml/model/services/transformer.rb', line 79 def invoke_transform(method, value) if method.arity == 2 method.call(value, context) else method.call(value) end end |