plugman 0.1.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,30 @@
1
1
  Plugman
2
2
  =======
3
3
 
4
+ Plugman is a plugin manager that supports event driven communication
5
+ with plugins. It handles the loading, initialization and all
6
+ communications with the plugins.
7
+
8
+
9
+ Installation
10
+ ------------
11
+
12
+ The easiest way to get plugman is through rubygems
13
+
14
+ gem install plugman
15
+
16
+ If you want to track the latest changes
17
+
18
+ git clone git://github.com/kjellm/plugman.git
19
+ gem build plugman.gemspec
20
+ gem install plugman-XXX.gem
21
+
22
+
23
+ Usage
24
+ -----
25
+
26
+ See the API documentation at <http://rubydoc.info/gems/plugman/>.
27
+
4
28
  Alternatives
5
29
  ------------
6
30
 
@@ -11,3 +35,21 @@ FIX document differences
11
35
  - little-plugger
12
36
  - pluginfactory
13
37
 
38
+
39
+ Bugs
40
+ ----
41
+
42
+ Report bugs to <http://github.com/kjellm/plugman/issues>
43
+
44
+
45
+ Author
46
+ ------
47
+
48
+ Kjell-Magne Øierud &lt;kjellm AT acm DOT org&gt;
49
+
50
+
51
+ License
52
+ -------
53
+
54
+ This computer program is distributed under the GPL. Please see the
55
+ COPYING file part of this distribution.
@@ -5,14 +5,46 @@ require 'plugman/plugin_base'
5
5
  require 'logger'
6
6
  require 'stringio'
7
7
 
8
+ #
9
+ # Plugman is a plugin manager that supports event driven communication
10
+ # with plugins. It handles the loading, initialization and all
11
+ # communications with the plugins.
12
+ #
13
+ # To call a method on the registered plugins, call plugman with a
14
+ # method name matching
15
+ #
16
+ # /^signal_(before|after|at)/
17
+ #
18
+ # plugman will then call similar named (without signal_) methods on all
19
+ # plugins which responds to the method.
20
+ #
21
+ # === Example
22
+ #
23
+ # Put some plugins in lib/your_app/plugin/. For documentation on
24
+ # writing plugins, see link:Plugman/PluginBase.html
25
+ #
26
+ # require 'plugman'
27
+ #
28
+ # class YourApp
29
+ #
30
+ # def initialize
31
+ # @pm = Plugman.new('your_app')
32
+ # @pm.load_plugins
33
+ # end
34
+ #
35
+ # def main
36
+ # @pm.signal_at_starup
37
+ #
38
+ # # ...
39
+ #
40
+ # @pm.signal_before_bar
41
+ # end
42
+ #
43
+
8
44
  class Plugman
9
45
 
10
46
  def initialize(finder_or_name)
11
- if finder_or_name.is_a?(String)
12
- @finder = Finder::Standard.new(finder_or_name)
13
- else
14
- @finder = finder
15
- end
47
+ self.finder = finder_or_name
16
48
  @plugins = []
17
49
  @log = StringIO.new("")
18
50
  @logger = Logger.new(@log)
@@ -23,6 +55,8 @@ class Plugman
23
55
  @log.string
24
56
  end
25
57
 
58
+ # Looks for plugins, requires them, checks state, initializes, and
59
+ # registers the plugins
26
60
  def load_plugins
27
61
  @finder.plugin_files.each do |f|
28
62
  require_plugin(f)
@@ -35,13 +69,11 @@ class Plugman
35
69
  @plugins = @plugins.select {|p| p.state_ok? }
36
70
  end
37
71
 
38
- def register_plugin(klass)
39
- @plugins.push(klass.new)
40
- end
41
-
72
+ # Calls the
42
73
  def method_missing(name, *arguments, &block)
43
- if name.to_s =~ /^send_(before|after|at|.*_hook$)/
44
- method = name.to_s[5..-1]
74
+ if name.to_s =~ /^signal_(before|after|at)/
75
+ method = name.to_s[7..-1]
76
+ @logger.debug("Sending #{method} to plugins")
45
77
  @plugins.select {|p| p.respond_to?(method)}.each do |p|
