Sha256: f4420d661525a2ed90fd3ca5ab609e1ee75b64fc6791f36e6d4d6f137319114a

Contents?: true

Size: 1007 Bytes

Versions: 2

Compression:

Stored size: 1007 Bytes

Contents

require "active_support/core_ext/hash"
require "morfo/version"
require "morfo/tools"
require "morfo/actions"
require "morfo/builder"

module Morfo
  class Base
    def self.field *field_path
      act = Morfo::Actions::Field.new(field_path, mapping_actions)
      mapping_actions[field_path] = act
      act
    end

    def self.morf input, options = {}
      input.map { |row| morf_single(row, options) }
    end

    def self.morf_single input, options = {}
      output = {}
      mapping_actions.each do |field_path, action|
        output.deep_merge!(store_value(action.execute(input), field_path, options))
      end
      output
    end

    private
    def self.mapping_actions
      @actions ||= {}
    end

    def self.store_value value, to, options
      return {} if value.nil? && !options[:include_nil_values]

      to.reverse.inject({}) do |hash, key|
        if hash.empty?
          hash.merge!(key => value)
        else
          { key => hash }
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
morfo-0.5.1 lib/morfo.rb
morfo-0.5.0 lib/morfo.rb