Sha256: 1fcab09ebfb240daf313c464cacc356a261025d30a21d32f47b936b731b15b14

Contents?: true

Size: 1.13 KB

Versions: 5

Compression:

Stored size: 1.13 KB

Contents

module Alf
  module Engine
    #
    # Aggregates the whole operand according to a Summarization. The resulting
    # iterator contains only one tuple.
    #
    # Example:
    #
    #   res = [
    #     {:name => "Jones", :price => 12.0, :id => 1},
    #     {:name => "Smith", :price => 10.0, :id => 2}
    #   ]
    #   agg = Summarization[:size  => "count", 
    #                       :total => "sum{ price }"]
    #   Aggregate.new(res, agg).to_a
    #   # => [
    #   #      {:size => 2, :total => 22.0}
    #   #    ]
    #
    class Aggregate < Cog

      # @return [Enumerable] The operand
      attr_reader :operand

      # @return [Summarization] The summarization to use
      attr_reader :summarization

      # Creates an Aggregate instance
      def initialize(operand, summarization)
        @operand = operand
        @summarization = summarization
      end

      # (see Cog#each)
      def each
        agg = operand.inject(@summarization.least) do |memo,tuple|
          @summarization.happens(memo, tuple)
        end
        yield @summarization.finalize(agg)
      end

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