Class: Cadence::Computer
- Inherits:
-
Object
- Object
- Cadence::Computer
- Defined in:
- lib/cadence/computer.rb
Defined Under Namespace
Classes: Counter
Instance Attribute Summary collapse
-
#counter ⇒ Object
Returns the value of attribute counter.
-
#counters ⇒ Object
Returns the value of attribute counters.
-
#duration ⇒ Object
Returns the value of attribute duration.
-
#finished_at ⇒ Object
Returns the value of attribute finished_at.
-
#started_at ⇒ Object
Returns the value of attribute started_at.
-
#total ⇒ Object
Returns the value of attribute total.
Instance Method Summary collapse
-
#every(n = 1, &block) ⇒ Object
Overall total and count tracking.
-
#for(key) ⇒ Object
Keys.
-
#initialize {|_self| ... } ⇒ Computer
constructor
A new instance of Computer.
- #method_missing(method_name, *args, &block) ⇒ Object
- #n ⇒ Object
- #next ⇒ Object
- #rate ⇒ Object
-
#start {|_self| ... } ⇒ Object
Runtime.
-
#ticks(n = 1, &block) ⇒ Object
Timing.
- #timestamp ⇒ Object
Constructor Details
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
#counter ⇒ Object
Returns the value of attribute counter.
3 4 5 |
# File 'lib/cadence/computer.rb', line 3 def counter @counter end |
#counters ⇒ Object
Returns the value of attribute counters.
3 4 5 |
# File 'lib/cadence/computer.rb', line 3 def counters @counters end |
#duration ⇒ Object
Returns the value of attribute duration.
3 4 5 |
# File 'lib/cadence/computer.rb', line 3 def duration @duration end |
#finished_at ⇒ Object
Returns the value of attribute finished_at.
3 4 5 |
# File 'lib/cadence/computer.rb', line 3 def finished_at @finished_at end |
#started_at ⇒ Object
Returns the value of attribute started_at.
3 4 5 |
# File 'lib/cadence/computer.rb', line 3 def started_at @started_at end |
#total ⇒ Object
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 |
#n ⇒ Object
65 66 67 |
# File 'lib/cadence/computer.rb', line 65 def n self.counter.count end |
#next ⇒ Object
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 |
#rate ⇒ Object
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
44 45 46 47 48 49 50 |
# File 'lib/cadence/computer.rb', line 44 def start @started_at = self. yield self @finished_at = self. @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 |
#timestamp ⇒ Object
52 53 54 |
# File 'lib/cadence/computer.rb', line 52 def Time.now end |