Method: Cinch::Timer#initialize

Defined in:
lib/cinch/timer.rb

#initialize(bot, options, &block) ⇒ Timer

Returns a new instance of Timer.

Parameters:

  • bot (Bot)

    The instance of Bot the timer is associated with

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • :interval (Numeric)

    The interval (in seconds) of the timer

  • :shots (Integer) — default: Float::INFINITY

    How often should the timer fire?

  • :threaded (Boolean) — default: true

    If true, each invocation will be executed in a thread of its own.

  • :start_automatically (Boolean) — default: true

    If true, the timer will automatically start after the bot finished connecting.

  • :stop_automaticall (Boolean) — default: true

    If true, the timer will automatically stop when the bot disconnects.

Since:

  • 2.0.0



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/cinch/timer.rb', line 64

def initialize(bot, options, &block)
  options = {:threaded => true, :shots => Float::INFINITY, :start_automatically => true, :stop_automatically => true}.merge(options)

  @bot        = bot
  @interval   = options[:interval].to_f
  @threaded   = options[:threaded]
  @orig_shots = options[:shots]
  # Setting @shots here so the attr_reader won't return nil
  @shots      = @orig_shots
  @block      = block

  @started = false
  @thread_group = ThreadGroup.new

  if options[:start_automatically]
    @bot.on :connect, //, self do |m, timer|
      timer.start
    end
  end

  if options[:stop_automatically]
    @bot.on :disconnect, //, self do |m, timer|
      timer.stop
    end
  end
end