Class: Chawk::Models::Aggregator

Inherits:
Object
  • Object
show all
Defined in:
lib/aggregator.rb

Overview

TODO: Most expensive version imaginable. To be replaced.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ Aggregator

Returns a new instance of Aggregator.



9
10
11
12
13
14
# File 'lib/aggregator.rb', line 9

def initialize(node)
  node.check_read_access
  if node.points.length > 0
    @dataset = node.points.to_a.reduce([]) {|ary,p| ary << p.value}
  end
end

Instance Attribute Details

#datasetObject (readonly)

Returns the value of attribute dataset.



7
8
9
# File 'lib/aggregator.rb', line 7

def dataset
  @dataset
end

Instance Method Details

#countObject



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

def count
  @dataset.length
end

#maxObject



16
17
18
# File 'lib/aggregator.rb', line 16

def max()
  @dataset.max
end

#meanObject



24
25
26
# File 'lib/aggregator.rb', line 24

def mean
  sum.to_f / @dataset.length
end

#minObject



20
21
22
# File 'lib/aggregator.rb', line 20

def min()
  @dataset.min
end

#stdevObject



40
41
42
43
# File 'lib/aggregator.rb', line 40

def stdev
  m = mean
  Math.sqrt((sumsqr - count * m * m)/(count-1))
end

#sumObject



28
29
30
# File 'lib/aggregator.rb', line 28

def sum
  @dataset.reduce(0) {|sum,p| sum+=p}
end

#sumsqrObject



36
37
38
# File 'lib/aggregator.rb', line 36

def sumsqr
  @dataset.map {|x| x * x}.reduce(&:+)
end