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.
- #owner ⇒ Object
- #owner=(o) ⇒ Object
-
#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 23 |
# 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 @owner = nil end |
Instance Method Details
#due? ⇒ Boolean
Returns true if action is due to run
49 50 51 |
# File 'lib/mod_spox/Action.rb', line 49 def due? @wait_remaining <= 0 end |
#is_complete? ⇒ Boolean
Returns if the Action has completed all its runs
67 68 69 |
# File 'lib/mod_spox/Action.rb', line 67 def is_complete? @completed end |
#owner ⇒ Object
36 37 38 |
# File 'lib/mod_spox/Action.rb', line 36 def owner @owner end |
#owner=(o) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/mod_spox/Action.rb', line 25 def owner=(o) unless(o.is_a?(Symbol)) if(o.is_a?(ModSpox::Plugin)) o = o.name.to_sym else raise Exceptions::BotException.new('Unsupported type given for owner') end end @owner = o end |
#remaining ⇒ Object
Returns the remaining number of seconds
54 55 56 |
# File 'lib/mod_spox/Action.rb', line 54 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
60 61 62 63 64 |
# File 'lib/mod_spox/Action.rb', line 60 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
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/mod_spox/Action.rb', line 79 def run begin unless @data.nil? @func.call(@data) else @func.call end rescue Object => boom Logger.warn("Action generated an exception during run: #{boom}\n#{boom.backtrace.join("\n")}") end end |
#schedule ⇒ Object
Used for scheduling with Timer. Resets its internal timer and returns itself
73 74 75 76 |
# File 'lib/mod_spox/Action.rb', line 73 def schedule @wait_remaining = @period return self end |
#tick(amount) ⇒ Object
- amount
-
number of seconds passed
Decrement wait time by given number of seconds
42 43 44 45 46 |
# File 'lib/mod_spox/Action.rb', line 42 def tick(amount) @wait_remaining = @wait_remaining - amount if @wait_remaining > 0 @wait_remaining = 0 if @wait_remaining < 0 @completed = true if @once && @wait_remaining <= 0 end |