pluginfactory 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,223 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ BEGIN {
4
+ require 'pathname'
5
+ basedir = Pathname.new( __FILE__ ).dirname.parent
6
+
7
+ libdir = basedir + "lib"
8
+
9
+ $LOAD_PATH.unshift( libdir ) unless $LOAD_PATH.include?( libdir )
10
+ }
11
+
12
+ begin
13
+ require 'spec/runner'
14
+ require 'pluginfactory'
15
+ rescue LoadError
16
+ unless Object.const_defined?( :Gem )
17
+ require 'rubygems'
18
+ retry
19
+ end
20
+ raise
21
+ end
22
+
23
+
24
+
25
+ class Plugin
26
+ extend PluginFactory
27
+ def self::derivative_dirs
28
+ [ 'plugins', 'plugins/private' ]
29
+ end
30
+ end
31
+
32
+
33
+
34
+ describe PluginFactory do
35
+
36
+ it "calls its logging callback with the level and joined message if set" do
37
+ level = nil; msg = nil
38
+ PluginFactory.logger_callback = lambda {|level, msg|}
39
+
40
+ PluginFactory.log( :level, 'message1', 'message2' )
41
+ level.should == :level
42
+ msg.should == 'message1message2'
43
+ end
44
+
45
+ it "doesn't error when its log method is called if no logging callback is set" do
46
+ PluginFactory.logger_callback = nil
47
+ lambda { PluginFactory.log(:level, "msg") }.should_not raise_error()
48
+ end
49
+
50
+ end
51
+
52
+ describe "A class extended with PluginFactory" do
53
+
54
+ before( :each ) do
55
+ # This is kind of cheating, but it makes testing possible without knowing
56
+ # the order the examples are run in
57
+ Plugin.derivatives.clear
58
+ end
59
+
60
+
61
+ it "knows about all of its derivatives" do
62
+ Plugin.derivatives.should be_empty()
63
+ Plugin.derivatives.should be_an_instance_of( Hash )
64
+
65
+ class SubPlugin < Plugin; end
66
+
67
+ Plugin.derivatives.should have(4).members
68
+ Plugin.derivatives.keys.should include( 'sub' )
69
+ Plugin.derivatives.keys.should include( 'subplugin' )
70
+ Plugin.derivatives.keys.should include( 'SubPlugin' )
71
+ Plugin.derivatives.keys.should include( SubPlugin )
72
+ end
73
+
74
+ it "can return an Array of all of its derivative classes" do
75
+ Plugin.derivative_classes.should be_empty()
76
+
77
+ class OtherPlugin < Plugin; end
78
+
79
+ Plugin.derivative_classes.should == [OtherPlugin]
80
+ end
81
+
82
+ it "returns derivatives directly if they're already loaded" do
83
+ class AlreadyLoadedPlugin < Plugin; end
84
+ Kernel.should_not_receive( :require )
85
+ Plugin.create( 'alreadyloaded' ).should be_an_instance_of( AlreadyLoadedPlugin )
86
+ Plugin.create( 'AlreadyLoaded' ).should be_an_instance_of( AlreadyLoadedPlugin )
87
+ Plugin.create( 'AlreadyLoadedPlugin' ).should be_an_instance_of( AlreadyLoadedPlugin )
88
+ Plugin.create( AlreadyLoadedPlugin ).should be_an_instance_of( AlreadyLoadedPlugin )
89
+ end
90
+
91
+ it "filters errors that happen when creating an instance of derivatives so they " +
92
+ "point to the right place" do
93
+ class PugilistPlugin < Plugin
94
+ def initialize
95
+ raise "Oh noes -- an error!"
96
+ end
97
+ end
98
+
99
+ begin
100
+ Plugin.create('pugilist')
101
+ rescue ::Exception => err
102
+ err.backtrace.first.should =~ /#{__FILE__}/
103
+ else
104
+ fail "Expected an exception to be raised"
105
+ end
106
+ end
107
+
108
+ it "will refuse to create an object other than one of its derivatives" do
109
+ class Doppelgaenger; end
110
+ lambda { Plugin.create(Doppelgaenger) }.
111
+ should raise_error( ArgumentError, /is not a descendent of/ )
112
+ end
113
+
114
+
115
+ it "will load new plugins from the require path if they're not loaded yet" do
116
+ loaded_class = nil
117
+
118
+ Plugin.should_receive( :require ).and_return {|*args|
119
+ loaded_class = Class.new( Plugin ) do
120
+ def self::name; "DazzlePlugin"; end
121
+ end
122
+ true
123
+ }
124
+
125
+ Plugin.create( 'dazzle' ).should be_an_instance_of( loaded_class )
126
+ end
127
+
128
+
129
+ it "will #require derivatives that aren't yet loaded" do
130
+ loaded_class = nil
131
+
132
+ Plugin.should_receive( :require ).and_return {|*args|
133
+ loaded_class = Class.new( Plugin ) do
134
+ def self::name; "SnazzlePlugin"; end
135
+ end
136
+ true
137
+ }
138
+
139
+ Plugin.create( 'snazzle' ).should be_an_instance_of( loaded_class )
140
+ end
141
+
142
+
143
+ it "will output a sensible description of what it tried to load if requiring a " +
144
+ "derivative fails" do
145
+
146
+ # at least 6 -> 3 variants * 2 paths
147
+ Plugin.should_receive( :require ).
148
+ at_least(6).times.
149
+ and_return {|path| raise LoadError, "path" }
150
+
151
+ lambda { Plugin.create('scintillating') }.
152
+ should raise_error( FactoryError, /couldn't find a \S+ named \S+.*tried \[/i )
153
+ end
154
+
155
+
156
+ it "will output a sensible description when a require succeeds, but it loads something unintended" do
157
+ # at least 6 -> 3 variants * 2 paths
158
+ Plugin.should_receive( :require ).and_return( true )
159
+
160
+ lambda { Plugin.create('corruscating') }.
161
+ should raise_error( FactoryError, /Require of '\S+' succeeded, but didn't load a Plugin/i )
162
+ end
163
+
164
+
165
+ it "will re-raise the first exception raised when attempting to load a " +
166
+ "derivative if none of the paths work" do
167
+
168
+ # at least 6 -> 3 variants * 2 paths
169
+ Plugin.should_receive( :require ).at_least(6).times.and_return {|path|
170
+ raise ScriptError, "error while parsing #{path}"
171
+ }
172
+
173
+ lambda { Plugin.create('portable') }.
174
+ should raise_error( ScriptError, /error while parsing/ )
175
+ end
176
+ end
177
+
178
+
179
+ describe "A derivative of a class extended with PluginFactory" do
180
+
181
+ before( :all ) do
182
+ class TestingPlugin < Plugin; end
183
+ end
184
+
185
+ it "knows what type of factory loads it" do
186
+ TestingPlugin.factory_type.should == 'Plugin'
187
+ end
188
+
189
+ it "raises a FactoryError if it can't figure out what type of factory loads it" do
190
+ TestingPlugin.stub!( :ancestors ).and_return( [] )
191
+ lambda { TestingPlugin.factory_type }.
192
+ should raise_error( FactoryError, /couldn't find factory base/i )
193
+ end
194
+ end
195
+
196
+
197
+ describe "A derivative of a class extended with PluginFactory that isn't named <Something>Plugin" do
198
+
199
+ before( :all ) do
200
+ class BlackSheep < Plugin; end
201
+ end
202
+
203
+ it "is still creatable via its full name" do
204
+ Plugin.create( 'blacksheep' ).should be_an_instance_of( BlackSheep )
205
+ end
206
+
207
+ end
208
+
209
+
210
+ describe "A derivative of a class extended with PluginFactory in another namespace" do
211
+
212
+ before( :all ) do
213
+ module Test
214
+ class LoadablePlugin < Plugin; end
215
+ end
216
+ end
217
+
218
+ it "is still creatable via its derivative name" do
219
+ Plugin.create( 'loadable' ).should be_an_instance_of( Test::LoadablePlugin )
220
+ end
221
+
222
+ end
223
+
metadata CHANGED
@@ -1,57 +1,76 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.4
3
- specification_version: 1
4
2
  name: pluginfactory
5
3
  version: !ruby/object:Gem::Version
6
- version: 1.0.2
7
- date: 2007-07-17 00:00:00 -07:00
8
- summary: Mixin module that adds plugin behavior to any class
9
- require_paths:
10
- - lib
11
- email: ged@FaerieMUD.org
12
- homepage: http://devEiate.org/projects/PluginFactory
13
- rubyforge_project:
14
- description: PluginFactory is a module that provides plugin behavior for any class. Including PluginFactory in your class provides a ::create class method, which can be passed the name of any derivatives. Derivative classes are loaded dynamically and instantiated by the factory class.
15
- autorequire: pluginfactory
16
- default_executable:
17
- bindir: bin
18
- has_rdoc: true
19
- required_ruby_version: !ruby/object:Gem::Version::Requirement
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: 1.8.5
24
- version:
4
+ version: 1.0.3
25
5
  platform: ruby
26
- signing_key:
27
- cert_chain:
28
- post_install_message:
29
6
  authors:
30
7
  - Michael Granger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-08-13 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: PluginFactory is a mixin module that turns an including class into a factory for its derivatives, capable of searching for and loading them by name. This is useful when you have an abstract base class which defines an interface and basic functionality for a part of a larger system, and a collection of subclasses which implement the interface for different underlying functionality.
17
+ email: ged@FaerieMUD.org
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
31
24
  files:
32
- - COPYRIGHT
25
+ - Rakefile
33
26
  - ChangeLog
34
27
  - README
35
- - install.rb
28
+ - LICENSE
29
+ - spec/pluginfactory_spec.rb
36
30
  - lib/pluginfactory.rb
37
- - test.rb
38
- - tests/mybase.rb
39
- - tests/othersub.rb
40
- - tests/subofmybase.rb
41
- - tests/dir/deepsubofmybase.rb
42
- - utils.rb
43
- test_files:
44
- - test.rb
31
+ - rake/dependencies.rb
32
+ - rake/helpers.rb
33
+ - rake/manual.rb
34
+ - rake/packaging.rb
35
+ - rake/publishing.rb
36
+ - rake/rdoc.rb
37
+ - rake/style.rb
38
+ - rake/svn.rb
39
+ - rake/testing.rb
40
+ - rake/verifytask.rb
41
+ has_rdoc: true
42
+ homepage: http://deveiate.org/projects/PluginFactory/
43
+ post_install_message:
45
44
  rdoc_options:
46
- - --main
45
+ - -w
46
+ - "4"
47
+ - -SHN
48
+ - -i
49
+ - .
50
+ - -m
47
51
  - README
48
- extra_rdoc_files:
49
- - README
50
- executables: []
51
-
52
- extensions: []
53
-
52
+ - -W
53
+ - http://deveiate.org/projects/PluginFactory//browser/trunk/
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
54
68
  requirements: []
55
69
 
56
- dependencies: []
57
-
70
+ rubyforge_project: pluginfactory
71
+ rubygems_version: 1.2.0
72
+ signing_key:
73
+ specification_version: 2
74
+ summary: A mixin for making plugin classes
75
+ test_files:
76
+ - spec/pluginfactory_spec.rb
data/COPYRIGHT DELETED
@@ -1,131 +0,0 @@
1
-
2
-
3
-
4
-
5
- The "Artistic License"
6
-
7
- Preamble
8
-
9
- The intent of this document is to state the conditions under which a
10
- Package may be copied, such that the Copyright Holder maintains some
11
- semblance of artistic control over the development of the package,
12
- while giving the users of the package the right to use and distribute
13
- the Package in a more-or-less customary fashion, plus the right to make
14
- reasonable modifications.
15
-
16
- Definitions:
17
-
18
- "Package" refers to the collection of files distributed by the
19
- Copyright Holder, and derivatives of that collection of files
20
- created through textual modification.
21
-
22
- "Standard Version" refers to such a Package if it has not been
23
- modified, or has been modified in accordance with the wishes
24
- of the Copyright Holder as specified below.
25
-
26
- "Copyright Holder" is whoever is named in the copyright or
27
- copyrights for the package.
28
-
29
- "You" is you, if you're thinking about copying or distributing
30
- this Package.
31
-
32
- "Reasonable copying fee" is whatever you can justify on the
33
- basis of media cost, duplication charges, time of people involved,
34
- and so on. (You will not be required to justify it to the
35
- Copyright Holder, but only to the computing community at large
36
- as a market that must bear the fee.)
37
-
38
- "Freely Available" means that no fee is charged for the item
39
- itself, though there may be fees involved in handling the item.
40
- It also means that recipients of the item may redistribute it
41
- under the same conditions they received it.
42
-
43
- 1. You may make and give away verbatim copies of the source form of the
44
- Standard Version of this Package without restriction, provided that you
45
- duplicate all of the original copyright notices and associated disclaimers.
46
-
47
- 2. You may apply bug fixes, portability fixes and other modifications
48
- derived from the Public Domain or from the Copyright Holder. A Package
49
- modified in such a way shall still be considered the Standard Version.
50
-
51
- 3. You may otherwise modify your copy of this Package in any way, provided
52
- that you insert a prominent notice in each changed file stating how and
53
- when you changed that file, and provided that you do at least ONE of the
54
- following:
55
-
56
- a) place your modifications in the Public Domain or otherwise make them
57
- Freely Available, such as by posting said modifications to Usenet or
58
- an equivalent medium, or placing the modifications on a major archive
59
- site such as uunet.uu.net, or by allowing the Copyright Holder to include
60
- your modifications in the Standard Version of the Package.
61
-
62
- b) use the modified Package only within your corporation or organization.
63
-
64
- c) rename any non-standard executables so the names do not conflict
65
- with standard executables, which must also be provided, and provide
66
- a separate manual page for each non-standard executable that clearly
67
- documents how it differs from the Standard Version.
68
-
69
- d) make other distribution arrangements with the Copyright Holder.
70
-
71
- 4. You may distribute the programs of this Package in object code or
72
- executable form, provided that you do at least ONE of the following:
73
-
74
- a) distribute a Standard Version of the executables and library files,
75
- together with instructions (in the manual page or equivalent) on where
76
- to get the Standard Version.
77
-
78
- b) accompany the distribution with the machine-readable source of
79
- the Package with your modifications.
80
-
81
- c) give non-standard executables non-standard names, and clearly
82
- document the differences in manual pages (or equivalent), together
83
- with instructions on where to get the Standard Version.
84
-
85
- d) make other distribution arrangements with the Copyright Holder.
86
-
87
- 5. You may charge a reasonable copying fee for any distribution of this
88
- Package. You may charge any fee you choose for support of this
89
- Package. You may not charge a fee for this Package itself. However,
90
- you may distribute this Package in aggregate with other (possibly
91
- commercial) programs as part of a larger (possibly commercial) software
92
- distribution provided that you do not advertise this Package as a
93
- product of your own. You may embed this Package's interpreter within
94
- an executable of yours (by linking); this shall be construed as a mere
95
- form of aggregation, provided that the complete Standard Version of the
96
- interpreter is so embedded.
97
-
98
- 6. The scripts and library files supplied as input to or produced as
99
- output from the programs of this Package do not automatically fall
100
- under the copyright of this Package, but belong to whoever generated
101
- them, and may be sold commercially, and may be aggregated with this
102
- Package. If such scripts or library files are aggregated with this
103
- Package via the so-called "undump" or "unexec" methods of producing a
104
- binary executable image, then distribution of such an image shall
105
- neither be construed as a distribution of this Package nor shall it
106
- fall under the restrictions of Paragraphs 3 and 4, provided that you do
107
- not represent such an executable image as a Standard Version of this
108
- Package.
109
-
110
- 7. C subroutines (or comparably compiled subroutines in other
111
- languages) supplied by you and linked into this Package in order to
112
- emulate subroutines and variables of the language defined by this
113
- Package shall not be considered part of this Package, but are the
114
- equivalent of input as in Paragraph 6, provided these subroutines do
115
- not change the language in any way that would cause it to fail the
116
- regression tests for the language.
117
-
118
- 8. Aggregation of this Package with a commercial distribution is always
119
- permitted provided that the use of this Package is embedded; that is,
120
- when no overt attempt is made to make this Package's interfaces visible
121
- to the end user of the commercial distribution. Such use shall not be
122
- construed as a distribution of this Package.
123
-
124
- 9. The name of the Copyright Holder may not be used to endorse or promote
125
- products derived from this software without specific prior written permission.
126
-
127
- 10. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
128
- IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
129
- WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
130
-
131
- The End
data/install.rb DELETED
@@ -1,172 +0,0 @@
1
- #!/usr/bin/ruby
2
- #
3
- # Module Install Script
4
- # $Id: install.rb 31 2004-11-21 17:14:13Z ged $
5
- #
6
- # Thanks to Masatoshi SEKI for ideas found in his install.rb.
7
- #
8
- # Copyright (c) 2001-2004 The FaerieMUD Consortium.
9
- #
10
- # This is free software. You may use, modify, and/or redistribute this
11
- # software under the terms of the Perl Artistic License. (See
12
- # http://language.perl.com/misc/Artistic.html)
13
- #
14
-
15
- require './utils.rb'
16
- include UtilityFunctions
17
-
18
- require 'rbconfig'
19
- include Config
20
-
21
- require 'find'
22
- require 'ftools'
23
- require 'optparse'
24
-
25
- $version = %q$Revision: 31 $
26
- $rcsId = %q$Id: install.rb 31 2004-11-21 17:14:13Z ged $
27
-
28
- # Define required libraries
29
- RequiredLibraries = [
30
- # libraryname, nice name, RAA URL, Download URL, e.g.,
31
- #[ 'strscan', "Strscan",
32
- # 'http://www.ruby-lang.org/en/raa-list.rhtml?name=strscan',
33
- # 'http://i.loveruby.net/archive/strscan/strscan-0.6.7.tar.gz',
34
- #],
35
- ]
36
-
37
- class Installer
38
-
39
- @@PrunePatterns = [
40
- /CVS/,
41
- /~$/,
42
- %r:(^|/)\.:,
43
- /\.tpl$/,
44
- ]
45
-
46
- def initialize( testing=false )
47
- @ftools = (testing) ? self : File
48
- end
49
-
50
- ### Make the specified dirs (which can be a String or an Array of Strings)
51
- ### with the specified mode.
52
- def makedirs( dirs, mode=0755, verbose=false )
53
- dirs = [ dirs ] unless dirs.is_a? Array
54
-
55
- oldumask = File::umask
56
- File::umask( 0777 - mode )
57
-
58
- for dir in dirs
59
- if @ftools == File
60
- File::mkpath( dir, $verbose )
61
- else
62
- $stderr.puts "Make path %s with mode %o" % [ dir, mode ]
63
- end
64
- end
65
-
66
- File::umask( oldumask )
67
- end
68
-
69
- def install( srcfile, dstfile, mode=nil, verbose=false )
70
- dstfile = File.catname(srcfile, dstfile)
71
- unless FileTest.exist? dstfile and File.cmp srcfile, dstfile
72
- $stderr.puts " install #{srcfile} -> #{dstfile}"
73
- else
74
- $stderr.puts " skipping #{dstfile}: unchanged"
75
- end
76
- end
77
-
78
- public
79
-
80
- def installFiles( src, dstDir, mode=0444, verbose=false )
81
- directories = []
82
- files = []
83
-
84
- if File.directory?( src )
85
- Find.find( src ) {|f|
86
- Find.prune if @@PrunePatterns.find {|pat| f =~ pat}
87
- next if f == src
88
-
89
- if FileTest.directory?( f )
90
- directories << f.gsub( /^#{src}#{File::Separator}/, '' )
91
- next
92
-
93
- elsif FileTest.file?( f )
94
- files << f.gsub( /^#{src}#{File::Separator}/, '' )
95
-
96
- else
97
- Find.prune
98
- end
99
- }
100
- else
101
- files << File.basename( src )
102
- src = File.dirname( src )
103
- end
104
-
105
- dirs = [ dstDir ]
106
- dirs |= directories.collect {|d| File.join(dstDir,d)}
107
- makedirs( dirs, 0755, verbose )
108
- files.each {|f|
109
- srcfile = File.join(src,f)
110
- dstfile = File.dirname(File.join( dstDir,f ))
111
-
112
- if verbose
113
- if mode
114
- $stderr.puts "Install #{srcfile} -> #{dstfile} (mode %o)" % mode
115
- else
116
- $stderr.puts "Install #{srcfile} -> #{dstfile}"
117
- end
118
- end
119
-
120
- @ftools.install( srcfile, dstfile, mode, verbose )
121
- }
122
- end
123
-
124
- end
125
-
126
-
127
- if $0 == __FILE__
128
- for lib in RequiredLibraries
129
- testForRequiredLibrary( *lib )
130
- end
131
-
132
- dryrun = false
133
-
134
- # Parse command-line switches
135
- ARGV.options {|oparser|
136
- oparser.banner = "Usage: #$0 [options]\n"
137
-
138
- oparser.on( "--verbose", "-v", TrueClass, "Make progress verbose" ) {
139
- $VERBOSE = true
140
- debugMsg "Turned verbose on."
141
- }
142
-
143
- oparser.on( "--dry-run", "-n", TrueClass, "Don't really install anything" ) {
144
- debugMsg "Turned dry-run on."
145
- dryrun = true
146
- }
147
-
148
- # Handle the 'help' option
149
- oparser.on( "--help", "-h", "Display this text." ) {
150
- $stderr.puts oparser
151
- exit!(0)
152
- }
153
-
154
- oparser.parse!
155
- }
156
-
157
- debugMsg "Sitelibdir = '#{CONFIG['sitelibdir']}'"
158
- sitelibdir = CONFIG['sitelibdir']
159
- debugMsg "Sitearchdir = '#{CONFIG['sitearchdir']}'"
160
- sitearchdir = CONFIG['sitearchdir']
161
-
162
- message "Installing..."
163
- i = Installer.new( dryrun )
164
- #i.installFiles( "redist", sitelibdir, 0444, verbose )
165
- i.installFiles( "lib", sitelibdir, 0444, $VERBOSE )
166
-
167
- message "done.\n"
168
- end
169
-
170
-
171
-
172
-