Sha256: 459df48982556212d372a2fa1deaaa63468a9f953d97abe06005af41ba339bd8

Contents?: true

Size: 967 Bytes

Versions: 5

Compression:

Stored size: 967 Bytes

Contents

module Alf
  module Engine
    #
    # Autonumbers input tuples under a new `as` attribute. Autonumbering starts
    # at 0.
    #
    # Example:
    #
    #     operand = [
    #       {:name => "Jones"}, 
    #       {:name => "Smith"}
    #     ]
    #     Autonum.new(operand, :id).to_a
    #     # => [
    #     #      {:name => "Jones", :id => 0}, 
    #     #      {:name => "Smith", :id => 1}
    #     #    ]
    #
    class Autonum < Cog

      # @return [Enumerable] The operand
      attr_reader :operand

      # @return [Symbol] Name of the autonum attribute
      attr_reader :as

      # Creates an Autonum instance
      def initialize(operand, as)
        @operand = operand
        @as = as
      end

      # (see Cog#each)
      def each
        autonum = 0
        @operand.each do |tuple|
          yield tuple.merge(@as => autonum)
          autonum += 1
        end
      end

    end # class Autonum
  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/autonum.rb
alf-0.12.1 lib/alf-engine/alf/engine/autonum.rb
alf-0.12.0 lib/alf-engine/alf/engine/autonum.rb
alf-0.11.1 lib/alf-engine/alf/engine/autonum.rb
alf-0.11.0 lib/alf-engine/alf/engine/autonum.rb