pluggability 0.0.2 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e540538307554e6530b3aceb32cdcc5943819fb5
4
- data.tar.gz: 7c1548aa2c9959d938bcfebfd62896727a9f02f0
3
+ metadata.gz: a4a48369fcbed3a05f6c1d709edf28215bc7a69f
4
+ data.tar.gz: 71d8323ae8af471d64796aebdb53814c12b34d79
5
5
  SHA512:
6
- metadata.gz: b2a7beb87ff1f941522f4467db3caf61bac30f3d520962bce63c2a029f97f3d04ab6d4294e315f6c6349899afa3f766ef7b905d41564514ba08d38c807ff5c0a
7
- data.tar.gz: 3a717ceb7a3d075691c36e0a520d8de3ca416bb25e2e13f59d560aa852a904fbe34392bd4e1ed3d685ff8d90e8fe833771fbe4b9d6e72666636cc62fb0906938
6
+ metadata.gz: 5d7eaa318bd1a7b45a0e704ea1c8e3b10c290551a407c7334ff92b1e065fa5fd89e6a3fdf9036018edd5fc75be5ec5110b5a39542090b6b158da1ec74cc20d01
7
+ data.tar.gz: 930077f441537a74d322f7ef28ac2be40c2031194bfc31e67d6f6ec723029627a48af8d86bf5cb6a7eacf2f3f2291a643bdecb22ad1f2254daea3db8b47d8b37
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
@@ -1,2 +1,2 @@
1
- M��uT&Zb@���X���O�h����)��/��%�s���oC��Y��RZ�����̻~�K��|f�x�����#
2
- A�J��eS9!&��?.���IQ ��%(�X_E��t�c�kkJ���ž\����(^��3h��|
1
+ qK�GI��M�yR����t���^S��Dr�1J�x�'+�~�7@
2
+
data/History.rdoc CHANGED
@@ -1,3 +1,9 @@
1
+ == v0.1.0 [2013-03-27] Michael Granger <ged@FaerieMUD.org>
2
+
3
+ - Add loading via underbarred name variants (CommaDelimitedThing ->
4
+ comma_delimited)
5
+ - Rename some stuff for consistency.
6
+
1
7
  == v0.0.2 [2012-08-13] Michael Granger <ged@FaerieMUD.org>
2
8
 
3
9
  Simplify Pluggability#derivatives.
data/lib/pluggability.rb CHANGED
@@ -12,7 +12,7 @@ module Pluggability
12
12
 
13
13
 
14
14
  # Library version
15
- VERSION = '0.0.2'
15
+ VERSION = '0.1.0'
16
16
 
17
17
 
18
18
  ### An exception class for Pluggability specific errors.
@@ -55,7 +55,7 @@ module Pluggability
55
55
 
56
56
 
57
57
  ### Returns the type name used when searching for a derivative.
58
- def factory_type
58
+ def plugin_type
59
59
  base = nil
60
60
  self.ancestors.each do |klass|
61
61
  if klass.instance_variables.include?( :@derivatives ) ||
@@ -65,7 +65,7 @@ module Pluggability
65
65
  end
66
66
  end
67
67
 
68
- raise FactoryError, "Couldn't find factory base for #{self.name}" if
68
+ raise FactoryError, "Couldn't find plugin base for #{self.name}" if
69
69
  base.nil?
70
70
 
71
71
  if base.name =~ /^.*::(.*)/
@@ -74,6 +74,7 @@ module Pluggability
74
74
  return base.name
75
75
  end
76
76
  end
77
+ alias factory_type plugin_type
77
78
 
78
79
 
79
80
  ### Inheritance callback -- Register subclasses in the derivatives hash
@@ -84,16 +85,7 @@ module Pluggability
84
85
 
85
86
  # If it's not an anonymous class, make some keys out of variants of its name
86
87
  if subclass.name
87
- simple_name = subclass.name.sub( /#<Class:0x[[:xdigit:]]+>::/i, '' )
88
- keys << simple_name << simple_name.downcase
89
-
90
- # Handle class names like 'FooBar' for 'Bar' factories.
91
- Pluggability.log.debug "Inherited %p for %p-type plugins" % [ subclass, self.factory_type ]
92
- if subclass.name.match( /(?:.*::)?(\w+)(?:#{self.factory_type})/i )
93
- keys << Regexp.last_match[1].downcase
94
- else
95
- keys << subclass.name.sub( /.*::/, '' ).downcase
96
- end
88
+ keys += self.make_derivative_names( subclass )
97
89
  else
98
90
  Pluggability.log.debug " no name-based variants for anonymous subclass %p" % [ subclass ]
99
91
  end
@@ -108,6 +100,27 @@ module Pluggability
108
100
  end
109
101
 
110
102
 
103
+ ### Return all variants of the name of the given +subclass+ that can be
104
+ ### used to load it.
105
+ def make_derivative_names( subclass )
106
+ keys = []
107
+
108
+ simple_name = subclass.name.sub( /^.*::/i, '' ).sub( /\W+$/, '' )
109
+ keys << simple_name << simple_name.downcase
110
+ keys << simple_name.gsub( /([a-z])([A-Z])/, "\\1_\\2" ).downcase
111
+
112
+ # Handle class names like 'FooBar' for 'Bar' factories.
113
+ Pluggability.log.debug "Inherited %p for %p-type plugins" % [ subclass, self.plugin_type ]
114
+ if subclass.name.match( /(?:.*::)?(\w+)(?:#{self.plugin_type})/i )
115
+ keys << Regexp.last_match[1].downcase
116
+ else
117
+ keys << subclass.name.sub( /.*::/, '' ).downcase
118
+ end
119
+
120
+ return keys
121
+ end
122
+
123
+
111
124
  ### Returns an Array of registered derivatives
112
125
  def derivative_classes
113
126
  self.derivatives.values.uniq
@@ -169,7 +182,7 @@ module Pluggability
169
182
 
170
183
 
171
184
  ### Find and load all derivatives of this class, using plugin_prefixes if any
172
- ### are defined, or a pattern derived from the #factory_type if not. Returns
185
+ ### are defined, or a pattern derived from the #plugin_type if not. Returns
173
186
  ### an array of all derivative classes. Load failures are logged but otherwise
174
187
  ### ignored.
175
188
  def load_all
@@ -183,8 +196,8 @@ module Pluggability
183
196
  end
184
197
  else
185
198
  # Use all but the last pattern, which will just be '*.rb'
186
- Pluggability.log.debug "Using factory type (%p) to build load patterns." %
187
- [ self.factory_type ]
199
+ Pluggability.log.debug "Using plugin type (%p) to build load patterns." %
200
+ [ self.plugin_type ]
188
201
  patterns += self.make_require_path( '*', '' )[0..-2].
189
202
  map {|f| f + '.rb' }
190
203
  end
@@ -225,7 +238,7 @@ module Pluggability
225
238
  unless self.derivatives[ class_name.downcase ]
226
239
  errmsg = "Require of '%s' succeeded, but didn't load a %s named '%s' for some reason." % [
227
240
  result,
228
- self.factory_type,
241
+ self.plugin_type,
229
242
  class_name.downcase,
230
243
  ]
231
244
  Pluggability.log.error( errmsg )
@@ -236,11 +249,11 @@ module Pluggability
236
249
 
237
250
  ### Build and return the unique part of the given <tt>class_name</tt>
238
251
  ### either by stripping leading namespaces if the name already has the
239
- ### name of the factory type in it (eg., 'My::FooService' for Service,
240
- ### or by appending the factory type if it doesn't.
252
+ ### name of the plugin type in it (eg., 'My::FooService' for Service,
253
+ ### or by appending the plugin type if it doesn't.
241
254
  def get_module_name( class_name )
242
- if class_name =~ /\w+#{self.factory_type}/
243
- mod_name = class_name.sub( /(?:.*::)?(\w+)(?:#{self.factory_type})/, "\\1" )
255
+ if class_name =~ /\w+#{self.plugin_type}/
256
+ mod_name = class_name.sub( /(?:.*::)?(\w+)(?:#{self.plugin_type})/, "\\1" )
244
257
  else
245
258
  mod_name = class_name
246
259
  end
@@ -290,7 +303,7 @@ module Pluggability
290
303
  # some reason.
291
304
  if fatals.empty?
292
305
  errmsg = "Couldn't find a %s named '%s': tried %p" % [
293
- self.factory_type,
306
+ self.plugin_type,
294
307
  mod_name,
295
308
  tries
296
309
  ]
@@ -310,7 +323,7 @@ module Pluggability
310
323
  ### "drivers/SocketDataDriver", "drivers/socket", "drivers/Socket"]
311
324
  def make_require_path( modname, subdir )
312
325
  path = []
313
- myname = self.factory_type
326
+ myname = self.plugin_type
314
327
 
315
328
  # Make permutations of the two parts
316
329
  path << modname
@@ -104,7 +104,7 @@ describe Pluggability do
104
104
  Plugin.should_receive( :require ).with( 'plugins/dazzle_plugin' ).and_return do |*args|
105
105
  loaded_class = Class.new( Plugin )
106
106
  # Simulate a named class, since we're not really requiring
107
- Plugin.derivatives['dazzle'] = loaded_class
107
+ Plugin.derivatives['dazzle'] = loaded_class
108
108
  true
109
109
  end
110
110
 
@@ -162,7 +162,7 @@ describe Pluggability do
162
162
  and_return( true )
163
163
  Plugin.should_receive( :require ).with( 'plugins/private/third.rb' ).
164
164
  and_return( true )
165
-
165
+
166
166
  Plugin.load_all
167
167
  end
168
168
  end
@@ -170,15 +170,15 @@ describe Pluggability do
170
170
 
171
171
  context "derivative of an extended class" do
172
172
 
173
- it "knows what type of factory loads it" do
174
- TestingPlugin.factory_type.should == 'Plugin'
173
+ it "knows what type of plugin loads it" do
174
+ TestingPlugin.plugin_type.should == 'Plugin'
175
175
  end
176
176
 
177
177
  it "raises a FactoryError if it can't figure out what type of factory loads it" do
178
178
  TestingPlugin.stub!( :ancestors ).and_return( [] )
179
179
  expect {
180
- TestingPlugin.factory_type
181
- }.to raise_error( Pluggability::FactoryError, /couldn't find factory base/i )
180
+ TestingPlugin.plugin_type
181
+ }.to raise_error( Pluggability::FactoryError, /couldn't find plugin base/i )
182
182
  end
183
183
  end
184
184
 
@@ -189,6 +189,10 @@ describe Pluggability do
189
189
  Plugin.create( 'blacksheep' ).should be_an_instance_of( BlackSheep )
190
190
  end
191
191
 
192
+ it "is loadable via its underbarred name" do
193
+ Plugin.create( 'black_sheep' ).should be_an_instance_of( BlackSheep )
194
+ end
195
+
192
196
  end
193
197
 
194
198
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pluggability
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Chase
@@ -31,7 +31,7 @@ cert_chain:
31
31
  6mKCwjpegytE0oifXfF8k75A9105cBnNiMZOe1tXiqYc/exCgWvbggurzDOcRkZu
32
32
  /YSusaiDXHKU2O3Akc3htA==
33
33
  -----END CERTIFICATE-----
34
- date: 2013-03-01 00:00:00.000000000 Z
34
+ date: 2013-03-27 00:00:00.000000000 Z
35
35
  dependencies:
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: loggability
@@ -181,7 +181,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
181
181
  version: '0'
182
182
  requirements: []
183
183
  rubyforge_project: pluggability
184
- rubygems_version: 2.0.0
184
+ rubygems_version: 2.0.3
185
185
  signing_key:
186
186
  specification_version: 4
187
187
  summary: Pluggability is a mixin module that turns an including class into a factory
metadata.gz.sig CHANGED
Binary file