Module: Cinch::Plugin

Includes:
Helpers
Defined in:
lib/cinch/plugin.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#Channel, #User

Instance Attribute Details

#botBot (readonly)

Returns:



264
265
266
# File 'lib/cinch/plugin.rb', line 264

def bot
  @bot
end

Class Method Details

.included(by) ⇒ Object



301
302
303
# File 'lib/cinch/plugin.rb', line 301

def self.included(by)
  by.extend ClassMethods
end

Instance Method Details

#configHash

Provides access to plugin-specific options.

Returns:

  • (Hash)

    A hash of options



297
298
299
# File 'lib/cinch/plugin.rb', line 297

def config
  @bot.config.plugins.options[self.class] || {}
end

#execute(*args) ⇒ void

This method is abstract.

This method returns an undefined value.

This method will be executed whenever a message matches the match pattern of the plugin.



291
292
# File 'lib/cinch/plugin.rb', line 291

def execute(*args)
end

#initialize(bot) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



266
267
268
269
# File 'lib/cinch/plugin.rb', line 266

def initialize(bot)
  @bot = bot
  self.class.__register_with_bot(bot, self)
end

#listen(*args) ⇒ void

This method is abstract.

This method returns an undefined value.

This method will be executed whenever an event the plugin listens to occurs.



282
283
# File 'lib/cinch/plugin.rb', line 282

def listen(*args)
end

#synchronize(*args) { ... } ⇒ void

This method returns an undefined value.

Since Cinch uses threads, all handlers can be run simultaneously, even the same handler multiple times. This also means, that your code has to be thread-safe. Most of the time, this is not a problem, but if you are accessing stored data, you will most likely have to synchronize access to it. Instead of managing all mutexes yourself, Cinch provides a synchronize method, which takes a name and block.

Synchronize blocks with the same name share the same mutex, which means that only one of them will be executed at a time.

Examples:

configure do |c|
  
  @i = 0
end

on :channel, /^start counting!/ do
  synchronize(:my_counter) do
    10.times do
      val = @i
      # at this point, another thread might've incremented :i already.
      # this thread wouldn't know about it, though.
      @i = val + 1
    end
  end
end

Parameters:

  • name (String, Symbol)

    a name for the synchronize block.

Yields:



272
273
274
# File 'lib/cinch/plugin.rb', line 272

def synchronize(*args, &block)
  @bot.synchronize(*args, &block)
end