Skip to content
This repository has been archived by the owner on Apr 9, 2019. It is now read-only.

Commit

Permalink
Move config_var from Bugzilla and Quote plugin into Plugin class
Browse files Browse the repository at this point in the history
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
Marcel Cary authored and timriley committed Aug 3, 2009
1 parent 8d2b8e1 commit 86c7118
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 39 deletions.
19 changes: 18 additions & 1 deletion lib/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 1 addition & 11 deletions plugins/bugzilla.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand Down
11 changes: 1 addition & 10 deletions plugins/our_quotes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 0 additions & 17 deletions spec/bugzilla_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 43 additions & 0 deletions spec/plugin_spec.rb
Original file line number Diff line number Diff line change
@@ -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.