Sha256: c3799624a67332fbde2795a5d6dae66dd9c5aaeefcfc5d8472981e8d00acfd13

Contents?: true

Size: 1.11 KB

Versions: 5

Compression:

Stored size: 1.11 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 < Cog

      # @return [Enumerable] The operand
      attr_reader :operand

      # @return [Heading] Heading for coercion
      attr_reader :heading

      # Creates an Coerce instance
      def initialize(operand, heading)
        @operand = operand
        @heading = heading
      end

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

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
alf-0.12.2 lib/alf-engine/alf/engine/coerce.rb
alf-0.12.1 lib/alf-engine/alf/engine/coerce.rb
alf-0.12.0 lib/alf-engine/alf/engine/coerce.rb
alf-0.11.1 lib/alf-engine/alf/engine/coerce.rb
alf-0.11.0 lib/alf-engine/alf/engine/coerce.rb