Class: Elapse::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/elapse/instance.rb

Overview

An instance of the system.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Instance

Returns a new instance of Instance.



10
11
12
13
# File 'lib/elapse/instance.rb', line 10

def initialize(attrs = {})
  clear
  attrs.each {|k, v| send("#{k}=", v)}
end

Instance Attribute Details

#stackObject

Stacked stopwatches.



5
6
7
# File 'lib/elapse/instance.rb', line 5

def stack
  @stack
end

#stopwatchesObject

Named stopwatches.



8
9
10
# File 'lib/elapse/instance.rb', line 8

def stopwatches
  @stopwatches
end

Instance Method Details

#clearObject

Clear everything.



16
17
18
19
20
# File 'lib/elapse/instance.rb', line 16

def clear
  @stack = []
  @stopwatches = {}
  self    # By convention.
end

#cumulative(sw_key) ⇒ Object

Return sum of all measurements made by the stopwatch.

See Elapse::cumulative.



25
26
27
28
29
30
31
32
33
34
# File 'lib/elapse/instance.rb', line 25

def cumulative(sw_key)
  sw = find_stopwatch(sw_key)

  begin
    sw.cumulative
  rescue RuntimeError => e
    # Append stopwatch name.
    raise "#{e.message}: #{sw_key.inspect}"
  end
end

#reset(sw_key) ⇒ Object

Reset the named stopwatch.

See Elapse::reset.



39
40
41
42
# File 'lib/elapse/instance.rb', line 39

def reset(sw_key)
  # Let's be strict.
  find_stopwatch(sw_key).reset
end

#start(sw_key = nil) ⇒ Object

Start the stopwatch. Return Time::now.

See Elapse::start.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/elapse/instance.rb', line 47

def start(sw_key = nil)
  sw = if sw_key
    # Named mode.
    @stopwatches[sw_key] ||= Stopwatch.new
  else
    # Stacked mode.
    Stopwatch.new
  end

  begin
    out = sw.start
  rescue RuntimeError => e
    # Append stopwatch name.
    raise "#{e.message}: #{sw_key.inspect}"
  end

  if not sw_key
    # Stacked mode.
    @stack.push(sw)
  end

  out
end

#stop(sw_key = nil) ⇒ Object

Stop the stopwatch. Return Time::now.

See Elapse::stop.



74
75
76
# File 'lib/elapse/instance.rb', line 74

def stop(sw_key = nil)
  fetch_stopwatch(sw_key).stop
end

#took(sw_key = nil, &block) ⇒ Object

Return time of the last stopwatch measurement. If the stopwatch is running, stop it.

See Elapse::took.



81
82
83
84
85
86
87
88
89
# File 'lib/elapse/instance.rb', line 81

def took(sw_key = nil, &block)
  if block
    start(sw_key)
    yield
    return took(sw_key)
  end

  fetch_stopwatch(sw_key).took
end