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(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

Raises:

  • (ArgumentError)


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

#ownerObject

Returns the value of attribute owner.



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

def owner
  @owner
end

#timerObject

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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

def is_complete?
    @completed
end

#remainingObject

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

#runObject

Run the action



87
88
89
# File 'lib/actiontimer/Action.rb', line 87

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

#scheduleObject

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