delegate-class 0.0.1
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/.gitignore +4 -0
- data/Gemfile +10 -0
- data/Guardfile +14 -0
- data/README.md +30 -0
- data/Rakefile +1 -0
- data/delegate-class.gemspec +24 -0
- data/lib/delegate/class.rb +28 -0
- data/lib/delegate/class/version.rb +5 -0
- data/spec/delegate_class_spec.rb +61 -0
- data/spec/spec_helper.rb +3 -0
- metadata +57 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard 'minitest' do
|
5
|
+
# with Minitest::Unit
|
6
|
+
#watch(%r|^test/test_(.*)\.rb|)
|
7
|
+
#watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
|
8
|
+
#watch(%r|^test/test_helper\.rb|) { "test" }
|
9
|
+
|
10
|
+
# with Minitest::Spec
|
11
|
+
watch(%r|^spec/(.*)_spec\.rb|)
|
12
|
+
watch(%r|^lib/(.*)\.rb|) { |m| "spec/#{m[1]}_spec.rb" }
|
13
|
+
watch(%r|^spec/spec_helper\.rb|) { "spec" }
|
14
|
+
end
|
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# Delegate-class
|
2
|
+
|
3
|
+
Using the default ruby delegate library, this gem is used to wrap the class _AND_ instance methods of another class.
|
4
|
+
(_Though I still don't know the real usage of it_)
|
5
|
+
|
6
|
+
You may want to use it like this:
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
require "delegate/class"
|
10
|
+
|
11
|
+
class A
|
12
|
+
def self.class_meth
|
13
|
+
:class_meth
|
14
|
+
end
|
15
|
+
|
16
|
+
def instance_meth
|
17
|
+
:instance_meth
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class B < ClassDelegator(A)
|
22
|
+
def self.class_meth
|
23
|
+
super.to_s + "!"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
```
|
29
|
+
|
30
|
+
Hope you like it (:
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "delegate/class/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "delegate-class"
|
7
|
+
s.version = Delegate::Class::VERSION
|
8
|
+
s.authors = ["Bruno Tavares"]
|
9
|
+
s.email = ["bruno.exz@gmail.com"]
|
10
|
+
s.homepage = "http://bltavares.com"
|
11
|
+
s.summary = %q{Wrap a whole class (including classes methods) into a delegate class}
|
12
|
+
s.description = %q{Wrap a whole class (including classes methods) into a delegate class}
|
13
|
+
|
14
|
+
s.rubyforge_project = "delegate-class"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
# s.add_development_dependency "rspec"
|
23
|
+
# s.add_runtime_dependency "rest-client"
|
24
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require "delegate"
|
2
|
+
|
3
|
+
module Delegate
|
4
|
+
module Class
|
5
|
+
#borrowed from Delegate.delegating_block
|
6
|
+
def self.delegate_block(mid, target)
|
7
|
+
lambda do |*args, &block|
|
8
|
+
begin
|
9
|
+
target.__send__(mid, *args, &block)
|
10
|
+
ensure
|
11
|
+
$@.delete_if {|t| /\A#{Regexp.quote(__FILE__)}:#{__LINE__-2}:/ =~ t} if $@
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def ClassDelegator(superclass)
|
19
|
+
klass = DelegateClass(superclass)
|
20
|
+
klass_methods = superclass.public_methods - klass.public_methods
|
21
|
+
|
22
|
+
klass_methods.each do |meth|
|
23
|
+
klass.define_singleton_method(meth,Delegate::Class.delegate_block(meth, superclass))
|
24
|
+
end
|
25
|
+
|
26
|
+
klass
|
27
|
+
end
|
28
|
+
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "delegate/class"
|
3
|
+
|
4
|
+
|
5
|
+
class A
|
6
|
+
def instance_meth
|
7
|
+
"instance meth"
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.class_meth
|
11
|
+
"class meth"
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
class ModifiedObj < ClassDelegator(A)
|
17
|
+
def instance_meth
|
18
|
+
super + "!"
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.class_meth
|
22
|
+
super + "!"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe Delegate::Class, "ClassDelegator" do
|
27
|
+
let(:a) { A.new }
|
28
|
+
let(:obj_class) { Class.new ClassDelegator(A) }
|
29
|
+
let(:obj) { obj_class.new a }
|
30
|
+
|
31
|
+
describe "Istance methods" do
|
32
|
+
it "must respond to delegated instance methods" do
|
33
|
+
obj.must_respond_to :instance_meth
|
34
|
+
end
|
35
|
+
it "must delegate instance methods" do
|
36
|
+
obj.instance_meth.must_equal a.instance_meth
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "Class methods" do
|
41
|
+
it "must respond to delegated class methods" do
|
42
|
+
obj_class.must_respond_to :class_meth
|
43
|
+
end
|
44
|
+
|
45
|
+
it "must delegate class methods" do
|
46
|
+
obj.class.class_meth.must_equal A.class_meth
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "Modified methods" do
|
51
|
+
let(:modified) { ModifiedObj.new a }
|
52
|
+
|
53
|
+
it "must modify the instance method" do
|
54
|
+
modified.instance_meth.must_equal(a.instance_meth + "!")
|
55
|
+
end
|
56
|
+
|
57
|
+
it "must modify the class method" do
|
58
|
+
ModifiedObj.class_meth.must_equal(A.class_meth + "!")
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: delegate-class
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Bruno Tavares
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-27 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Wrap a whole class (including classes methods) into a delegate class
|
15
|
+
email:
|
16
|
+
- bruno.exz@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- Guardfile
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- delegate-class.gemspec
|
27
|
+
- lib/delegate/class.rb
|
28
|
+
- lib/delegate/class/version.rb
|
29
|
+
- spec/delegate_class_spec.rb
|
30
|
+
- spec/spec_helper.rb
|
31
|
+
homepage: http://bltavares.com
|
32
|
+
licenses: []
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project: delegate-class
|
51
|
+
rubygems_version: 1.8.11
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: Wrap a whole class (including classes methods) into a delegate class
|
55
|
+
test_files:
|
56
|
+
- spec/delegate_class_spec.rb
|
57
|
+
- spec/spec_helper.rb
|