Sha256: 5677e8281239dde79ed6fd8110ea0801be7a26cc5c623ee9ba3a83319ee982f2

Contents?: true

Size: 951 Bytes

Versions: 2

Compression:

Stored size: 951 Bytes

Contents

require 'morfo/version'

module Morfo
  class Base
    def self.map from, to, &transformation
      mapping_actions << [from, to, transformation]
    end

    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

    private
    def self.extract_value value, from
      Array(from).inject(value) do |resulting_value, key|
        resulting_value ? resulting_value[key] : nil
      end
    end

    def self.apply_transformation value, transformation
      transformation ? transformation.call(value) : value
    end

    def self.mapping_actions
      @actions ||= []
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
morfo-0.0.2 lib/morfo.rb
morfo-0.0.1 lib/morfo.rb