From c2737d4d972df30725e417bed0940fc8df8e88bd Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sat, 4 Jan 2014 14:41:08 -0500 Subject: [PATCH] Factor out plugin running into the Plugin module (along with the includers() handling). --- bin/mailshears | 15 +---------- lib/common/plugin.rb | 57 +++++++++++++++++++++++++++------------ lib/mv/mv_plugin.rb | 16 +++-------- lib/prune/prune_plugin.rb | 20 +++----------- lib/rm/rm_plugin.rb | 18 ++++--------- 5 files changed, 53 insertions(+), 73 deletions(-) diff --git a/bin/mailshears b/bin/mailshears index 215bd6b..f7d5afa 100755 --- a/bin/mailshears +++ b/bin/mailshears @@ -101,20 +101,7 @@ require 'stringio' output_buffer = StringIO.new() $stdout = output_buffer -plugin_module.includers.each do |plugin_module_includer| - plugin = plugin_module_includer.new(cfg) - - if cfg.i_mean_business then - runner = plugin.runner().new() - else - runner = plugin.dummy_runner().new() - end - - # The splat passes the correct (we hope) number of arguments to the - # appropriate runner. The Rm(Dummy)Runner have splats on their - # *target arguments as well, to turn ARGV back into an array. - runner.run(plugin, *ARGV) -end +plugin_module.run(cfg, *ARGV) # Restore stdout, and print the header plus whatever the plugins # produced if they produced anything. If they didn't, we avoid diff --git a/lib/common/plugin.rb b/lib/common/plugin.rb index ed4ea00..81e1fbb 100644 --- a/lib/common/plugin.rb +++ b/lib/common/plugin.rb @@ -2,26 +2,49 @@ # operations that all plugins are supposed to support. module Plugin - def Plugin.included(c) - # Callback, called whenever another class or module includes this - # one. The parameter given is the name of the class or module - # that included us. - @includers ||= [] - @includers << c - end + module Run + # Module methods, meant to be extended. Includers can explicitly + # extend this to get a run() method defined in terms of their own + # runner() and dummy_runner() methods. - def Plugin.includers - return @includers - end + def included(c) + # Callback, called whenever another class or module includes this + # one. The parameter given is the name of the class or module + # that included us. + @includers ||= [] + @includers << c + end - def runner() - # The Runner associated with this plugin. - raise NotImplementedError - end + def includers + return @includers + end - def dummy_runner() - # The RummyRunner associated with this plugin. - raise NotImplementedError + def runner() + # The Runner associated with this plugin. + raise NotImplementedError + end + + def dummy_runner() + # The RummyRunner associated with this plugin. + raise NotImplementedError + end + + def run(cfg, *args) + includers().each do |includer| + plugin = includer.new(cfg) + + if cfg.i_mean_business then + runner = runner().new() + else + runner = dummy_runner().new() + end + + # The splat passes the correct (we hope) number of arguments to the + # appropriate runner. The Rm(Dummy)Runner have splats on their + # *target arguments as well, to turn ARGV back into an array. + runner.run(plugin, *args) + end + end end def describe_domain(domain) diff --git a/lib/mv/mv_plugin.rb b/lib/mv/mv_plugin.rb index 958f6d4..4b61603 100644 --- a/lib/mv/mv_plugin.rb +++ b/lib/mv/mv_plugin.rb @@ -3,23 +3,13 @@ module MvPlugin # Plugins for moving (renaming) users. # - def MvPlugin.included(c) - # Callback, called whenever another class or module includes this - # one. The parameter given is the name of the class or module - # that included us. - @includers ||= [] - @includers << c - end - - def MvPlugin.includers - return @includers - end + extend Plugin::Run - def runner() + def self.runner() return MvRunner end - def dummy_runner() + def self.dummy_runner() return MvDummyRunner end diff --git a/lib/prune/prune_plugin.rb b/lib/prune/prune_plugin.rb index 0fbe69d..44fd636 100644 --- a/lib/prune/prune_plugin.rb +++ b/lib/prune/prune_plugin.rb @@ -1,31 +1,19 @@ require 'rm/rm_plugin' module PrunePlugin - include RmPlugin - # # Plugins for the removal of leftover non-PostfixAdmin users, # i.e. after an user has been removed from the PostfixAdmin # database. # + include RmPlugin + extend Plugin::Run - def PrunePlugin.included(c) - # Callback, called whenever another class or module includes this - # one. The parameter given is the name of the class or module - # that included us. - @includers ||= [] - @includers << c - end - - def PrunePlugin.includers - return @includers - end - - def runner() + def self.runner() return PruneRunner end - def dummy_runner + def self.dummy_runner return PruneDummyRunner end diff --git a/lib/rm/rm_plugin.rb b/lib/rm/rm_plugin.rb index 95c595a..fe938b1 100644 --- a/lib/rm/rm_plugin.rb +++ b/lib/rm/rm_plugin.rb @@ -1,25 +1,17 @@ +require 'common/plugin.rb' + module RmPlugin # # Plugins for the removal of users. # - def RmPlugin.included(c) - # Callback, called whenever another class or module includes this - # one. The parameter given is the name of the class or module - # that included us. - @includers ||= [] - @includers << c - end - - def RmPlugin.includers - return @includers - end + extend Plugin::Run - def runner() + def self.runner() return RmRunner end - def dummy_runner() + def self.dummy_runner() return RmDummyRunner end -- 2.44.2