Class: ActionTimer::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/actiontimer/Action.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timer, period, once = false, data = nil, &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



11
12
13
14
15
16
17
18
19
20
# File 'lib/actiontimer/Action.rb', line 11

def initialize(timer, period, once=false, data=nil, &block)
    @period = period.to_f
    @block = block
    @data = data
    @once = once
    @timer = timer
    @completed = false
    @wait_remaining = @period
    @owner = nil
end

Instance Attribute Details

#ownerObject

Returns the value of attribute owner.



4
5
6
# File 'lib/actiontimer/Action.rb', line 4

def owner
  @owner
end

Instance Method Details

#due?Boolean

Is action due for execution

Returns:

  • (Boolean)


64
65
66
# File 'lib/actiontimer/Action.rb', line 64

def due?
    @wait_remaining <= 0
end

#is_complete?Boolean

Action is ready to be destroyed

Returns:

  • (Boolean)


52
53
54
# File 'lib/actiontimer/Action.rb', line 52

def is_complete?
    @completed
end

#remainingObject

Time remaning before Action is due



39
40
41
# File 'lib/actiontimer/Action.rb', line 39

def remaining
    @wait_remaining <= 0 ? 0 : @wait_remaining
end

#reset_period(new_time) ⇒ Object

new_time

new period

Resets the wait period between runs



45
46
47
48
49
# File 'lib/actiontimer/Action.rb', line 45

def reset_period(new_time)
    @period = new_time.to_f
    @wait_remaining = @period
    @timer.wakeup
end

#runObject

Run the action



69
70
71
# File 'lib/actiontimer/Action.rb', line 69

def run
    @data.nil? ? @block.call : @block.call(*@data)
end

#scheduleObject

Used for scheduling with Timer. Resets the interval and returns itself



58
59
60
61
# File 'lib/actiontimer/Action.rb', line 58

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



32
33
34
35
36
# File 'lib/actiontimer/Action.rb', line 32

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