Class: TestBench::Output::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/test_bench/output/timer.rb,
lib/test_bench/output/timer/substitute.rb

Direct Known Subclasses

Substitute::Timer

Defined Under Namespace

Modules: Mode, Substitute

Constant Summary collapse

Error =
Class.new(RuntimeError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#start_timeObject

Returns the value of attribute start_time.



6
7
8
# File 'lib/test_bench/output/timer.rb', line 6

def start_time
  @start_time
end

Class Method Details

.configure(receiver, attr_name: nil) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/test_bench/output/timer.rb', line 8

def self.configure(receiver, attr_name: nil)
  attr_name ||= :timer

  instance = new
  receiver.public_send(:"#{attr_name}=", instance)
  instance
end

Instance Method Details

#modeObject



48
49
50
51
52
53
54
# File 'lib/test_bench/output/timer.rb', line 48

def mode
  if start_time.nil?
    Mode.stopped
  else
    Mode.running
  end
end

#resetObject



56
57
58
59
60
61
62
# File 'lib/test_bench/output/timer.rb', line 56

def reset
  previous_start_time = self.start_time

  self.start_time = nil

  previous_start_time
end

#running?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/test_bench/output/timer.rb', line 40

def running?
  mode == Mode.running
end

#start(now = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/test_bench/output/timer.rb', line 16

def start(now=nil)
  now ||= ::Time.now.utc

  if mode == Mode.running
    raise Error, "Timer has already started (Start Time: #{start_time})"
  end

  self.start_time = now
end

#stop(now = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/test_bench/output/timer.rb', line 26

def stop(now=nil)
  now ||= ::Time.now.utc

  if mode == Mode.stopped
    raise Error, "Timer has not started"
  end

  start_time = reset

  elapsed = now - start_time

  elapsed.round(3)
end

#stopped?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/test_bench/output/timer.rb', line 44

def stopped?
  mode == Mode.stopped
end