Sha256: a97eb3f904c1302a7464c8f879685f30edc5fb1afdd8a161d7a07aa73a421a4d

Contents?: true

Size: 971 Bytes

Versions: 2

Compression:

Stored size: 971 Bytes

Contents

module Morfo
  class Builder
    def initialize(definitions)
      @definitions = definitions
    end

    def build
      # WTF??? `definitions` is not accessible inside class
      # so this javascript technique is necesseray
      tmp_definitions = definitions.map { |h| h.symbolize_keys }
      Class.new(Morfo::Base) do
        tmp_definitions.each do |definition|
          f = field(*definition[:field])

          if definition[:from]
            f = f.from(*definition[:from])
          end

          if definition[:calculated]
            base_hash = Morfo::Tools::BaseKeys.new(definition[:calculated]).build
            f = f.calculated { |r| definition[:calculated] % base_hash.merge(Morfo::Tools::FlattenHashKeys.new(r).flatten) }
          end

          if definition[:transformed]
            f = f.transformed { |v| definition[:transformed] % {value: v} }
          end
        end
      end
    end

    private

    attr_reader :definitions
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
morfo-0.6.0 lib/morfo/builder.rb
morfo-0.5.1 lib/morfo/builder.rb