Sha256: 63a08fb8997d8ba8c656af615897203fa115e33ab8e4024713efe87b7c9f5039

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

require 'deep_merge/rails_compat'
require "morfo/version"
require "morfo/tools"
require "morfo/actions"
require "morfo/builder"
require "morfo/errors"

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.deeper_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

1 entries across 1 versions & 1 rubygems

Version Path
morfo-0.7.0 lib/morfo.rb