Class: ActionTimer::Action
- Inherits:
-
Object
- Object
- ActionTimer::Action
- Defined in:
- lib/actiontimer/Action.rb
Instance Attribute Summary collapse
-
#owner ⇒ Object
Returns the value of attribute owner.
-
#timer ⇒ Object
Returns the value of attribute timer.
Instance Method Summary collapse
-
#due? ⇒ Boolean
Is action due for execution.
-
#initialize(hash, &block) ⇒ Action
constructor
- timer
- Timer this action resides within period
- amount of time between runs once
- only run this action once data
- data to pass to block block
-
block to be executed.
-
#is_complete? ⇒ Boolean
Action is ready to be destroyed.
-
#remaining ⇒ Object
Time remaning before Action is due.
-
#reset_period(new_time) ⇒ Object
- new_time
-
new period Resets the wait period between runs.
-
#run ⇒ Object
Run the action.
-
#schedule ⇒ Object
Used for scheduling with Timer.
-
#tick(amount) ⇒ Object
- amount
-
amount of time that has passed Decrement remaining wait time by given amount.
Constructor Details
#initialize(hash, &block) ⇒ Action
- timer
-
Timer this action resides within
- period
-
amount of time between runs
- once
-
only run this action once
- data
-
data to pass to block
- block
-
block to be executed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/actiontimer/Action.rb', line 12 def initialize(hash, &block) raise ArgumentError.new('Period must be supplied') unless hash[:period] raise ArgumentError.new('Block must be provided') unless block_given? raise ArgumentError.new('Block must accept data value') if hash[:data] && block.arity == 0 if((block.arity > 0 || block.arity < -1) && (!hash.has_key?(:data) || hash[:data].nil?)) raise ArgumentError.new('Data must be supplied for block') end args = {:once => false, :data => nil, :owner => nil}.merge(hash) @period = args[:period].to_f @block = block @data = args[:data] @once = args[:once] @timer = args[:timer] @completed = false @wait_remaining = @period @owner = args[:owner] end |
Instance Attribute Details
#owner ⇒ Object
Returns the value of attribute owner.
4 5 6 |
# File 'lib/actiontimer/Action.rb', line 4 def owner @owner end |
#timer ⇒ Object
Returns the value of attribute timer.
5 6 7 |
# File 'lib/actiontimer/Action.rb', line 5 def timer @timer end |
Instance Method Details
#due? ⇒ Boolean
Is action due for execution
82 83 84 |
# File 'lib/actiontimer/Action.rb', line 82 def due? @wait_remaining <= 0 end |
#is_complete? ⇒ Boolean
Action is ready to be destroyed
70 71 72 |
# File 'lib/actiontimer/Action.rb', line 70 def is_complete? @completed end |
#remaining ⇒ Object
Time remaning before Action is due
56 57 58 |
# File 'lib/actiontimer/Action.rb', line 56 def remaining @wait_remaining <= 0 ? 0 : @wait_remaining end |
#reset_period(new_time) ⇒ Object
- new_time
-
new period
Resets the wait period between runs
62 63 64 65 66 67 |
# File 'lib/actiontimer/Action.rb', line 62 def reset_period(new_time) @period = new_time.to_f @wait_remaining = @period @completed = false @timer.wakeup unless @timer.nil? end |
#run ⇒ Object
Run the action
87 88 89 |
# File 'lib/actiontimer/Action.rb', line 87 def run @data.nil? ? @block.call : @block.call(*@data) end |
#schedule ⇒ Object
Used for scheduling with Timer. Resets the interval and returns itself
76 77 78 79 |
# File 'lib/actiontimer/Action.rb', line 76 def schedule @wait_remaining = @period return self end |
#tick(amount) ⇒ Object
- amount
-
amount of time that has passed
Decrement remaining wait time by given amount
47 48 49 50 51 52 53 |
# File 'lib/actiontimer/Action.rb', line 47 def tick(amount) amount = amount.to_f amount = 0 if amount < 0 @wait_remaining = @wait_remaining - amount if @wait_remaining > 0 @wait_remaining = 0 if @wait_remaining < 0 @completed = true if @once && @wait_remaining <= 0 end |