Class: Lutaml::Model::Transformer

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/model/services/transformer.rb

Direct Known Subclasses

ExportTransformer, ImportTransformer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#attributeObject (readonly)

Returns the value of attribute attribute.



47
48
49
# File 'lib/lutaml/model/services/transformer.rb', line 47

def attribute
  @attribute
end

#contextObject (readonly)

Returns the value of attribute context.



47
48
49
# File 'lib/lutaml/model/services/transformer.rb', line 47

def context
  @context
end

#formatObject (readonly)

Returns the value of attribute format.



47
48
49
# File 'lib/lutaml/model/services/transformer.rb', line 47

def format
  @format
end

#ruleObject (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

Returns:

  • (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