Class: TimeController

Inherits:
Object
  • Object
show all
Defined in:
lib/controllers/time_controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_timechunksObject

Public API



21
22
23
# File 'lib/controllers/time_controller.rb', line 21

def active_timechunks
  @active_timechunks
end

#timechunk_factoryObject

Public API



21
22
23
# File 'lib/controllers/time_controller.rb', line 21

def timechunk_factory
  @timechunk_factory
end

#timechunksObject

Public API



21
22
23
# File 'lib/controllers/time_controller.rb', line 21

def timechunks
  @timechunks
end

Instance Method Details

#list_active_timersObject

Lists all active timers.

Since:

  • x.x.x



74
75
76
# File 'lib/controllers/time_controller.rb', line 74

def list_active_timers
  active_timechunks
end

#start_timerObject

Creates a new timechunk, starting it at the moment of creation (i.e. an instantly running timer).

Since:

  • x.x.x



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.

Parameters:

  • id (defaults to: nil)

    A full or partial id of an active timechunk.

Since:

  • x.x.x



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