Class: Benelux::Stats

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

Defined Under Namespace

Classes: Calculator, Group

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*names) ⇒ Stats

Returns a new instance of Stats.



6
7
8
9
# File 'lib/benelux/stats.rb', line 6

def initialize(*names)
  @names = []
  add_groups names
end

Instance Attribute Details

#namesObject (readonly)

Returns the value of attribute names.



4
5
6
# File 'lib/benelux/stats.rb', line 4

def names
  @names
end

Instance Method Details

#+(other) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/benelux/stats.rb', line 49

def +(other)
  if !other.is_a?(Benelux::Stats)
    raise TypeError, "can't convert #{other.class} into Stats" 
  end
  s = self.clone
  other.names.each do |name|
    s.add_group name
    s.group(name) << other.group(name)
  end
  s
end

#add_groups(*args) ⇒ Object Also known as: add_group



33
34
35
36
37
38
39
40
41
# File 'lib/benelux/stats.rb', line 33

def add_groups(*args)
  args.flatten.each do |meth|
    next if has_group? meth
    @names << meth
    self.class.send :attr_reader, meth
    (g = Benelux::Stats::Group.new).name = meth
    instance_variable_set("@#{meth}", g)
  end
end

#clearObject



18
19
20
21
# File 'lib/benelux/stats.rb', line 18

def clear
  each { |g| g.clear }
  @names.clear
end

#create_zero_group(name) ⇒ Object



13
14
15
16
17
# File 'lib/benelux/stats.rb', line 13

def create_zero_group(name)
  g = Benelux::Stats::Group.new
  g.name = name
  g
end

#each(&blk) ⇒ Object

Each group



26
27
28
# File 'lib/benelux/stats.rb', line 26

def each(&blk)
  @names.each { |name| blk.call(group(name)) }
end

#each_pair(&blk) ⇒ Object

Each group name, group



30
31
32
# File 'lib/benelux/stats.rb', line 30

def each_pair(&blk)
  @names.each { |name| blk.call(name, group(name)) }
end

#group(name) ⇒ Object



10
11
12
# File 'lib/benelux/stats.rb', line 10

def group(name)
  @names.member?(name) ? self.send(name) : create_zero_group(name)
end

#has_group?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def has_group?(name)
  @names.member? name
end

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



42
43
44
# File 'lib/benelux/stats.rb', line 42

def sample(name, s, tags={})
  self.send(name).sample(s, tags)
end

#sizeObject



22
23
24
# File 'lib/benelux/stats.rb', line 22

def size
  @names.size
end