forked from moneypenny-bot/moneypenny
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
plugin management with a custom responder
This is going to take some refactoring to make it less hackish. Potential ideas include making all plugins inherit directly from Plugin to remove the need for special casing between the two types. But as a first pass this works.
- Loading branch information
Showing
5 changed files
with
140 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module Moneypenny | ||
class PluginManager | ||
def initialize(responders, listeners) | ||
@responders = responders | ||
@listeners = listeners | ||
end | ||
|
||
def respond( message ) | ||
case message | ||
when /unload plugin (.*)/ | ||
begin | ||
@responders.unload_plugin!($1) && | ||
"Unloaded #{$1}" | ||
rescue ArgumentError | ||
@listeners.unload_plugin!($1) | ||
"Unloaded #{$1}" | ||
end | ||
when /load plugin (.*)/ | ||
begin | ||
@responders.load_plugin!($1) && | ||
"Loaded #{$1}" | ||
rescue ArgumentError | ||
@listeners.load_plugin!($1) | ||
"Loaded #{$1}" | ||
end | ||
when /load all plugins/ | ||
@responders.load_all! && | ||
@listeners.load_all! | ||
else | ||
false | ||
end | ||
rescue ArgumentError | ||
"Can't find that plugin" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
require File.expand_path(File.dirname(__FILE__) + '/spec_helper') | ||
|
||
class TestResponder < Moneypenny::Responder | ||
def self.respond(message) | ||
end | ||
end | ||
|
||
class TestListener < Moneypenny::Listener | ||
def self.respond(message) | ||
end | ||
end | ||
|
||
describe Moneypenny::PluginManager do | ||
|
||
before(:each) do | ||
@responder = Moneypenny::Responder.new | ||
@listener = Moneypenny::Listener.new | ||
@plugin_manager = Moneypenny::PluginManager.new(@responder, @listener) | ||
end | ||
|
||
describe "respond" do | ||
it "returns true for a message 'unload plugin (.*)'" do | ||
@plugin_manager.respond("unload plugin bar").should be_true | ||
end | ||
|
||
it "it removes the requested responder plugin" do | ||
@responder.load_plugin!(TestResponder.name) | ||
@responder.loaded_plugins.should include(TestResponder) | ||
@plugin_manager.respond("unload plugin TestResponder") | ||
@responder.loaded_plugins.should_not include(TestResponder) | ||
end | ||
|
||
it "it removes the requested listener plugin" do | ||
@listener.load_plugin!(TestListener.name) | ||
@listener.loaded_plugins.should include(TestListener) | ||
@plugin_manager.respond("unload plugin TestListener") | ||
@listener.loaded_plugins.should_not include(TestListener) | ||
end | ||
|
||
it "returns true for a message 'load plugin (.*)'" do | ||
@plugin_manager.respond("load plugin bar").should be_true | ||
end | ||
|
||
it "it loads the requested responder plugin" do | ||
@responder.loaded_plugins.should_not include(TestResponder) | ||
@plugin_manager.respond("load plugin TestResponder") | ||
@responder.loaded_plugins.should include(TestResponder) | ||
end | ||
|
||
it "it loads the requested listener plugin" do | ||
@listener.loaded_plugins.should_not include(TestListener) | ||
@plugin_manager.respond("load plugin TestListener") | ||
@listener.loaded_plugins.should include(TestListener) | ||
end | ||
|
||
it "returns true for a message 'load all plugins'" do | ||
@plugin_manager.respond("load all plugins").should be_true | ||
end | ||
|
||
it "loads all plugins when asked to" do | ||
@listener.loaded_plugins.should_not include(TestListener) | ||
@responder.loaded_plugins.should_not include(TestResponder) | ||
@plugin_manager.respond("load all plugins") | ||
@responder.loaded_plugins.should include(TestResponder) | ||
@listener.loaded_plugins.should include(TestListener) | ||
end | ||
|
||
it "returns false for any non-matching message" do | ||
@plugin_manager.respond("piss off").should be_false | ||
end | ||
end | ||
end |