Sha256: 5134b8c67ef864da1a533ba4e23097f43fac285bc601334a9caca846e6ed3a92

Contents?: true

Size: 1.24 KB

Versions: 5

Compression:

Stored size: 1.24 KB

Contents

module Alf
  module Engine
    #
    # Coerce input tuples according to a Heading. Attributes that do not belong
    # to the heading are simply ignored but are kept in the output.
    #
    # Example:
    #
    #     operand = [
    #       {:name => "Jones", :price => "10.0"}, 
    #       {:name => "Smith", :price => "-12.0"}
    #     ]
    #     
    #     Coerce.new(operand, Heading[:price => Float]).to_a
    #     # => [
    #     #      {:name => "Jones", :price => 10.0}, 
    #     #      {:name => "Smith", :price => -12.0}
    #     #    ]
    #
    class Coerce
      include Cog

      # @return [Enumerable] The operand
      attr_reader :operand

      # @return [Heading] Heading for coercions
      attr_reader :coercions

      # Creates an Coerce instance
      def initialize(operand, coercions, expr = nil, compiler = nil)
        super(expr, compiler)
        @operand = operand
        @coercions = coercions
      end

      # (see Cog#each)
      def _each
        @operand.each do |tuple|
          yield tuple.merge(Hash[@coercions.map{|k,d|
            [k, Support.coerce(tuple[k], d)]
          }])
        end
      end

      def arguments
        [ coercions ]
      end

    end # class Coerce
  end # module Engine
end # module Alf

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
alf-core-0.16.3 lib/alf/engine/coerce.rb
alf-core-0.16.2 lib/alf/engine/coerce.rb
alf-core-0.16.1 lib/alf/engine/coerce.rb
alf-core-0.16.0 lib/alf/engine/coerce.rb
alf-core-0.15.0 lib/alf/engine/coerce.rb