Class: SpecForge::Forge::Timer

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

Overview

Simple timer for tracking execution duration

Used to measure how long a forge run takes from start to finish.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTimer

Creates a new timer in the reset state



22
23
24
# File 'lib/spec_forge/forge/timer.rb', line 22

def initialize
  reset
end

Instance Attribute Details

#started_atTime? (readonly)

Returns Time when the timer started.

Returns:

  • (Time, nil)

    Time when the timer started



12
13
14
# File 'lib/spec_forge/forge/timer.rb', line 12

def started_at
  @started_at
end

#stopped_atTime? (readonly)

Returns Time when the timer stopped.

Returns:

  • (Time, nil)

    Time when the timer stopped



15
16
17
# File 'lib/spec_forge/forge/timer.rb', line 15

def stopped_at
  @stopped_at
end

Instance Method Details

#resetTimer

Resets the timer to its initial state

Returns:

  • (Timer)

    self for chaining



31
32
33
34
35
36
# File 'lib/spec_forge/forge/timer.rb', line 31

def reset
  @started_at = nil
  @stopped_at = nil

  self
end

#startTimer

Starts the timer

Returns:

  • (Timer)

    self for chaining



43
44
45
46
47
48
49
# File 'lib/spec_forge/forge/timer.rb', line 43

def start
  reset

  @started_at ||= Time.current

  self
end

#started?Boolean

Returns whether the timer has been started

Returns:

  • (Boolean)

    True if the timer has been started



69
70
71
# File 'lib/spec_forge/forge/timer.rb', line 69

def started?
  !started_at.nil?
end

#stopTimer

Stops the timer

Returns:

  • (Timer)

    self for chaining



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

def stop
  return self if @started_at.nil?

  @stopped_at ||= Time.current

  self
end

#stopped?Boolean

Returns whether the timer has been stopped

Returns:

  • (Boolean)

    True if the timer has been stopped



78
79
80
# File 'lib/spec_forge/forge/timer.rb', line 78

def stopped?
  !stopped_at.nil?
end

#time_elapsedFloat

Returns the elapsed time in seconds

Returns:

  • (Float)

    Seconds elapsed since start (or 0 if not started)



87
88
89
90
91
# File 'lib/spec_forge/forge/timer.rb', line 87

def time_elapsed
  return 0 if started_at.nil?

  (stopped_at || Time.current) - started_at
end