Class: Cadence::Computer::Counter

Inherits:
Object
  • Object
show all
Defined in:
lib/cadence/computer/counter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(count = 0) ⇒ Counter

Returns a new instance of Counter.



5
6
7
8
# File 'lib/cadence/computer/counter.rb', line 5

def initialize(count = 0)
  @count    = 0
  @triggers = {}
end

Instance Attribute Details

#countObject

Returns the value of attribute count.



3
4
5
# File 'lib/cadence/computer/counter.rb', line 3

def count
  @count
end

#triggersObject

Returns the value of attribute triggers.



3
4
5
# File 'lib/cadence/computer/counter.rb', line 3

def triggers
  @triggers
end

Instance Method Details

#every(n = 1, &block) ⇒ Object



10
11
12
# File 'lib/cadence/computer/counter.rb', line 10

def every(n = 1, &block)
  @triggers[n] = block
end

#nObject



21
22
23
# File 'lib/cadence/computer/counter.rb', line 21

def n
  @count
end

#nextObject



14
15
16
17
18
19
# File 'lib/cadence/computer/counter.rb', line 14

def next
  @count += 1
  @triggers.each do |(n, trigger)|
    instance_eval(&trigger) if @count % n == 0
  end
end