Class: Campfire::PollingBot::Plugin
- Inherits:
-
Object
- Object
- Campfire::PollingBot::Plugin
- Defined in:
- lib/campfire/polling_bot/plugin.rb
Direct Known Subclasses
AirbrakePlugin, BookmarkPlugin, DebugPlugin, DeployPlugin, GreetingPlugin, HelpPlugin, HistoryPlugin, ImageSearchPlugin, InterjectorPlugin, KibitzPlugin, ReloadPlugin, RemindMePlugin, SMSPlugin, SamplePlugin, TimePlugin, TweetPlugin, TwitterSearchPlugin
Constant Summary collapse
- HALT =
returned by a command when command processing should halt (continues by default)
1
Instance Attribute Summary collapse
- #bot ⇒ Object
-
#config ⇒ Object
Returns the value of attribute config.
Class Method Summary collapse
-
.accepts(message_type, params = {}) ⇒ Object
called from Plugin objects to indicate what kinds of messages they accept if the :addressed_to_me flag is true, it will only accept messages addressed to the bot (e.g. “Wes, __” or “__, Wes”) Examples: accepts :text_message, :addressed_to_me => true accepts :enter_message accepts :all.
-
.accepts?(message) ⇒ Boolean
returns true if the plugin accepts the given message type.
-
.bot ⇒ Object
bot accessor.
- .bot=(bot) ⇒ Object
- .directory ⇒ Object
- .directory=(dir) ⇒ Object
-
.inherited(klass) ⇒ Object
keep track of subclasses.
- .load_all(bot) ⇒ Object
- .load_plugin_classes ⇒ Object
-
.priority(value = nil) ⇒ Object
method to set or get the priority.
- .requires_config(flag = true) ⇒ Object
- .requires_config? ⇒ Boolean
-
.setup_database(datauri) ⇒ Object
set up the plugin database.
- .subclasses ⇒ Object
Instance Method Summary collapse
-
#accepts?(message) ⇒ Boolean
convenience method to call accepts on a plugin instance.
-
#initialize ⇒ Plugin
constructor
A new instance of Plugin.
- #logger ⇒ Object
-
#priority ⇒ Object
convenience method to get the priority of a plugin instance.
- #requires_config? ⇒ Boolean
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ Plugin
Returns a new instance of Plugin.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/campfire/polling_bot/plugin.rb', line 14 def initialize # load the config file if we have one name = self.to_s.gsub(/([[:upper:]]+)([[:upper:]][[:lower:]])/,'\1_\2'). gsub(/([[:lower:]\d])([[:upper:]])/,'\1_\2'). tr("-", "_"). downcase config_dir = bot.config.config_dir || self.class.directory filepath = File.join(config_dir, "#{name}.yml") if File.exists?(filepath) self.config = YAML.load_file(filepath) else self.config = {} end end |
Instance Attribute Details
#bot ⇒ Object
62 63 64 |
# File 'lib/campfire/polling_bot/plugin.rb', line 62 def bot @bot || self.class.bot end |
#config ⇒ Object
Returns the value of attribute config.
12 13 14 |
# File 'lib/campfire/polling_bot/plugin.rb', line 12 def config @config end |
Class Method Details
.accepts(message_type, params = {}) ⇒ Object
called from Plugin objects to indicate what kinds of messages they accept if the :addressed_to_me flag is true, it will only accept messages addressed to the bot (e.g. “Wes, __” or “__, Wes”) Examples:
accepts :text_message, :addressed_to_me => true
accepts :enter_message
accepts :all
148 149 150 151 152 153 154 155 156 |
# File 'lib/campfire/polling_bot/plugin.rb', line 148 def self.accepts(, params = {}) @accepts ||= {} if == :all @accepts[:all] = params[:addressed_to_me] ? :addressed_to_me : :for_anyone else klass = Campfire.const_get(.to_s.gsub(/(?:^|_)(\S)/) {$1.upcase}) @accepts[klass] = params[:addressed_to_me] ? :addressed_to_me : :for_anyone end end |
.accepts?(message) ⇒ Boolean
returns true if the plugin accepts the given message type
159 160 161 162 163 164 165 |
# File 'lib/campfire/polling_bot/plugin.rb', line 159 def self.accepts?() if @accepts[:all] @accepts[:all] == :addressed_to_me ? bot.addressed_to_me?() : true elsif @accepts[.class] @accepts[.class] == :addressed_to_me ? bot.addressed_to_me?() : true end end |
.bot ⇒ Object
bot accessor
55 56 57 |
# File 'lib/campfire/polling_bot/plugin.rb', line 55 def self.bot @@bot end |
.bot=(bot) ⇒ Object
58 59 60 |
# File 'lib/campfire/polling_bot/plugin.rb', line 58 def self.bot=(bot) @@bot = bot end |
.directory ⇒ Object
50 51 52 |
# File 'lib/campfire/polling_bot/plugin.rb', line 50 def self.directory @directory end |
.directory=(dir) ⇒ Object
46 47 48 |
# File 'lib/campfire/polling_bot/plugin.rb', line 46 def self.directory=(dir) @directory = dir end |
.inherited(klass) ⇒ Object
keep track of subclasses
30 31 32 33 34 35 36 37 |
# File 'lib/campfire/polling_bot/plugin.rb', line 30 def self.inherited(klass) # save the plugin's directory filepath = caller[0].split(':')[0] klass.directory = File.dirname(caller[0].split(':')[0]) super if defined? super ensure ( @subclasses ||= [] ).push(klass).uniq! end |
.load_all(bot) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/campfire/polling_bot/plugin.rb', line 72 def self.load_all(bot) self.bot = bot load_plugin_classes # set up the database now that the plugins are loaded setup_database(bot.config.datauri) plugin_classes = self.subclasses.sort {|a,b| b.priority <=> a.priority } # initialize plugins plugins = plugin_classes.map { |p_class| p_class.new } # remove any plugins that require a config and don't have one plugins.reject! {|p| p.requires_config? and p.config.empty?} return plugins end |
.load_plugin_classes ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/campfire/polling_bot/plugin.rb', line 88 def self.load_plugin_classes # add each plugin dir to the load path Dir.glob(File.dirname(__FILE__) + "/plugins/*").each {|dir| $LOAD_PATH << dir } # load core first paths = Dir.glob(File.dirname(__FILE__) + "/plugins/shared/*.rb") # load all models & plugins paths += Dir.glob(File.dirname(__FILE__) + "/plugins/*/*.rb") paths.each do |path| begin path.match(/(.*?)\.rb$/) && (require $1) rescue Exception => e $stderr.puts "Unable to load #{path}: #{e.class}: #{e.}\n\t#{e.backtrace.join("\n\t")}" end end end |
.priority(value = nil) ⇒ Object
method to set or get the priority. Higher value == higher priority. Default is 0 command subclasses set their priority like so:
class FooPlugin << Campfire::PollingBot::Plugin
priority 10
...
117 118 119 120 121 122 |
# File 'lib/campfire/polling_bot/plugin.rb', line 117 def self.priority(value = nil) if value @priority = value end return @priority || 0 end |
.requires_config(flag = true) ⇒ Object
129 130 131 |
# File 'lib/campfire/polling_bot/plugin.rb', line 129 def self.requires_config(flag = true) @requires_config = flag end |
.requires_config? ⇒ Boolean
133 134 135 |
# File 'lib/campfire/polling_bot/plugin.rb', line 133 def self.requires_config? @requires_config end |
.setup_database(datauri) ⇒ Object
set up the plugin database
105 106 107 108 109 110 |
# File 'lib/campfire/polling_bot/plugin.rb', line 105 def self.setup_database(datauri) DataMapper.setup(:default, datauri) DataMapper.auto_upgrade! # not related to setting up the database, but for lack of a better place.... DataMapper::Model.raise_on_save_failure = true end |
.subclasses ⇒ Object
39 40 41 42 43 44 |
# File 'lib/campfire/polling_bot/plugin.rb', line 39 def self.subclasses @subclasses ||= [] @subclasses.inject( [] ) do |list, subclass| list.push(subclass, *subclass.subclasses) end end |
Instance Method Details
#accepts?(message) ⇒ Boolean
convenience method to call accepts on a plugin instance
168 169 170 |
# File 'lib/campfire/polling_bot/plugin.rb', line 168 def accepts?() self.class.accepts?() end |
#logger ⇒ Object
66 67 68 |
# File 'lib/campfire/polling_bot/plugin.rb', line 66 def logger bot.logger end |
#priority ⇒ Object
convenience method to get the priority of a plugin instance
125 126 127 |
# File 'lib/campfire/polling_bot/plugin.rb', line 125 def priority self.class.priority end |
#requires_config? ⇒ Boolean
137 138 139 |
# File 'lib/campfire/polling_bot/plugin.rb', line 137 def requires_config? self.class.requires_config? end |
#to_s ⇒ Object
172 173 174 |
# File 'lib/campfire/polling_bot/plugin.rb', line 172 def to_s self.class.to_s end |