Class: Chamomile::Timer
- Inherits:
-
Object
- Object
- Chamomile::Timer
- Defined in:
- lib/chamomile/components/timer.rb
Overview
Countdown timer with timeout notification and tick-based updates.
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#interval ⇒ Object
readonly
Returns the value of attribute interval.
-
#remaining ⇒ Object
readonly
Returns the value of attribute remaining.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Class Method Summary collapse
Instance Method Summary collapse
- #handle(msg) ⇒ Object (also: #update)
-
#initialize(timeout:, interval: 1.0) ⇒ Timer
constructor
A new instance of Timer.
- #reset ⇒ Object
- #running? ⇒ Boolean
- #start_cmd ⇒ Object
- #stop ⇒ Object
- #timed_out? ⇒ Boolean
- #toggle ⇒ Object
- #view ⇒ Object
Constructor Details
#initialize(timeout:, interval: 1.0) ⇒ Timer
Returns a new instance of Timer.
27 28 29 30 31 32 33 34 |
# File 'lib/chamomile/components/timer.rb', line 27 def initialize(timeout:, interval: 1.0) @id = self.class.next_id @timeout = timeout.to_f @interval = interval @remaining = @timeout @tag = 0 @running = false end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
25 26 27 |
# File 'lib/chamomile/components/timer.rb', line 25 def id @id end |
#interval ⇒ Object (readonly)
Returns the value of attribute interval.
25 26 27 |
# File 'lib/chamomile/components/timer.rb', line 25 def interval @interval end |
#remaining ⇒ Object (readonly)
Returns the value of attribute remaining.
25 26 27 |
# File 'lib/chamomile/components/timer.rb', line 25 def remaining @remaining end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
25 26 27 |
# File 'lib/chamomile/components/timer.rb', line 25 def timeout @timeout end |
Class Method Details
.next_id ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/chamomile/components/timer.rb', line 13 def self.next_id @id_mutex.synchronize do if Process.pid != @id_pid @id_pid = Process.pid @next_id = 0 @id_mutex = Mutex.new end @next_id += 1 "#{@id_pid}-tm-#{@next_id}" end end |
Instance Method Details
#handle(msg) ⇒ Object Also known as: update
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/chamomile/components/timer.rb', line 73 def handle(msg) case msg when TimerTickMsg return unless msg.id == @id && msg.tag == @tag @remaining = [(@remaining - @interval), 0.0].max @tag += 1 if @remaining <= 0 @running = false timeout_cmd else tick_cmd end end end |
#reset ⇒ Object
58 59 60 61 62 63 |
# File 'lib/chamomile/components/timer.rb', line 58 def reset @remaining = @timeout @running = false @tag += 1 self end |
#running? ⇒ Boolean
65 66 67 |
# File 'lib/chamomile/components/timer.rb', line 65 def running? @running end |
#start_cmd ⇒ Object
36 37 38 39 40 41 |
# File 'lib/chamomile/components/timer.rb', line 36 def start_cmd return nil if @running || timed_out? @running = true tick_cmd end |
#stop ⇒ Object
43 44 45 46 47 |
# File 'lib/chamomile/components/timer.rb', line 43 def stop @running = false @tag += 1 self end |
#timed_out? ⇒ Boolean
69 70 71 |
# File 'lib/chamomile/components/timer.rb', line 69 def timed_out? @remaining <= 0 end |
#toggle ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/chamomile/components/timer.rb', line 49 def toggle if @running stop nil else start_cmd end end |
#view ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/chamomile/components/timer.rb', line 92 def view total = @remaining.ceil.to_i total = 0 if total.negative? hours = total / 3600 minutes = (total % 3600) / 60 seconds = total % 60 if hours.positive? format("%d:%02d:%02d", hours, minutes, seconds) else format("%02d:%02d", minutes, seconds) end end |