Class: Timer::Action

Inherits:
Object show all
Defined in:
lib/rbot/timer.rb

Overview

class representing individual timed action

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Action

Options are:

start

Time when the Action should be run for the first time. Repeatable Actions will be repeated after that, see :period. One-time Actions will not (obviously) Default: Time.now + :period

period

How often repeatable Action should be run, in seconds. Default: 1

blocked

if true, Action starts as blocked (i.e. will stay dormant until unblocked)

args

Arguments to pass to the Action callback. Default: []

repeat

Should the Action be called repeatedly? Default: false

code

You can specify the Action body using &block, or using this option.



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rbot/timer.rb', line 43

def initialize(options = {}, &block)
  opts = {
    :period => 1,
    :blocked => false,
    :args => [],
    :repeat => false
  }.merge(options)

  @block = nil
  debug("adding timer #{self} :period => #{opts[:period]}, :repeat => #{opts[:repeat].inspect}")
  self.configure(opts, &block)
  debug("added #{self}")
end

Instance Attribute Details

#nextObject

Time when the Action should be called next



27
28
29
# File 'lib/rbot/timer.rb', line 27

def next
  @next
end

Instance Method Details

#blockObject

blocks an Action, so it won’t be run



88
89
90
# File 'lib/rbot/timer.rb', line 88

def block
  @blocked = true
end

#blocked?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/rbot/timer.rb', line 97

def blocked?
  @blocked
end

#configure(opts = {}, &block) ⇒ Object

Provides for on-the-fly reconfiguration of the Actions Accept the same arguments as the constructor



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rbot/timer.rb', line 59

def configure(opts = {}, &block)
  @period = opts[:period] if opts.include? :period
  @blocked = opts[:blocked] if opts.include? :blocked
  @repeat = opts[:repeat] if opts.include? :repeat

  if block_given?
    @block = block
  elsif opts[:code]
    @block = opts[:code]
  end

  raise 'huh?? blockless action?' unless @block
  if opts.include? :args
    @args = Array === opts[:args] ? opts[:args] : [opts[:args]]
  end

  if opts[:start] and (Time === opts[:start])
    self.next = opts[:start]
  else
    self.next = Time.now + (opts[:start] || @period)
  end
end

#reschedule(period, &block) ⇒ Object

modify the Action period



83
84
85
# File 'lib/rbot/timer.rb', line 83

def reschedule(period, &block)
  self.configure(:period => period, &block)
end

#run(now = Time.now) ⇒ Object

calls the Action callback, resets .next to the Time of the next call, if the Action is repeatable.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rbot/timer.rb', line 103

def run(now = Time.now)
  raise 'inappropriate time to run()' unless self.next && self.next <= now
  self.next = nil
  begin
    @block.call(*@args)
  rescue Exception => e
    error "Timer action #{self.inspect}: block #{@block.inspect} failed!"
    error e.pretty_inspect
    debug e.backtrace.join("\n")
  end

  if @repeat && @period > 0
    self.next = now + @period
  end

  return self.next
end

#unblockObject

unblocks a blocked Action



93
94
95
# File 'lib/rbot/timer.rb', line 93

def unblock
  @blocked = false
end