Class: Cadence::Computer

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

Defined Under Namespace

Classes: Counter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Computer

Returns a new instance of Computer.

Yields:

  • (_self)

Yield Parameters:



6
7
8
9
10
11
12
# File 'lib/cadence/computer.rb', line 6

def initialize
  @counter  = Counter.new
  @counters = {}
  @ticks    = {}
  
  yield self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



19
20
21
# File 'lib/cadence/computer.rb', line 19

def method_missing(method_name, *args, &block)
  self.for(method_name, &block)
end

Instance Attribute Details

#counterObject

Returns the value of attribute counter.



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

def counter
  @counter
end

#countersObject

Returns the value of attribute counters.



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

def counters
  @counters
end

#durationObject

Returns the value of attribute duration.



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

def duration
  @duration
end

#finished_atObject

Returns the value of attribute finished_at.



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

def finished_at
  @finished_at
end

#started_atObject

Returns the value of attribute started_at.



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

def started_at
  @started_at
end

#totalObject

Returns the value of attribute total.



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

def total
  @total
end

Instance Method Details

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

Overall total and count tracking



25
26
27
# File 'lib/cadence/computer.rb', line 25

def every(n = 1, &block)
  @counter.every(n, &block)
end

#for(key) ⇒ Object

Keys



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

def for(key)
  self.counters[key] ||= Counter.new
end

#nObject



65
66
67
# File 'lib/cadence/computer.rb', line 65

def n
  self.counter.count
end

#nextObject



28
29
30
31
32
33
34
# File 'lib/cadence/computer.rb', line 28

def next
  @counter.next
  @ticks.each do |(n, (last, tick))|
    @ticks[n] = [self.duration.to_i, tick]
    instance_eval(&tick) if self.duration.to_i > last.to_i && self.duration.to_i % n == 0
  end
end

#rateObject



60
61
62
63
# File 'lib/cadence/computer.rb', line 60

def rate
  # self.duration / self.counter.count
  self.counter.count / self.duration
end

#start {|_self| ... } ⇒ Object

Runtime

Yields:

  • (_self)

Yield Parameters:



44
45
46
47
48
49
50
# File 'lib/cadence/computer.rb', line 44

def start
  @started_at   = self.timestamp
  yield self
  @finished_at  = self.timestamp
  
  @duration = @finished_at - @started_at
end

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

Timing



38
39
40
# File 'lib/cadence/computer.rb', line 38

def ticks(n = 1, &block)
  @ticks[n] ||= [nil, block]
end

#timestampObject



52
53
54
# File 'lib/cadence/computer.rb', line 52

def timestamp
  Time.now
end