Sha256: d63b6659bd5f5c063b26f71f58105b13f0635e89fdba3b21d8657f37f4034887

Contents?: true

Size: 1.3 KB

Versions: 5

Compression:

Stored size: 1.3 KB

Contents

module Alf
  module Engine
    #
    # Aggregates the whole operand according to a Summarization. The result 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
      include 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, expr = nil, compiler = nil)
        super(expr, compiler)
        @operand = operand
        @summarization = summarization
      end

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

      def arguments
        [ summarization ]
      end

    end # class Aggregate
  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/aggregate.rb
alf-core-0.16.2 lib/alf/engine/aggregate.rb
alf-core-0.16.1 lib/alf/engine/aggregate.rb
alf-core-0.16.0 lib/alf/engine/aggregate.rb
alf-core-0.15.0 lib/alf/engine/aggregate.rb