Module: Cinch::Plugin::ClassMethods
- Defined in:
- lib/cinch/plugin.rb
Overview
The ClassMethods module includes all methods that the user will need for creating plugins for the Cinch framework: Setting options (see #set and the attributes) as well as methods for configuring the actual pattern matching (#match, #listen_to).
Furthermore, the attributes allow for programmatically inspecting plugins.
Defined Under Namespace
Classes: Hook, Listener, Matcher, Timer
Instance Attribute Summary collapse
-
#ctcps ⇒ Array<String>
readonly
All CTCPs.
-
#help ⇒ String?
The help message.
-
#hooks ⇒ Hash{:pre, :post => Array<Hook>}
readonly
All hooks.
-
#listeners ⇒ Array<Listener>
readonly
All listeners.
-
#matchers ⇒ Array<Matcher>
readonly
All matchers.
-
#plugin_name ⇒ String?
The name of the plugin.
-
#prefix ⇒ String, ...
The prefix.
-
#react_on ⇒ Array<:message, :channel, :private>
The list of events to react on.
-
#required_options ⇒ Array<Symbol>
Required plugin options.
-
#suffix ⇒ String, ...
The suffix.
-
#timers ⇒ Array<Timer>
readonly
All timers.
Instance Method Summary collapse
-
#__hooks(type = nil, events = nil, group = nil) ⇒ Hash
private
-
#call_hooks(type, event, group, instance, args) ⇒ Boolean
private
True if processing should continue.
-
#check_for_missing_options(bot) ⇒ Array<Symbol>?
private
-
#ctcp(command) ⇒ Object
-
#hook(type, options = {}) ⇒ Hook
Defines a hook which will be run before or after a handler is executed, depending on the value of
type
. -
#listen_to(*types, options = {}) ⇒ Array<Listener>
Events to listen to.
-
#match(pattern, options = {}) ⇒ Matcher
Set a match pattern.
-
#set(*args)
Set options.
-
#timer(interval, options = {}) ⇒ Timer
Instance Attribute Details
#ctcps ⇒ Array<String> (readonly)
Returns All CTCPs.
62 63 64 |
# File 'lib/cinch/plugin.rb', line 62 def ctcps @ctcps end |
#help ⇒ String?
Returns The help message.
65 66 67 |
# File 'lib/cinch/plugin.rb', line 65 def help @help end |
#hooks ⇒ Hash{:pre, :post => Array<Hook>} (readonly)
Returns All hooks.
29 30 31 |
# File 'lib/cinch/plugin.rb', line 29 def hooks @hooks end |
#listeners ⇒ Array<Listener> (readonly)
Returns All listeners.
56 57 58 |
# File 'lib/cinch/plugin.rb', line 56 def listeners @listeners end |
#matchers ⇒ Array<Matcher> (readonly)
Returns All matchers.
53 54 55 |
# File 'lib/cinch/plugin.rb', line 53 def matchers @matchers end |
#plugin_name ⇒ String? #plugin_name=(new_name) ⇒ String
The name of the plugin.
41 42 43 |
# File 'lib/cinch/plugin.rb', line 41 def plugin_name @plugin_name end |
#prefix ⇒ String, ...
Returns The prefix.
68 69 70 |
# File 'lib/cinch/plugin.rb', line 68 def prefix @prefix end |
#react_on ⇒ Array<:message, :channel, :private>
Returns The list of events to react on.
32 33 34 |
# File 'lib/cinch/plugin.rb', line 32 def react_on @react_on end |
#required_options ⇒ Array<Symbol>
Returns Required plugin options.
74 75 76 |
# File 'lib/cinch/plugin.rb', line 74 def @required_options end |
#suffix ⇒ String, ...
Returns The suffix.
71 72 73 |
# File 'lib/cinch/plugin.rb', line 71 def suffix @suffix end |
#timers ⇒ Array<Timer> (readonly)
Returns All timers.
59 60 61 |
# File 'lib/cinch/plugin.rb', line 59 def timers @timers end |
Instance Method Details
#__hooks(type = nil, events = nil, group = nil) ⇒ Hash
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.
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/cinch/plugin.rb', line 299 def __hooks(type = nil, events = nil, group = nil) if type.nil? hooks = @hooks else hooks = @hooks[type] end if events.nil? return hooks else events = [*events] if hooks.is_a?(Hash) hooks = hooks.map { |k, v| v } end hooks = hooks.select { |hook| (events & hook.for).size > 0 } end return hooks.select { |hook| hook.group.nil? || hook.group == group } end |
#call_hooks(type, event, group, instance, args) ⇒ Boolean
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.
Returns True if processing should continue.
321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/cinch/plugin.rb', line 321 def call_hooks(type, event, group, instance, args) stop = __hooks(type, event, group).find { |hook| # stop after the first hook that returns false if hook.method.respond_to?(:call) instance.instance_exec(*args, &hook.method) == false else instance.__send__(hook.method, *args) == false end } !stop end |
#check_for_missing_options(bot) ⇒ Array<Symbol>?
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.
338 339 340 341 342 |
# File 'lib/cinch/plugin.rb', line 338 def (bot) @required_options.select { |option| !bot.config.plugins.[self].has_key?(option) } end |
#ctcp(command) ⇒ Object
244 245 246 |
# File 'lib/cinch/plugin.rb', line 244 def ctcp(command) @ctcps << command.to_s.upcase end |
#hook(type, options = {}) ⇒ Hook
Defines a hook which will be run before or after a handler is
executed, depending on the value of type
.
289 290 291 292 293 294 295 |
# File 'lib/cinch/plugin.rb', line 289 def hook(type, = {}) = {:for => [:match, :listen_to, :ctcp], :method => :hook, :group => nil}.merge() hook = Hook.new(type, [:for], [:method], [:group]) __hooks(type) << hook hook end |
#listen_to(*types, options = {}) ⇒ Array<Listener>
Events to listen to.
231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/cinch/plugin.rb', line 231 def listen_to(*types) = {:method => :listen} if types.last.is_a?(Hash) .merge!(types.pop) end listeners = types.map {|type| Listener.new(type.to_s.to_sym, [:method])} @listeners.concat listeners listeners end |
#match(pattern, options = {}) ⇒ Matcher
Document match/listener grouping
Set a match pattern.
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/cinch/plugin.rb', line 195 def match(pattern, = {}) = { :use_prefix => true, :use_suffix => true, :method => :execute, :group => nil, :prefix => nil, :suffix => nil, :react_on => nil, :strip_colors => false, }.merge() if [:react_on] [:react_on] = [:react_on].to_s.to_sym end matcher = Matcher.new(pattern, *.values_at(:use_prefix, :use_suffix, :method, :group, :prefix, :suffix, :react_on, :strip_colors)) @matchers << matcher matcher end |
#set(key, value) #set(options)
This method returns an undefined value.
Set options.
Available options:
159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/cinch/plugin.rb', line 159 def set(*args) case args.size when 1 # {:key => value, ...} args.first.each do |key, value| self.send("#{key}=", value) end when 2 # key, value self.send("#{args.first}=", args.last) else raise ArgumentError # TODO proper error message end end |