Class: Ircbot::Plugins

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/ircbot/plugins.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client = nil, plugins = nil) ⇒ Plugins

Returns a new instance of Plugins.



11
12
13
14
15
16
17
# File 'lib/ircbot/plugins.rb', line 11

def initialize(client = nil, plugins = nil)
  @client  = client  || Client::Standalone.new
  @plugins = Dictionary.new
  @cache   = {}

  load Array(plugins)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



8
9
10
# File 'lib/ircbot/plugins.rb', line 8

def client
  @client
end

#pluginsObject (readonly)

Returns the value of attribute plugins.



7
8
9
# File 'lib/ircbot/plugins.rb', line 7

def plugins
  @plugins
end

Instance Method Details

#activeObject



53
54
55
# File 'lib/ircbot/plugins.rb', line 53

def active
  cache(:active) { select(&:running) }
end

#active_namesObject

Accessors



49
50
51
# File 'lib/ircbot/plugins.rb', line 49

def active_names
  cache(:active_names) { active.map(&:plugin_name) }
end

#botObject



73
74
75
# File 'lib/ircbot/plugins.rb', line 73

def bot
  client
end

#cache(key = nil, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/ircbot/plugins.rb', line 35

def cache(key = nil, &block)
  key = key.to_s
  return @cache ||= {} if key.empty?

  if @cache.has_key?(key)
    @cache[key]
  else
    @cache[key] = block.call
  end
end

#changed!Object



97
98
99
# File 'lib/ircbot/plugins.rb', line 97

def changed!
  clear_cache
end

#clear_cacheObject

Caching



31
32
33
# File 'lib/ircbot/plugins.rb', line 31

def clear_cache
  @cache = {}
end

#delete(plugin) ⇒ Object



90
91
92
93
94
95
# File 'lib/ircbot/plugins.rb', line 90

def delete(plugin)
  name = plugin.is_a?(Plugin) ? plugin.name : plugin.to_s
  plugin!(name)
  @plugins.delete(name)
  changed!
end

#each(&block) ⇒ Object



24
25
26
# File 'lib/ircbot/plugins.rb', line 24

def each(&block)
  plugins.values.__send__(:each, &block)
end

#eval_plugin(name) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/ircbot/plugins.rb', line 144

def eval_plugin(name)
  class_name = plugin_class_name(name)

  if Object.const_defined?(class_name)
    Object.send(:remove_const, class_name)
  end

  path = Ircbot.glob_for(:plugin, name).first or
    raise PluginNotFound, name.to_s

  script = path.read{}
  Dir.chdir(path.parent) do
    eval(script, Ircbot.toplevel_binding)
  end

  return Object.const_get(class_name).new

rescue NameError => e
  raise LoadError, "Expected #{path} to define #{class_name} (#{e})"
end

#inspectObject



165
166
167
# File 'lib/ircbot/plugins.rb', line 165

def inspect
  plugins.values.inspect
end

#invalid_plugin_found(text) ⇒ Object



169
170
171
# File 'lib/ircbot/plugins.rb', line 169

def invalid_plugin_found(text)
  broadcast text
end

#load(plugin) ⇒ Object Also known as: <<

IO



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/ircbot/plugins.rb', line 104

def load(plugin)
  case plugin
  when Array
    plugin.each do |name|
      self << name
    end
  when Plugin
    plugin.plugins = self
    plugins[plugin.plugin_name] = plugin
    plugin.running = true
    plugin.setup
  when Class
    if plugin.ancestors.include?(Ircbot::Plugin)
      self << plugin.new(self)
    else
      raise ArgumentError, "#{plugin} is not Ircbot::Plugin"
    end
  when String, Symbol
    self << {"name" => plugin.to_s}
  when Hash
    begin
      attrs = Mash.new(plugin)
      name  = attrs["name"]
      if name.blank?
        raise "no names: #{attrs.inspect}"
      end
      plugin = eval_plugin(name)
      plugin.attrs = attrs
      self << plugin
    rescue Exception => e
      invalid_plugin_found "Plugin error(#{name}): #{e}[#{e.class}]"
    end
  else
    raise NotImplementedError, "#<< for #{plugin.class}"
  end
  changed!
  return self
end

#plugin(name) ⇒ Object



69
70
71
# File 'lib/ircbot/plugins.rb', line 69

def plugin(name)
  plugin!(name) rescue nil
end

#plugin!(plugin) ⇒ Object Also known as: []



57
58
59
60
61
62
63
64
65
66
# File 'lib/ircbot/plugins.rb', line 57

def plugin!(plugin)
  case plugin
  when String, Symbol
    @plugins[plugin.to_s] or raise PluginNotFound, plugin.to_s
  when Plugin
    plugin
  else
    raise PluginNotFound, "#{plugin.class}(#{plugin})"
  end
end

#start(plugin) ⇒ Object

Operations



80
81
82
83
# File 'lib/ircbot/plugins.rb', line 80

def start(plugin)
  plugin!(plugin).running = true
  changed!
end

#stop(plugin) ⇒ Object



85
86
87
88
# File 'lib/ircbot/plugins.rb', line 85

def stop(plugin)
  plugin!(plugin).running = false
  changed!
end