Class: TickTacker::Timer

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/tick_tacker/timer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTimer

Returns a new instance of Timer.



10
11
12
# File 'lib/tick_tacker/timer.rb', line 10

def initialize
  with(:total_time => 25.minutes)
end

Instance Attribute Details

#elapsed_timeObject

Returns the value of attribute elapsed_time.



8
9
10
# File 'lib/tick_tacker/timer.rb', line 8

def elapsed_time
  @elapsed_time
end

#total_timeObject (readonly)

Returns the value of attribute total_time.



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

def total_time
  @total_time
end

Instance Method Details

#notifyObject



23
24
25
26
27
# File 'lib/tick_tacker/timer.rb', line 23

def notify
  time_remaining = total_time - elapsed_time
  notification_options = {:time_remaining => time_remaining}
  notify_observers(notification_options)
end

#startObject



45
46
47
48
49
# File 'lib/tick_tacker/timer.rb', line 45

def start
  @ticker.repeat do
    self.update
  end
end

#stopObject



51
52
53
# File 'lib/tick_tacker/timer.rb', line 51

def stop
  @ticker.stop
end

#update(options = {}) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/tick_tacker/timer.rb', line 36

def update(options = {})
  @elapsed_time ||= 0
  default = {:time_elapsed => @ticker.interval}
  config = default.merge(options)
  update_time_elapsed(config[:time_elapsed])
  changed
  notify
end

#update_time_elapsed(time) ⇒ Object



29
30
31
32
33
34
# File 'lib/tick_tacker/timer.rb', line 29

def update_time_elapsed(time)
  @elapsed_time += time
  if @elapsed_time >= @total_time
    @ticker.stop
  end
end

#with(options) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/tick_tacker/timer.rb', line 14

def with(options)
  default = {:ticker => Ticker.new}
  config = default.merge(options)
  @ticker = config[:ticker]
  @ticker_time = @ticker.interval
  @total_time = config[:total_time]
  self
end