Class: ModSpox::Action
- Inherits:
-
Object
- Object
- ModSpox::Action
- Defined in:
- lib/mod_spox/Action.rb
Instance Method Summary collapse
-
#due? ⇒ Boolean
Returns true if action is due to run.
-
#initialize(timer, period, data = nil, once = false, &func) ⇒ Action
constructor
- timer
- Timer the action is being added to period
- number of seconds between runs data
- data to be available for the action once
- only run the action once &func
-
block of code to run Create a new Action.
-
#is_complete? ⇒ Boolean
Returns if the Action has completed all its runs.
-
#remaining ⇒ Object
Returns the remaining number of seconds.
-
#reset_period(new_time) ⇒ Object
- new_time
-
number of seconds between runs Resets the wait time between runs.
-
#run ⇒ Object
Runs the function block of the action.
-
#schedule ⇒ Object
Used for scheduling with Timer.
-
#tick(amount) ⇒ Object
- amount
-
number of seconds passed Decrement wait time by given number of seconds.
Constructor Details
#initialize(timer, period, data = nil, once = false, &func) ⇒ Action
- timer
-
Timer the action is being added to
- period
-
number of seconds between runs
- data
-
data to be available for the action
- once
-
only run the action once
- &func
-
block of code to run
Create a new Action
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/mod_spox/Action.rb', line 13 def initialize(timer, period, data=nil, once=false, &func) @period = period.to_f @func = func @data = data @once = once @due = false @timer = timer @completed = false @wait_remaining = @period end |
Instance Method Details
#due? ⇒ Boolean
Returns true if action is due to run
31 32 33 |
# File 'lib/mod_spox/Action.rb', line 31 def due? @wait_remaining <= 0 end |
#is_complete? ⇒ Boolean
Returns if the Action has completed all its runs
49 50 51 |
# File 'lib/mod_spox/Action.rb', line 49 def is_complete? @completed end |
#remaining ⇒ Object
Returns the remaining number of seconds
36 37 38 |
# File 'lib/mod_spox/Action.rb', line 36 def remaining @wait_remaining <= 0 ? 0 : @wait_remaining end |
#reset_period(new_time) ⇒ Object
- new_time
-
number of seconds between runs
Resets the wait time between runs
42 43 44 45 46 |
# File 'lib/mod_spox/Action.rb', line 42 def reset_period(new_time) @period = new_time.to_f @wait_remaining = @period @timer.wakeup end |
#run ⇒ Object
Runs the function block of the action
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/mod_spox/Action.rb', line 61 def run begin unless @data.nil? @func.call(@data) else @func.call end rescue Object => boom Logger.log("Action generated an exception during run: #{boom}\n#{boom.backtrace.join("\n")}", 10) end @completed = true if @once end |
#schedule ⇒ Object
Used for scheduling with Timer. Resets its internal timer and returns itself
55 56 57 58 |
# File 'lib/mod_spox/Action.rb', line 55 def schedule @wait_remaining = @period return self end |
#tick(amount) ⇒ Object
- amount
-
number of seconds passed
Decrement wait time by given number of seconds
26 27 28 |
# File 'lib/mod_spox/Action.rb', line 26 def tick(amount) @wait_remaining = @wait_remaining - amount end |