From 5f7962ed2369c92cd4889fd2d24c60e5e0efe284 Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Wed, 16 Jan 2013 05:12:29 +0000 Subject: [PATCH] Rework help system (again) to reduce channel noise. --- plugin/help_cinch_plugin.rb | 19 ++++++++++++------- plugin_handler.rb | 16 ++++++++++++++++ spec/help_spec.rb | 11 +++++++++-- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/plugin/help_cinch_plugin.rb b/plugin/help_cinch_plugin.rb index 2d564eb..2804ef9 100644 --- a/plugin/help_cinch_plugin.rb +++ b/plugin/help_cinch_plugin.rb @@ -2,18 +2,23 @@ module TurbotPlugins class Help include Cinch::Plugin + PluginHandler.add_plugin(self) + + def self.help + PluginCommand.new('.help', 'This help display.') + end + set :prefix, PREFIX - match /help/, method: :help + match /help$/, method: :help def help(m) - m.reply pretty_help.to_s + m.reply "Hi, I'm turbot. I know how to respond to many commands (I also accept pull requests for more). For more information on any of them execute .help ." + m.reply PluginHandler.matchers.join(", ") end - def pretty_help - plugin_commands = PluginHandler.plugins.inject([]) {|m,p| m += Array(p.help) } - rows = plugin_commands.collect {|c| [c.matchers,c.description]} - - Terminal::Table.new :headings => ['Matchers', 'Description'], :rows => rows + match /help (.+)/, method: :help_command + def help_command(m, matcher) + m.reply PluginHandler.command(matcher).display_row.join(' - ') end end end diff --git a/plugin_handler.rb b/plugin_handler.rb index a496604..d9c0363 100644 --- a/plugin_handler.rb +++ b/plugin_handler.rb @@ -6,6 +6,18 @@ def self.plugins def self.add_plugin(plugin) self.plugins << plugin end + + def self.commands + plugins.inject([]) {|m,p| m += Array(p.help) } + end + + def self.matchers + commands.collect{|c| c.matchers} + end + + def self.command(matcher) + commands.detect{|c| c.matchers =~ /#{matcher}/} + end end class PluginCommand @@ -15,4 +27,8 @@ def initialize(matchers, description) self.matchers = matchers self.description = description end + + def display_row + [matchers, description] + end end diff --git a/spec/help_spec.rb b/spec/help_spec.rb index d06bdfd..f41763b 100644 --- a/spec/help_spec.rb +++ b/spec/help_spec.rb @@ -22,10 +22,17 @@ def self.help context "#help" do it "should print the plugins help message." do - - m.should_receive(:reply).with("+-------------+----------------------------------+\n| Matchers | Description |\n+-------------+----------------------------------+\n| .nextmeetup | Get the next meetup information. |\n+-------------+----------------------------------+") + m.should_receive(:reply).with("Hi, I'm turbot. I know how to respond to many commands (I accept pull requests for more).") + m.should_receive(:reply).with(".nextmeetup") plugin.help(m) end end + + context "#help_command" do + it "should print the detailed help for the given matcher." do + m.should_receive(:reply).with(".nextmeetup - Get the next meetup information.") + plugin.help_command(m, '.nextmeetup') + end + end end