Class: Zoidberg::Timer::Action
- Inherits:
-
Object
- Object
- Zoidberg::Timer::Action
- Defined in:
- lib/zoidberg/timer.rb
Instance Attribute Summary collapse
-
#action ⇒ Proc
readonly
Action to run.
- #interval ⇒ Numeric readonly
- #last_run ⇒ Float readonly
- #recur ⇒ TrueClass, FalseClass readonly
- #waker ⇒ IO readonly
Instance Method Summary collapse
-
#cancel ⇒ TrueClass
Cancel the action.
- #cancelled? ⇒ TrueClass, FalseClass
-
#initialize(args = {}, &block) ⇒ self
constructor
Create a new action.
-
#run! ⇒ self
Run the action.
Constructor Details
#initialize(args = {}, &block) ⇒ self
Create a new action
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/zoidberg/timer.rb', line 27 def initialize(args={}, &block) unless(block) raise ArgumentError.new 'Action is required. Block must be provided!' end @action = block @recur = args.fetch(:recur, false) @interval = args.fetch(:interval, 5) @last_run = Time.now.to_f @cancel = false @waker = IO.pipe.last end |
Instance Attribute Details
#action ⇒ Proc (readonly)
Returns action to run.
13 14 15 |
# File 'lib/zoidberg/timer.rb', line 13 def action @action end |
#interval ⇒ Numeric (readonly)
15 16 17 |
# File 'lib/zoidberg/timer.rb', line 15 def interval @interval end |
#last_run ⇒ Float (readonly)
17 18 19 |
# File 'lib/zoidberg/timer.rb', line 17 def last_run @last_run end |
#recur ⇒ TrueClass, FalseClass (readonly)
11 12 13 |
# File 'lib/zoidberg/timer.rb', line 11 def recur @recur end |
#waker ⇒ IO (readonly)
19 20 21 |
# File 'lib/zoidberg/timer.rb', line 19 def waker @waker end |
Instance Method Details
#cancel ⇒ TrueClass
Cancel the action
42 43 44 45 |
# File 'lib/zoidberg/timer.rb', line 42 def cancel @recur = false @cancel = true end |
#cancelled? ⇒ TrueClass, FalseClass
48 49 50 |
# File 'lib/zoidberg/timer.rb', line 48 def cancelled? @cancel end |
#run! ⇒ self
Run the action
55 56 57 58 59 60 |
# File 'lib/zoidberg/timer.rb', line 55 def run! @last_run = Time.now.to_f action.call @last_run = Time.now.to_f self end |