Class: Ruck::EventClock
- Inherits:
-
Object
- Object
- Ruck::EventClock
- Defined in:
- lib/ruck/event_clock.rb
Instance Attribute Summary collapse
-
#now ⇒ Object
readonly
Returns the value of attribute now.
Instance Method Summary collapse
-
#fast_forward(dt) ⇒ Object
fast-forward this clock by the given time delta.
-
#initialize ⇒ EventClock
constructor
A new instance of EventClock.
- #next ⇒ Object
- #raise_all(event) ⇒ Object
- #schedule(obj, event = nil) ⇒ Object
- #unschedule(obj) ⇒ Object
- #unschedule_next ⇒ Object
Constructor Details
#initialize ⇒ EventClock
Returns a new instance of EventClock.
6 7 8 9 10 |
# File 'lib/ruck/event_clock.rb', line 6 def initialize @now = 0 @waiting = Hash.new { |hash, event| hash[event] = [] } @raised = [] end |
Instance Attribute Details
#now ⇒ Object (readonly)
Returns the value of attribute now.
4 5 6 |
# File 'lib/ruck/event_clock.rb', line 4 def now @now end |
Instance Method Details
#fast_forward(dt) ⇒ Object
fast-forward this clock by the given time delta
13 14 15 |
# File 'lib/ruck/event_clock.rb', line 13 def fast_forward(dt) @now += dt end |
#next ⇒ Object
26 27 28 |
# File 'lib/ruck/event_clock.rb', line 26 def next [@raised.first, 0] if @raised.length > 0 end |
#raise_all(event) ⇒ Object
34 35 36 37 |
# File 'lib/ruck/event_clock.rb', line 34 def raise_all(event) @raised += @waiting[event] @waiting[event].clear end |
#schedule(obj, event = nil) ⇒ Object
17 18 19 |
# File 'lib/ruck/event_clock.rb', line 17 def schedule(obj, event = nil) @waiting[event] << obj end |
#unschedule(obj) ⇒ Object
21 22 23 24 |
# File 'lib/ruck/event_clock.rb', line 21 def unschedule(obj) @waiting.each { |event, objs| objs.delete(obj) } @raised.delete(obj) end |
#unschedule_next ⇒ Object
30 31 32 |
# File 'lib/ruck/event_clock.rb', line 30 def unschedule_next [@raised.shift, 0] if @raised.length > 0 end |