Class: FSM::TimedEvent
- Inherits:
-
Object
- Object
- FSM::TimedEvent
- Defined in:
- lib/pretty-fsm/timed_event.rb
Overview
This class is useful to “register” actions to be performed after certain time. You would normally use this in a FSM state, to switch to another state after a while. The notion of ‘time’ (the now and duration parameters) can be arbitrary, it could be any number for example (as long as it makes sense).
Instance Attribute Summary collapse
-
#duration ⇒ Object
Returns the value of attribute duration.
Instance Method Summary collapse
-
#initialize(duration) ⇒ TimedEvent
constructor
Create a TimedEvent that will fire after duration seconds (can be a fractional number) starting from now.
-
#reset(now = Time.now) ⇒ Object
Resets the timer, to fire after #duration seconds after now.
-
#try(now = Time.now) ⇒ Object
This method should be called periodically with a block.
Constructor Details
#initialize(duration) ⇒ TimedEvent
Create a TimedEvent that will fire after duration seconds (can be a fractional number) starting from now.
9 10 11 12 |
# File 'lib/pretty-fsm/timed_event.rb', line 9 def initialize(duration) self.reset @duration = duration end |
Instance Attribute Details
#duration ⇒ Object
Returns the value of attribute duration.
6 7 8 |
# File 'lib/pretty-fsm/timed_event.rb', line 6 def duration @duration end |
Instance Method Details
#reset(now = Time.now) ⇒ Object
Resets the timer, to fire after #duration seconds after now
15 16 17 |
# File 'lib/pretty-fsm/timed_event.rb', line 15 def reset(now = Time.now) @time = now end |
#try(now = Time.now) ⇒ Object
This method should be called periodically with a block. If #duration seconds have passed since last reset, it will call the passed block.
21 22 23 24 25 26 |
# File 'lib/pretty-fsm/timed_event.rb', line 21 def try(now = Time.now) if ((now - @time) >= @duration) yield self.reset end end |