wizardwerdna-pluggable 0.0.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.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +63 -0
- data/Rakefile +48 -0
- data/VERSION +1 -0
- data/lib/pluggable.rb +37 -0
- data/spec/pluggable_spec.rb +94 -0
- data/spec/spec_helper.rb +9 -0
- metadata +73 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Andrew C. Greenberg
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
= pluggable
|
2
|
+
|
3
|
+
Pluggable is a mixin for classes requiring plugins. A pluggable class, +Klass+, has a public function, +plugins+, returning an array-like object that holds all subclasses of <tt>Klass::Plugin</tt>.
|
4
|
+
|
5
|
+
require 'pluggable'
|
6
|
+
class Test
|
7
|
+
include Pluggable
|
8
|
+
def process param
|
9
|
+
plugins.each {|plugin| plugin.process param}
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Plugin1 < Test:Plugin
|
14
|
+
def process param; ...; end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Plugin2 < Test:Plugin
|
18
|
+
def process param; ...; end
|
19
|
+
end
|
20
|
+
|
21
|
+
It may be convenient to have public methods of plugins delegated to from the +plugins+ object, which may in turn be delgated to by the +Pluggable+ class in various ways. For example:
|
22
|
+
|
23
|
+
require 'pluggable'
|
24
|
+
class Test
|
25
|
+
include Pluggable
|
26
|
+
def process param
|
27
|
+
plugins.each {|plugin| plugin.process param}
|
28
|
+
end
|
29
|
+
def missing_method symbol, *args
|
30
|
+
plugins.send(symbol, *args)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class Plugin1 < Test:Plugin
|
35
|
+
def foo; "foo"; end
|
36
|
+
def process param; subprocess; end
|
37
|
+
private
|
38
|
+
def subprocess; ...; end
|
39
|
+
end
|
40
|
+
|
41
|
+
class Plugin2 < Test:Plugin
|
42
|
+
def bar; "bar"; end
|
43
|
+
def process param; subprocess; end
|
44
|
+
private
|
45
|
+
def subprocess; ...; end
|
46
|
+
end
|
47
|
+
|
48
|
+
Test.new.plugins.delegate_public_methods_to_plugins_except :process
|
49
|
+
Test.new.foo # => "foo"
|
50
|
+
|
51
|
+
== Note on Patches/Pull Requests
|
52
|
+
|
53
|
+
* Fork the project.
|
54
|
+
* Make your feature addition or bug fix.
|
55
|
+
* Add tests for it. This is important so I don't break it in a
|
56
|
+
future version unintentionally.
|
57
|
+
* Commit, do not mess with rakefile, version, or history.
|
58
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
59
|
+
* Send me a pull request. Bonus points for topic branches.
|
60
|
+
|
61
|
+
== Copyright
|
62
|
+
|
63
|
+
Copyright (c) 2009 Andrew C. Greenberg. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "pluggable"
|
8
|
+
gem.summary = %Q{TODO: one-line summary of your gem}
|
9
|
+
gem.description = %Q{TODO: longer description of your gem}
|
10
|
+
gem.email = "wizardwerdna@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/wizardwerdna/pluggable"
|
12
|
+
gem.authors = ["Andrew C. Greenberg"]
|
13
|
+
gem.add_development_dependency "rspec"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'spec/rake/spectask'
|
21
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
22
|
+
spec.libs << 'lib' << 'spec'
|
23
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
24
|
+
end
|
25
|
+
|
26
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
27
|
+
spec.libs << 'lib' << 'spec'
|
28
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
29
|
+
spec.rcov = true
|
30
|
+
end
|
31
|
+
|
32
|
+
task :spec => :check_dependencies
|
33
|
+
|
34
|
+
task :default => :spec
|
35
|
+
|
36
|
+
require 'rake/rdoctask'
|
37
|
+
Rake::RDocTask.new do |rdoc|
|
38
|
+
if File.exist?('VERSION')
|
39
|
+
version = File.read('VERSION')
|
40
|
+
else
|
41
|
+
version = ""
|
42
|
+
end
|
43
|
+
|
44
|
+
rdoc.rdoc_dir = 'rdoc'
|
45
|
+
rdoc.title = "pluggable #{version}"
|
46
|
+
rdoc.rdoc_files.include('README*')
|
47
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
48
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.0
|
data/lib/pluggable.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
|
4
|
+
require 'singleton'
|
5
|
+
require 'forwardable'
|
6
|
+
module Pluggable
|
7
|
+
VERSION = '0.0.1'
|
8
|
+
|
9
|
+
def plugins
|
10
|
+
Plugins.instance
|
11
|
+
end
|
12
|
+
|
13
|
+
class Plugins < Array
|
14
|
+
include Singleton
|
15
|
+
extend Forwardable
|
16
|
+
|
17
|
+
def delegate_public_methods_to_plugins_except *excluded_methods
|
18
|
+
excluded_methods = excluded_methods.map{|each| each.to_s}
|
19
|
+
each do |instance|
|
20
|
+
delegated_methods = instance.public_methods-Plugin.public_instance_methods-excluded_methods
|
21
|
+
variable_name = variable_name_for_plugin_instance instance
|
22
|
+
instance_variable_set variable_name, instance
|
23
|
+
self.class.def_delegators variable_name, *delegated_methods
|
24
|
+
end
|
25
|
+
end
|
26
|
+
private
|
27
|
+
def variable_name_for_plugin_instance instance
|
28
|
+
"@ivar_for_#{instance.class.to_s}".gsub(/::/) {'_colons_'}.to_sym
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class Plugin
|
33
|
+
def self.inherited(klass)
|
34
|
+
Plugins.instance << klass.new
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
3
|
+
|
4
|
+
class Test
|
5
|
+
extend Forwardable
|
6
|
+
include Pluggable
|
7
|
+
def method_missing symbol, *args
|
8
|
+
plugins.send(symbol, *args)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class Plugin1 < Test::Plugin
|
13
|
+
def foo
|
14
|
+
"foo"
|
15
|
+
end
|
16
|
+
def exception_public
|
17
|
+
end
|
18
|
+
private
|
19
|
+
def exception_private
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class Plugin2 < Test::Plugin
|
24
|
+
def bar
|
25
|
+
"bar"
|
26
|
+
end
|
27
|
+
def exception_public
|
28
|
+
end
|
29
|
+
private
|
30
|
+
def exception_private
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class Plugin3 < Test::Plugin
|
35
|
+
def baz
|
36
|
+
"baz"
|
37
|
+
end
|
38
|
+
def exception_public
|
39
|
+
end
|
40
|
+
private
|
41
|
+
def exception_private
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
Test.new.plugins.delegate_public_methods_to_plugins_except :exception_public
|
46
|
+
|
47
|
+
describe Pluggable, "when included in a class" do
|
48
|
+
|
49
|
+
before(:each) do
|
50
|
+
@test_instance = Test.new
|
51
|
+
end
|
52
|
+
|
53
|
+
it "adds a plugins method to the class instance returning an instance of Plugins" do
|
54
|
+
@test_instance.should respond_to(:plugins)
|
55
|
+
@test_instance.plugins.should be_a_kind_of(Test::Plugins)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should include instances of all the plugins" do
|
59
|
+
@test_instance.plugins.should have(3).items
|
60
|
+
@test_instance.plugins.map{|each| each.class}.should include(Plugin1, Plugin2, Plugin3)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should delegate public methods of plugins" do
|
64
|
+
@test_instance.plugins.should respond_to(:foo)
|
65
|
+
@test_instance.plugins.should respond_to(:bar)
|
66
|
+
@test_instance.plugins.should respond_to(:baz)
|
67
|
+
@test_instance.plugins.foo.should == "foo"
|
68
|
+
@test_instance.plugins.bar.should == "bar"
|
69
|
+
@test_instance.plugins.baz.should == "baz"
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should not delegate excepted public methods of plugins" do
|
73
|
+
@test_instance.plugins.should_not respond_to(:exception_public)
|
74
|
+
@test_instance.should_not respond_to(:exception_public)
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should not delegate private methods of plugins" do
|
78
|
+
@test_instance.plugins.should_not respond_to(:exception_private)
|
79
|
+
@test_instance.should_not respond_to(:exception_private)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe Test, "when using message_missing to simulate delegation from the parent" do
|
84
|
+
|
85
|
+
before(:each) do
|
86
|
+
@test_instance = Test.new
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should delegate public methods of plugins" do
|
90
|
+
@test_instance.foo.should == "foo"
|
91
|
+
@test_instance.bar.should == "bar"
|
92
|
+
@test_instance.baz.should == "baz"
|
93
|
+
end
|
94
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wizardwerdna-pluggable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew C. Greenberg
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-08-20 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: "TODO: longer description of your gem"
|
26
|
+
email: wizardwerdna@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- lib/pluggable.rb
|
42
|
+
- spec/pluggable_spec.rb
|
43
|
+
- spec/spec_helper.rb
|
44
|
+
has_rdoc: false
|
45
|
+
homepage: http://github.com/wizardwerdna/pluggable
|
46
|
+
licenses:
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options:
|
49
|
+
- --charset=UTF-8
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "0"
|
63
|
+
version:
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.3.5
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: "TODO: one-line summary of your gem"
|
71
|
+
test_files:
|
72
|
+
- spec/pluggable_spec.rb
|
73
|
+
- spec/spec_helper.rb
|