Class: Benelux::Stats::Group

Inherits:
Array
  • Object
show all
Defined in:
lib/benelux/stats.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Group

Returns a new instance of Group.



47
48
49
# File 'lib/benelux/stats.rb', line 47

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



46
47
48
# File 'lib/benelux/stats.rb', line 46

def name
  @name
end

Instance Method Details

#+(other) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/benelux/stats.rb', line 51

def +(other)
  unless @name == other.name
    raise BeneluxError, "Cannot add #{other.name} to #{@name}"
  end
  other.each do |newcalc|
    calcs = self.select { |calc| calc.tags == newcalc.tags }
    self << newcalc and next if calcs.empty?
    # This should only ever contain one b/c we should
    # not have several calculators with the same tags. 
    calcs.each do |calc|
      calc += newcalc
    end
  end
  self
end

#[](*tags) ⇒ Object Also known as: filter



96
97
98
99
100
101
102
# File 'lib/benelux/stats.rb', line 96

def [](*tags)
  tags = Benelux::TagHelpers.normalize tags
  g = Benelux::Stats::Group.new @name
  g << self.select { |c| c.tags >= tags }
  g.flatten!(1)  # only 1 level deep
  g
end

#maxObject



81
# File 'lib/benelux/stats.rb', line 81

def max()     merge.max    end

#meanObject



79
# File 'lib/benelux/stats.rb', line 79

def mean()    merge.mean   end

#merge(*tags) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/benelux/stats.rb', line 85

def merge(*tags)
  tags = Benelux::TagHelpers.normalize tags
  mc = Calculator.new
  all = tags.empty? ? self : self.filter(tags)
  all.each { |calc| 
    mc.samples calc
    mc.add_tags calc.tags
  }
  mc
end

#minObject



80
# File 'lib/benelux/stats.rb', line 80

def min()     merge.min    end

#nObject



83
# File 'lib/benelux/stats.rb', line 83

def n()      merge.n     end

#sample(s, tags = {}) ⇒ Object

Raises:



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/benelux/stats.rb', line 67

def sample(s, tags={})
  raise BeneluxError, "tags must be a Hash" unless tags.kind_of?(Hash)
  calcs = self.select { |c| c.tags == tags }
  if calcs.empty?
    (c = Calculator.new).add_tags tags
    self << c
    calcs = [c]
  end
  calcs.each { |c| c.sample(s) }
  nil
end

#sdObject



82
# File 'lib/benelux/stats.rb', line 82

def sd()      merge.sd     end