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
Instance Method Summary collapse
-
#cancel ⇒ TrueClass
Cancel the action.
- #cancelled? ⇒ TrueClass, FalseClass (also: #canceled?)
-
#initialize(args = {}, &block) ⇒ self
constructor
Create a new action.
- #ready? ⇒ TrueClass, FalseClass
-
#run! ⇒ self
Run the action.
Constructor Details
#initialize(args = {}, &block) ⇒ self
Create a new action
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/zoidberg/timer.rb', line 25 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 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 |
Instance Method Details
#cancel ⇒ TrueClass
Cancel the action
39 40 41 42 |
# File 'lib/zoidberg/timer.rb', line 39 def cancel @recur = false @cancel = true end |
#cancelled? ⇒ TrueClass, FalseClass Also known as: canceled?
45 46 47 |
# File 'lib/zoidberg/timer.rb', line 45 def cancelled? @cancel end |
#ready? ⇒ TrueClass, FalseClass
63 64 65 66 67 |
# File 'lib/zoidberg/timer.rb', line 63 def ready? Time.now.to_f > ( last_run + interval ) end |
#run! ⇒ self
Run the action
53 54 55 56 57 58 59 60 |
# File 'lib/zoidberg/timer.rb', line 53 def run! unless(cancelled?) @last_run = Time.now.to_f action.call @last_run = Time.now.to_f end self end |