Class: ShellTest::ShellMethods::Timer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(clock = Time) ⇒ Timer

Returns a new instance of Timer.



9
10
11
12
# File 'lib/shell_test/shell_methods/timer.rb', line 9

def initialize(clock=Time)
  @clock = clock
  reset
end

Instance Attribute Details

#clockObject (readonly)

Returns the value of attribute clock.



4
5
6
# File 'lib/shell_test/shell_methods/timer.rb', line 4

def clock
  @clock
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



5
6
7
# File 'lib/shell_test/shell_methods/timer.rb', line 5

def start_time
  @start_time
end

#step_timeObject (readonly)

Returns the value of attribute step_time.



7
8
9
# File 'lib/shell_test/shell_methods/timer.rb', line 7

def step_time
  @step_time
end

#stop_timeObject (readonly)

Returns the value of attribute stop_time.



6
7
8
# File 'lib/shell_test/shell_methods/timer.rb', line 6

def stop_time
  @stop_time
end

Instance Method Details

#current_timeObject



14
15
16
# File 'lib/shell_test/shell_methods/timer.rb', line 14

def current_time
  clock.now.to_f
end

#elapsed_timeObject



35
36
37
# File 'lib/shell_test/shell_methods/timer.rb', line 35

def elapsed_time
  current_time - start_time
end

#resetObject



18
19
20
21
22
# File 'lib/shell_test/shell_methods/timer.rb', line 18

def reset
  @start_time = nil
  @stop_time  = nil
  @step_time  = 0
end

#running?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/shell_test/shell_methods/timer.rb', line 31

def running?
  start_time.nil? || stop_time.nil? ? false : true
end

#start(max_run_time = 60) ⇒ Object



24
25
26
27
28
29
# File 'lib/shell_test/shell_methods/timer.rb', line 24

def start(max_run_time=60)
  reset
  @start_time = current_time
  @stop_time  = start_time + max_run_time
  @step_time  = stop_time
end

#stopObject



39
40
41
42
43
44
45
46
47
# File 'lib/shell_test/shell_methods/timer.rb', line 39

def stop
  if running?
    elapsed = elapsed_time
    reset
    elapsed
  else
    nil
  end
end

#timeoutObject



65
66
67
68
# File 'lib/shell_test/shell_methods/timer.rb', line 65

def timeout
  timeout = step_time - current_time
  timeout < 0 ? 0 : timeout
end

#timeout=(timeout) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/shell_test/shell_methods/timer.rb', line 49

def timeout=(timeout)
  unless running?
    raise "cannot set timeout unless running"
  end

  case
  when timeout.nil?
    @step_time = stop_time
  when timeout < 0 
    step_time
  else
    mtime = current_time + timeout
    @step_time = mtime > stop_time ? stop_time : mtime
  end
end