Class: Morfo::Base
- Inherits:
-
Object
- Object
- Morfo::Base
- Defined in:
- lib/morfo.rb
Class Method Summary collapse
- .apply_transformation(value, transformation) ⇒ Object
- .extract_value(value, from) ⇒ Object
- .map(from, to, &transformation) ⇒ Object
- .mapping_actions ⇒ Object
- .morf(input) ⇒ Object
Class Method Details
.apply_transformation(value, transformation) ⇒ Object
29 30 31 |
# File 'lib/morfo.rb', line 29 def self.apply_transformation value, transformation transformation ? transformation.call(value) : value end |
.extract_value(value, from) ⇒ Object
23 24 25 26 27 |
# File 'lib/morfo.rb', line 23 def self.extract_value value, from Array(from).inject(value) do |resulting_value, key| resulting_value ? resulting_value[key] : nil end end |
.map(from, to, &transformation) ⇒ Object
5 6 7 |
# File 'lib/morfo.rb', line 5 def self.map from, to, &transformation mapping_actions << [from, to, transformation] end |
.mapping_actions ⇒ Object
33 34 35 |
# File 'lib/morfo.rb', line 33 def self.mapping_actions @actions ||= [] end |
.morf(input) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/morfo.rb', line 9 def self.morf input input.map do |value| mapping_actions.inject({}) do |output, (from, to, transformation)| resulting_value = apply_transformation( extract_value(value, from), transformation ) output.merge!(to => resulting_value) if resulting_value output end end end |