Class: TimeController
- Inherits:
-
Object
- Object
- TimeController
- Defined in:
- lib/controllers/time_controller.rb
Instance Attribute Summary collapse
-
#active_timechunks ⇒ Object
Public API.
-
#timechunk_factory ⇒ Object
Public API.
-
#timechunks ⇒ Object
Public API.
Instance Method Summary collapse
-
#initialize(timechunk_factory: TimeChunk.method(:new)) ⇒ TimeController
constructor
A new instance of TimeController.
-
#list_active_timers ⇒ Object
Lists all active timers.
-
#start_timer ⇒ Object
Creates a new timechunk, starting it at the moment of creation (i.e. an instantly running timer).
-
#stop_timer(id: nil) ⇒ Object
Finalizes a running timechunk (a ‘timer’).
Constructor Details
#initialize(timechunk_factory: TimeChunk.method(:new)) ⇒ TimeController
Returns a new instance of TimeController.
11 12 13 14 15 |
# File 'lib/controllers/time_controller.rb', line 11 def initialize(timechunk_factory: TimeChunk.method(:new)) self.timechunks = [] self.active_timechunks = [] self.timechunk_factory = timechunk_factory end |
Instance Attribute Details
#active_timechunks ⇒ Object
Public API
21 22 23 |
# File 'lib/controllers/time_controller.rb', line 21 def active_timechunks @active_timechunks end |
#timechunk_factory ⇒ Object
Public API
21 22 23 |
# File 'lib/controllers/time_controller.rb', line 21 def timechunk_factory @timechunk_factory end |
#timechunks ⇒ Object
Public API
21 22 23 |
# File 'lib/controllers/time_controller.rb', line 21 def timechunks @timechunks end |
Instance Method Details
#list_active_timers ⇒ Object
Lists all active timers.
74 75 76 |
# File 'lib/controllers/time_controller.rb', line 74 def list_active_timers active_timechunks end |
#start_timer ⇒ Object
Creates a new timechunk, starting it at the moment of creation (i.e. an instantly running timer).
31 32 33 34 |
# File 'lib/controllers/time_controller.rb', line 31 def start_timer new_timechunk = timechunk_factory.call.tap(&:start) append_active_timechunk(new_timechunk) end |
#stop_timer(id: nil) ⇒ Object
Finalizes a running timechunk (a ‘timer’).
If there are no running timers, raises ‘NoActiveTimeChunkException`.
If there is exactly one running timer, stops that timer.
If there is more than one timer, and a valid id is provided for one of them, stops that timer. Otherwise, if the provided id is invalid, raises ‘InvalidTimeChunkException`, and if no id is provided, `AmbiguousTimeChunkException` is raised.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/controllers/time_controller.rb', line 52 def stop_timer(id: nil) case active_timechunks.count when 0 raise NoActiveTimeChunkException when 1 timechunk = active_timechunks.shift timechunk.finish else raise AmbiguousTimeChunkException if id.nil? timechunk = find_first_active_timechunk_by(id: id) active_timechunks.delete(timechunk) timechunk.finish end end |