Permalink
Browse files

Move config_var from Bugzilla and Quote plugin into Plugin class

The config_var tool has been handy for processing configuration
parameters for a plugin.  It seems generic enough to roll into the
Plugin class.

Move the config_var class method and the initialize block into the
Plugin class.  Be sure to call "super" from the subclass's initialize
method.  Move the applicable examples into a separate plugin_spec.
  • Loading branch information...
1 parent 8d2b8e1 commit 86c71184123e7a1b75dcaeefabf8b9b5c15b497d Marcel Cary committed with Jul 6, 2009
Showing with 63 additions and 39 deletions.
  1. +18 −1 lib/plugin.rb
  2. +1 −11 plugins/bugzilla.rb
  3. +1 −10 plugins/our_quotes.rb
  4. +0 −17 spec/bugzilla_spec.rb
  5. +43 −0 spec/plugin_spec.rb
View
19 lib/plugin.rb
@@ -53,6 +53,23 @@ def at_time(timestamp, *methods)
Plugin.registered_times << Event::Time.new(timestamp, self.to_s, method)
end
end
+
+ # Declare a plugin configuration parameter with its default value
+ def config_var(name, default)
+ attr_reader name
+ @@config_defaults ||= {}
+ @@config_defaults[self.name] ||= {}
+ @@config_defaults[self.name][name] = default
+ end
+ end
+ def initialize
+ # initialize attr_readers setup with config_var
+ config_prefix = self.class.to_s.underscore
+ (@@config_defaults[self.class.name] || {}).each_pair { |name, default|
+ instance_variable_set("@#{name.to_s}",
+ bot.config["#{config_prefix}_#{name.to_s}"] ||
+ default)
+ }
end
end
-end
+end
View
12 plugins/bugzilla.rb
@@ -25,12 +25,6 @@ class Bugzilla < CampfireBot::Plugin
on_command 'bug', :describe_command
# on_message registered below...
- def self.config_var(name, default)
- attr_reader name
- @@config_defaults ||= {}
- @@config_defaults[name] = default
- end
-
config_var :data_file, File.join(BOT_ROOT, 'tmp', 'bugzilla.yml')
config_var :min_period, 30.minutes
config_var :debug_enabled, false
@@ -45,11 +39,7 @@ def self.config_var(name, default)
:use_htmlentities, :use_netrc
def initialize()
- @@config_defaults.each_pair { |name, default|
- instance_variable_set("@#{name.to_s}",
- bot.config["bugzilla_#{name.to_s}"] || default)
- }
-
+ super
@bug_id_regexp = Regexp.new(bug_id_pattern, Regexp::IGNORECASE)
@mention_regexp = Regexp.new(sprintf(mention_pattern,
bug_id_pattern, bug_word_pattern),
View
11 plugins/our_quotes.rb
@@ -50,23 +50,14 @@ class OurQuotes < CampfireBot::Plugin
# Configure with "our_quote_recall_command: quote"
#on_command 'quote', :quote
- def self.config_var(name, default)
- attr_reader name
- @@config_defaults ||= {}
- @@config_defaults[name] = default
- end
-
config_var :data_file, File.join(BOT_ROOT, 'var', 'quote-log.yml')
config_var :recall_command, "ourquote"
config_var :debug_enabled, false
attr_reader :use_htmlentities
def initialize()
- @@config_defaults.each_pair { |name, default|
- instance_variable_set("@#{name.to_s}",
- bot.config["quote_#{name.to_s}"] || default)
- }
+ super
self.class.on_command recall_command.to_s, :quote
View
17 spec/bugzilla_spec.rb
@@ -70,23 +70,6 @@
end
end
-describe "config_var method" do
- def setup_plugin(klass, config={})
- @bot = CampfireBot::Bot.instance
- @bot.stub!(:config).and_return({'nickname' => 'Bot'}.merge(config))
- @plugin = klass.new
- CampfireBot::Plugin.registered_plugins[klass.to_s] = @plugin
- end
- it "should setup default bug_url value" do
- setup_plugin Bugzilla
- @plugin.bug_url.should == "https://bugzilla/show_bug.cgi?id=%s"
- end
- it "should allow override of bug_url via bugzilla_bug_url configuration" do
- setup_plugin Bugzilla, 'bugzilla_bug_url' => 'http://b/%s'
- @plugin.bug_url.should == "http://b/%s"
- end
-end
-
describe "http_fetch_body method" do
before(:each) do
end
View
43 spec/plugin_spec.rb
@@ -0,0 +1,43 @@
+require 'spec'
+BOT_ROOT = File.join(File.dirname(__FILE__), '..')
+BOT_ENVIRONMENT = 'test'
+
+require File.join(File.dirname(__FILE__), '../lib/bot.rb')
+bot = CampfireBot::Bot.instance
+
+describe "config_var method" do
+ class ExamplePlugin < CampfireBot::Plugin
+ config_var :foo, "default foo"
+ end
+
+ def setup_plugin(klass, config={})
+ @bot = CampfireBot::Bot.instance
+ @bot.stub!(:config).and_return({'nickname' => 'Bot'}.merge(config))
+ @plugin = klass.new
+ CampfireBot::Plugin.registered_plugins[klass.to_s] = @plugin
+ end
+
+ it "should setup default bug_url value" do
+ setup_plugin ExamplePlugin
+ @plugin.foo.should == "default foo"
+ end
+
+ it "should allow override of parameter via configuration" do
+ setup_plugin ExamplePlugin, 'example_plugin_foo' => 'bar'
+ @plugin.foo.should == "bar"
+ end
+
+ it "should keep defaults for two classes separate" do
+ class ExamplePlugin2 < CampfireBot::Plugin
+ config_var :bar, "default bar"
+ end
+ setup_plugin ExamplePlugin
+ plugin = @plugin
+ setup_plugin ExamplePlugin2
+ plugin.instance_variable_get(:@foo).should == "default foo"
+ @plugin.instance_variable_get(:@bar).should == "default bar"
+ # Used to fail with value "default bar"
+ plugin.instance_variable_get(:@bar).should == nil
+ @plugin.instance_variable_get(:@foo).should == nil
+ end
+end

0 comments on commit 86c7118

Please sign in to comment.