This repository has been archived by the owner on Apr 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.rb
81 lines (67 loc) · 1.97 KB
/
plugin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Implements the base requirements for wrack plugins.
#
# Performs automatic registration so that all available plugins can be
# automatically accessed.
#
# Generates the necessary functionality for other classes to instantiate
# plugin instances.
require 'wrack/irc'
require 'wrack/receiver'
require 'wrack/pluginloader'
module Wrack
# Defines the class methods that backs plugin generated
#
# This is necessary to make the class level DSL function
module PluginBase
def receive(restrictions = {}, &block)
bare_receivers << {:restrictions => restrictions, :block => block}
end
def on_initialize(&block)
initializers << block
end
def bare_receivers
@bare_receivers ||= []
end
def initializers
@initializers ||= []
end
end
module Plugin
include Wrack::IRC::Commands
class << self
# Modify all plugins to include the class level methods, and register
# them for subsequent lookup
def included(klass)
klass.extend Wrack::PluginBase
@klasses ||= []
@klasses << klass unless @klasses.include? klass
end
# Expose all registered plugins
def registered
@klasses.dup
end
def loader
@loader ||= Wrack::PluginLoader.new
end
end
attr_accessor :receivers
attr_accessor :connection
attr_accessor :bot
# Defines a default constructor to copy all receivers generated during
# class instantiation into the object
def initialize(connection, bot, restrictions = {})
@connection = connection
@bot = bot
@receivers = []
# Instantiate all class level receivers for this instance
self.class.bare_receivers.each do |r|
receiver = Wrack::Receiver.new(self, r[:restrictions].merge(restrictions), &r[:block])
@receivers << receiver
end
# Evaluate all instance blocks
self.class.initializers.each do |block|
instance_exec &block
end
end
end
end