Class: Bmg::Summarizer

Inherits:
Object
  • Object
show all
Defined in:
lib/bmg/summarizer.rb,
lib/bmg/summarizer/avg.rb,
lib/bmg/summarizer/max.rb,
lib/bmg/summarizer/min.rb,
lib/bmg/summarizer/sum.rb,
lib/bmg/summarizer/count.rb,
lib/bmg/summarizer/concat.rb,
lib/bmg/summarizer/stddev.rb,
lib/bmg/summarizer/collect.rb,
lib/bmg/summarizer/variance.rb

Overview

Summarizer.

This class provides a basis for implementing aggregation operators.

Aggregation operators are made available through factory methods on the Summarizer class itself:

Summarizer.count
Summarizer.sum(:qty)
Summarizer.sum{|t| t[:qty] * t[:price] }

Once built, summarizers can be used either in black-box or white-box modes.

relation = ...
agg = Summarizer.sum(:qty)

# Black box mode:
result = agg.summarize(relation)

# White box mode:
memo = agg.least
relation.each do |tuple|
  memo = agg.happens(memo, tuple)
end
result = agg.finalize(memo)

Direct Known Subclasses

Avg, Collect, Concat, Count, Max, Min, Sum, Variance

Defined Under Namespace

Classes: Avg, Collect, Concat, Count, Max, Min, Stddev, Sum, Variance

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Summarizer

Creates an Summarizer instance.

Private method, please use the factory methods



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bmg/summarizer.rb', line 40

def initialize(*args, &block)
  @options = default_options
  args.push(block) if block
  args.each do |arg|
    case arg
    when Symbol, Proc then @functor = arg
    when Hash         then @options = @options.merge(arg)
    else
      raise ArgumentError, "Unexpected `#{arg}`"
    end
  end
end

Instance Attribute Details

#functorObject (readonly)

Returns the underlying functor, either a Symbol or a Proc.

Returns:

  • the underlying functor, either a Symbol or a Proc



35
36
37
# File 'lib/bmg/summarizer.rb', line 35

def functor
  @functor
end

#optionsObject (readonly)

Returns Aggregation options as a Hash.

Returns:

  • Aggregation options as a Hash



32
33
34
# File 'lib/bmg/summarizer.rb', line 32

def options
  @options
end

Class Method Details

.avg(*args, &bl) ⇒ Object

Factors an average summarizer



31
32
33
# File 'lib/bmg/summarizer/avg.rb', line 31

def self.avg(*args, &bl)
  Avg.new(*args, &bl)
end

.collect(*args, &bl) ⇒ Object

Factors a collect summarizer



26
27
28
# File 'lib/bmg/summarizer/collect.rb', line 26

def self.collect(*args, &bl)
  Collect.new(*args, &bl)
end

.concat(*args, &bl) ⇒ Object

Factors a concatenation summarizer



37
38
39
# File 'lib/bmg/summarizer/concat.rb', line 37

def self.concat(*args, &bl)
  Concat.new(*args, &bl)
end

.count(*args, &bl) ⇒ Object

Factors a count summarizer



26
27
28
# File 'lib/bmg/summarizer/count.rb', line 26

def self.count(*args, &bl)
  Count.new(*args, &bl)
end

.max(*args, &bl) ⇒ Object

Factors a max summarizer



26
27
28
# File 'lib/bmg/summarizer/max.rb', line 26

def self.max(*args, &bl)
  Max.new(*args, &bl)
end

.min(*args, &bl) ⇒ Object

Factors a min summarizer



26
27
28
# File 'lib/bmg/summarizer/min.rb', line 26

def self.min(*args, &bl)
  Min.new(*args, &bl)
end

.stddev(*args, &bl) ⇒ Object

Factors a standard deviation summarizer



21
22
23
# File 'lib/bmg/summarizer/stddev.rb', line 21

def self.stddev(*args, &bl)
  Stddev.new(*args, &bl)
end

.sum(*args, &bl) ⇒ Object

Factors a sum summarizer



26
27
28
# File 'lib/bmg/summarizer/sum.rb', line 26

def self.sum(*args, &bl)
  Sum.new(*args, &bl)
end

.variance(*args, &bl) ⇒ Object

Factors a variance summarizer



37
38
39
# File 'lib/bmg/summarizer/variance.rb', line 37

def self.variance(*args, &bl)
  Variance.new(*args, &bl)
end

Instance Method Details

#finalize(memo) ⇒ Object

This method finalizes an aggregation.

Argument memo is either least or the result of aggregating through happens. The default implementation simply returns memo. The method is intended to be overriden for complex aggregations that need statefull information such as ‘avg`.

Parameters:

  • memo (Object)

    the current aggregation value

Returns:

  • (Object)

    the aggregation value, as finalized



105
106
107
# File 'lib/bmg/summarizer.rb', line 105

def finalize(memo)
  memo
end

#happens(memo, tuple) ⇒ Object

This method is called on each aggregated tuple and must return an updated memo value. It can be seen as the block typically given to Enumerable.inject.

The default implementation collects the pre-value on the tuple and delegates to _happens.

Parameters:

  • memo

    the current aggregation value

  • the

    current iterated tuple

Returns:

  • updated memo value



82
83
84
85
# File 'lib/bmg/summarizer.rb', line 82

def happens(memo, tuple)
  value = @functor.is_a?(Proc) ? @functor.call(tuple) : tuple[@functor]
  _happens(memo, value)
end

#leastObject

Returns the least value, which is the one to use on an empty set.

This method is intended to be overriden by subclasses; default implementation returns nil.

Returns:

  • the least value for this summarizer



68
69
70
# File 'lib/bmg/summarizer.rb', line 68

def least
  nil
end

#summarize(enum) ⇒ Object

Summarizes an enumeration of tuples.

Parameters:

  • an

    enumerable of tuples

Returns:

  • the computed summarization value



113
114
115
# File 'lib/bmg/summarizer.rb', line 113

def summarize(enum)
  finalize(enum.inject(least){|m,t| happens(m, t) })
end

#to_summarizer_nameObject

Returns the canonical summarizer name



118
119
120
# File 'lib/bmg/summarizer.rb', line 118

def to_summarizer_name
  self.class.name.downcase[/::([a-z]+)$/, 1].to_sym
end