46
78
  p.send(method, *arguments)
47
79
  end
@@ -50,10 +82,23 @@ class Plugman
50
82
  end
51
83
  end
52
84
 
85
+ def register_plugin(klass)
86
+ @plugins.push(klass.new)
87
+ end
88
+
53
89
  # FIX implement respond_to? to match method_missing?
54
90
 
55
91
  private
56
92
 
93
+ def finder=(finder_or_name)
94
+ if finder_or_name.is_a?(String)
95
+ @finder = Finder::Standard.new(finder_or_name)
96
+ elsif finder_or_name.respond_to?(:plugin_files)
97
+ @finder = finder_or_name
98
+ else
99
+ raise ArgumentError 'Require a string or an object repsonding to plugin_files()'
100
+ end
101
+ end
57
102
 
58
103
  def require_plugin(f)
59
104
  @logger.debug "Requiering #{f}"
@@ -1,5 +1,38 @@
1
1
 
2
2
  class Plugman
3
+
4
+ #
5
+ # Plugins need to inherit this class for plugman to be able register
6
+ # them.
7
+ #
8
+ # A typical plugin will look like something like this:
9
+ #
10
+ # require 'plugman/plugin_base'
11
+ #
12
+ # class YourApp
13
+ # module Plugin
14
+ # class CoolPlugin < Plugman::PluginBase
15
+ #
16
+ # def at_startup
17
+ # puts "Whoa, it worked!"
18
+ # end
19
+ #
20
+ # def before_bar
21
+ # puts "foo"
22
+ # end
23
+ #
24
+ # end
25
+ # end
26
+ # end
27
+ #
28
+ # After a plugin is initialized, plugman will call state_ok?(). Only
29
+ # plugins who return true in this method will be registered by
30
+ # plugman.
31
+ #
32
+ # For documentation on method names, see the documentation for
33
+ # link:Plugman.html
34
+ #
35
+
3
36
  class PluginBase
4
37
 
5
38
  def self.manager=(obj)
@@ -10,6 +43,8 @@ class Plugman
10
43
  @@manager.register_plugin(klass)
11
44
  end
12
45
 
46
+ # Just returns true. Define this in your plugin class if you need
47
+ # to verify that your plugin state is ok after initialization.
13
48
  def state_ok?; true; end
14
49
 
15
50
  end
@@ -2,14 +2,16 @@ require 'plugman'
2
2
  require 'plugman/finder'
3
3
  require 'test/unit'
4
4
 
5
- class Foo < Test::Unit::TestCase
5
+ class TestPlugman < Test::Unit::TestCase
6
6
 
7
7
  def test_it
8
8
  pm = Plugman.new(Plugman::Finder::Simple.new(File.dirname(__FILE__) + '/plugin'))
9
9
  pm.load_plugins
10
10
 
11
11
  str = ""
12
- pm.send_before_big_bang(str)
12
+ pm.signal_before_big_bang(str)
13
+ print pm.log
14
+
13
15
  assert(str =~ /WHOOOP/)
14
16
  assert(str =~ /WHIIIIIIIIIIZZZZZZZZ/)
15
17
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plugman
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: '0.2'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,10 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-06-01 00:00:00.000000000 +02:00
12
+ date: 2011-06-14 00:00:00.000000000 +02:00
13
13
  default_executable:
14
14
  dependencies: []
15
- description: FIX
15
+ description: Plugman is a plugin manager that supports event driven communication
16
+ with plugins. It handles the loading, initialization and all communications with
17
+ the plugins.
16
18
  email: kjellm@acm.org
17
19
  executables: []
18
20
  extensions: []
@@ -23,7 +25,7 @@ files:
23
25
  - lib/plugman/finder/standard.rb
24
26
  - lib/plugman/finder/simple.rb
25
27
  - lib/plugman.rb
26
- - test/foo.rb
28
+ - test/test_plugman.rb
27
29
  - test/plugin/bar.rb
28
30
  - test/plugin/baz.rb
29
31
  - Rakefile