Class: Clockblock::Timer
- Inherits:
-
Object
- Object
- Clockblock::Timer
- Defined in:
- lib/clockblock/timer.rb
Defined Under Namespace
Classes: TimerException
Instance Attribute Summary collapse
-
#clockblock_result ⇒ Object
readonly
Returns the value of attribute clockblock_result.
-
#finished_at ⇒ Object
readonly
Returns the value of attribute finished_at.
-
#stage ⇒ Object
readonly
Returns the value of attribute stage.
-
#started_at ⇒ Object
readonly
Returns the value of attribute started_at.
Instance Method Summary collapse
- #attributes ⇒ Object
- #clock(name = "Anonymous Timer") ⇒ Object
- #duration ⇒ Object
- #finish ⇒ Object
-
#initialize(opts = {}) {|_self| ... } ⇒ Timer
constructor
A new instance of Timer.
- #inspect ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(opts = {}) {|_self| ... } ⇒ Timer
Returns a new instance of Timer.
5 6 7 8 |
# File 'lib/clockblock/timer.rb', line 5 def initialize(opts = {}) @stage = :initialized yield self if block_given? end |
Instance Attribute Details
#clockblock_result ⇒ Object (readonly)
Returns the value of attribute clockblock_result.
3 4 5 |
# File 'lib/clockblock/timer.rb', line 3 def clockblock_result @clockblock_result end |
#finished_at ⇒ Object (readonly)
Returns the value of attribute finished_at.
3 4 5 |
# File 'lib/clockblock/timer.rb', line 3 def finished_at @finished_at end |
#stage ⇒ Object (readonly)
Returns the value of attribute stage.
3 4 5 |
# File 'lib/clockblock/timer.rb', line 3 def stage @stage end |
#started_at ⇒ Object (readonly)
Returns the value of attribute started_at.
3 4 5 |
# File 'lib/clockblock/timer.rb', line 3 def started_at @started_at end |
Instance Method Details
#attributes ⇒ Object
46 47 48 |
# File 'lib/clockblock/timer.rb', line 46 def attributes { :started_at => started_at, :finished_at => finished_at, :duration => duration, :stage => stage } end |
#clock(name = "Anonymous Timer") ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/clockblock/timer.rb', line 10 def clock(name = "Anonymous Timer") begin start @stage = :executing @clockblock_result = yield stop rescue => ex @stage = :error raise TimerException.new("Error while executing '#{name}': '#{ex.}'!") end @clockblock_result end |
#duration ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/clockblock/timer.rb', line 38 def duration begin @finished_at - @started_at rescue nil end end |
#finish ⇒ Object
34 35 36 |
# File 'lib/clockblock/timer.rb', line 34 def finish stop end |
#inspect ⇒ Object
50 51 52 |
# File 'lib/clockblock/timer.rb', line 50 def inspect to_s end |
#start ⇒ Object
23 24 25 26 27 |
# File 'lib/clockblock/timer.rb', line 23 def start @finished_at = nil @stage = :started @started_at = Time.now end |
#stop ⇒ Object
29 30 31 32 |
# File 'lib/clockblock/timer.rb', line 29 def stop @stage = :finished @finished_at = Time.now end |
#to_s ⇒ Object
54 55 56 |
# File 'lib/clockblock/timer.rb', line 54 def to_s attributes.to_s end |