Class: Clockblock::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/clockblock/timer.rb

Defined Under Namespace

Classes: TimerException

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) {|_self| ... } ⇒ Timer

Returns a new instance of Timer.

Yields:

  • (_self)

Yield Parameters:



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_resultObject (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_atObject (readonly)

Returns the value of attribute finished_at.



3
4
5
# File 'lib/clockblock/timer.rb', line 3

def finished_at
  @finished_at
end

#stageObject (readonly)

Returns the value of attribute stage.



3
4
5
# File 'lib/clockblock/timer.rb', line 3

def stage
  @stage
end

#started_atObject (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

#attributesObject



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.message}'!")
  end
  @clockblock_result
end

#durationObject



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

#finishObject



34
35
36
# File 'lib/clockblock/timer.rb', line 34

def finish
  stop
end

#inspectObject



50
51
52
# File 'lib/clockblock/timer.rb', line 50

def inspect
  to_s
end

#startObject



23
24
25
26
27
# File 'lib/clockblock/timer.rb', line 23

def start
  @finished_at = nil
  @stage = :started
  @started_at = Time.now
end

#stopObject



29
30
31
32
# File 'lib/clockblock/timer.rb', line 29

def stop
  @stage = :finished
  @finished_at = Time.now
end

#to_sObject



54
55
56
# File 'lib/clockblock/timer.rb', line 54

def to_s
  attributes.to_s
end