Class: Benelux::Stats

Inherits:
Object
  • 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.



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

def initialize(*names)
  @names = []
  add_keepers 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



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

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

#add_keepers(*args) ⇒ Object Also known as: add_keeper



20
21
22
23
24
25
26
27
# File 'lib/benelux/stats.rb', line 20

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

#each(&blk) ⇒ Object

Each keeper



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

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

#each_pair(&blk) ⇒ Object

Each keeper name, keeper



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

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

#get_keeper(name) ⇒ Object



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

def get_keeper(name)
  self.send name
end

#has_keeper?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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