casting 0.6.9 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/casting/client.rb +1 -1
- data/lib/casting/missing_method_client_class.rb +1 -5
- data/lib/casting/prepared_delegation.rb +3 -30
- data/lib/casting/version.rb +1 -1
- data/test/casting_19_test.rb +0 -10
- data/test/casting_20_test.rb +0 -4
- data/test/client_test.rb +13 -0
- data/test/delegation_test.rb +6 -3
- data/test/missing_method_client_test.rb +13 -0
- data/test/null_module_test.rb +13 -15
- data/test/super_test.rb +0 -1
- data/test/test_helper.rb +0 -15
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0cca3576453e1acf20799cd80afe5a2e9e4c39cc
|
4
|
+
data.tar.gz: f892e78276d01554fa159d893507c61fcb4dcf37
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 68f289b4009fd5330f856fda1ce71f190ab8317fbf0aa343b9b4244afc6bcdb613be34457c3b98ceb120b620e889511b285099d1006cd6f679be874a6a0a1251
|
7
|
+
data.tar.gz: 75e7c54b13bad49081fd15597adc4891f3ebfdb5c66b423b0278ecbfd8a3dd48ff22ab1fcc6a2b40b7dae19a29d5e31d1a5a3b1a0986f9250c67e2c3c52c3950
|
data/lib/casting/client.rb
CHANGED
@@ -27,11 +27,7 @@ module Casting
|
|
27
27
|
|
28
28
|
def method_class_delegate(meth)
|
29
29
|
__class_delegates__.find{|attendant|
|
30
|
-
|
31
|
-
attendant.instance_methods
|
32
|
-
else
|
33
|
-
attendant.methods
|
34
|
-
end.include?(meth)
|
30
|
+
attendant.method_defined?(meth)
|
35
31
|
}
|
36
32
|
end
|
37
33
|
end
|
@@ -1,17 +1,3 @@
|
|
1
|
-
# Some features are only available in versions of Ruby
|
2
|
-
# where this method is true
|
3
|
-
unless defined?(self.module_method_rebinding?)
|
4
|
-
def module_method_rebinding?
|
5
|
-
return @__module_method_rebinding__ if defined?(@__module_method_rebinding__)
|
6
|
-
sample_method = Enumerable.instance_method(:to_a)
|
7
|
-
@__module_method_rebinding__ = begin
|
8
|
-
!!sample_method.bind(Object.new)
|
9
|
-
rescue TypeError
|
10
|
-
false
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
1
|
module Casting
|
16
2
|
|
17
3
|
class MissingAttendant < StandardError
|
@@ -36,11 +22,10 @@ module Casting
|
|
36
22
|
end
|
37
23
|
|
38
24
|
def to(object_or_module)
|
39
|
-
@attendant =
|
25
|
+
@attendant = object_or_module
|
40
26
|
begin
|
41
27
|
check_valid_type
|
42
28
|
rescue TypeError
|
43
|
-
raise unless module_method_rebinding?
|
44
29
|
@attendant = method_module || raise
|
45
30
|
end
|
46
31
|
self
|
@@ -69,19 +54,7 @@ module Casting
|
|
69
54
|
begin
|
70
55
|
!client.nil? && delegated_method.bind(client)
|
71
56
|
rescue TypeError
|
72
|
-
raise TypeError.new("`to' argument must be a module or an
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
def method_carrier(object_or_module)
|
77
|
-
if Module === object_or_module
|
78
|
-
if module_method_rebinding?
|
79
|
-
object_or_module
|
80
|
-
else
|
81
|
-
client.clone.extend(object_or_module)
|
82
|
-
end
|
83
|
-
else
|
84
|
-
object_or_module
|
57
|
+
raise TypeError.new("`to' argument must be a module or an object with #{delegated_method_name} defined in a module")
|
85
58
|
end
|
86
59
|
end
|
87
60
|
|
@@ -93,7 +66,7 @@ module Casting
|
|
93
66
|
if Module === attendant
|
94
67
|
attendant.instance_method(delegated_method_name)
|
95
68
|
else
|
96
|
-
attendant.method(delegated_method_name).
|
69
|
+
attendant.method(delegated_method_name).owner.instance_method(delegated_method_name)
|
97
70
|
end
|
98
71
|
rescue NameError => e
|
99
72
|
raise InvalidAttendant.new(e.message)
|
data/lib/casting/version.rb
CHANGED
data/test/casting_19_test.rb
CHANGED
@@ -2,8 +2,6 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
describe Casting, '.delegating' do
|
4
4
|
it 'delegates missing methods to object delegates' do
|
5
|
-
skip 'extending objects not used in this version of Ruby' if test_rebinding_methods?
|
6
|
-
|
7
5
|
client = test_person
|
8
6
|
client.extend(Casting::Client)
|
9
7
|
client.delegate_missing_methods
|
@@ -26,8 +24,6 @@ end
|
|
26
24
|
describe Casting::Delegation do
|
27
25
|
|
28
26
|
it 'calls a method defined on another object of the same type' do
|
29
|
-
skip 'extending objects not used in this version of Ruby' if test_rebinding_methods?
|
30
|
-
|
31
27
|
client = test_person
|
32
28
|
attendant = test_person
|
33
29
|
attendant.extend(TestPerson::Greeter)
|
@@ -36,8 +32,6 @@ describe Casting::Delegation do
|
|
36
32
|
end
|
37
33
|
|
38
34
|
it 'passes arguments to a delegated method' do
|
39
|
-
skip 'extending objects not used in this version of Ruby' if test_rebinding_methods?
|
40
|
-
|
41
35
|
client = test_person
|
42
36
|
attendant = test_person
|
43
37
|
attendant.extend(TestPerson::Verbose)
|
@@ -46,16 +40,12 @@ describe Casting::Delegation do
|
|
46
40
|
end
|
47
41
|
|
48
42
|
it 'delegates when given a module' do
|
49
|
-
skip 'extending objects not used in this version of Ruby' if test_rebinding_methods?
|
50
|
-
|
51
43
|
client = test_person
|
52
44
|
delegation = Casting::Delegation.new('greet', client).to(TestPerson::Greeter)
|
53
45
|
assert_equal 'hello', delegation.call
|
54
46
|
end
|
55
47
|
|
56
48
|
it 'does not delegate when given a class' do
|
57
|
-
skip 'extending objects not used in this version of Ruby' if test_rebinding_methods?
|
58
|
-
|
59
49
|
client = test_person
|
60
50
|
assert_raises(TypeError){
|
61
51
|
Casting::Delegation.new('class_defined', client).to(Unrelated)
|
data/test/casting_20_test.rb
CHANGED
@@ -3,8 +3,6 @@ require 'test_helper'
|
|
3
3
|
describe Casting::Delegation do
|
4
4
|
|
5
5
|
it 'finds the module defining a method and uses it to delegate' do
|
6
|
-
skip 'not able to bind module methods in this version of Ruby' unless test_rebinding_methods?
|
7
|
-
|
8
6
|
client = test_person
|
9
7
|
attendant = Unrelated.new
|
10
8
|
delegation = Casting::Delegation.new('unrelated', client).to(attendant)
|
@@ -12,8 +10,6 @@ describe Casting::Delegation do
|
|
12
10
|
end
|
13
11
|
|
14
12
|
it 'does not delegate to methods defined in classes' do
|
15
|
-
skip 'not able to bind module methods in this version of Ruby' unless test_rebinding_methods?
|
16
|
-
|
17
13
|
client = test_person
|
18
14
|
attendant = Unrelated.new
|
19
15
|
assert_raises(TypeError){
|
data/test/client_test.rb
CHANGED
@@ -65,4 +65,17 @@ describe Casting::Client do
|
|
65
65
|
client.delegate('to_s', client)
|
66
66
|
}
|
67
67
|
end
|
68
|
+
|
69
|
+
it 'does not delegate singleton methods' do
|
70
|
+
client = test_person.extend(Casting::Client)
|
71
|
+
client.delegate_missing_methods
|
72
|
+
attendant = test_person
|
73
|
+
|
74
|
+
def attendant.hello
|
75
|
+
'hello'
|
76
|
+
end
|
77
|
+
assert_raises(TypeError){
|
78
|
+
client.delegate('hello', attendant)
|
79
|
+
}
|
80
|
+
end
|
68
81
|
end
|
data/test/delegation_test.rb
CHANGED
@@ -8,9 +8,12 @@ describe Casting::Delegation do
|
|
8
8
|
|
9
9
|
it 'raises an error when calling without an attendant object' do
|
10
10
|
delegation = Casting::Delegation.new('some_method', Object.new)
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
begin
|
12
|
+
delegation.call
|
13
|
+
rescue StandardError => e
|
14
|
+
end
|
15
|
+
assert_kind_of Casting::MissingAttendant, e
|
16
|
+
assert_equal "You must set your attendant object using `to'.", e.message
|
14
17
|
end
|
15
18
|
|
16
19
|
it 'raises an error when setting an invalid attendant type' do
|
@@ -77,6 +77,19 @@ describe Casting::MissingMethodClient, '#uncast' do
|
|
77
77
|
|
78
78
|
assert_equal 'name from TestPerson', jim.uncast.name
|
79
79
|
end
|
80
|
+
|
81
|
+
it "removes the specified number of delegates" do
|
82
|
+
jim = test_person.extend(Casting::Client, Casting::MissingMethodClient)
|
83
|
+
jim.cast_as(TestPerson::Greeter, TestPerson::Verbose)
|
84
|
+
|
85
|
+
assert_includes(jim.delegated_methods(true), :psst)
|
86
|
+
assert_includes(jim.delegated_methods(true), :verbose)
|
87
|
+
|
88
|
+
jim.uncast(2)
|
89
|
+
|
90
|
+
refute_includes(jim.delegated_methods(true), :psst)
|
91
|
+
refute_includes(jim.delegated_methods(true), :verbose)
|
92
|
+
end
|
80
93
|
end
|
81
94
|
|
82
95
|
describe Casting::MissingMethodClient, '#delegated_methods' do
|
data/test/null_module_test.rb
CHANGED
@@ -1,23 +1,21 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
attendant = Casting::Null
|
3
|
+
describe Casting::Null do
|
4
|
+
it 'will answer to any method with nil' do
|
5
|
+
client = TestPerson.new
|
6
|
+
client.extend(Casting::Client)
|
7
|
+
attendant = Casting::Null
|
9
8
|
|
10
|
-
|
11
|
-
end
|
9
|
+
assert_nil client.delegate('greet', attendant)
|
12
10
|
end
|
11
|
+
end
|
13
12
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
13
|
+
describe Casting::Blank do
|
14
|
+
it 'will answer to any method with an empty string' do
|
15
|
+
client = TestPerson.new
|
16
|
+
client.extend(Casting::Client)
|
17
|
+
attendant = Casting::Blank
|
19
18
|
|
20
|
-
|
21
|
-
end
|
19
|
+
assert_empty client.delegate('greet', attendant)
|
22
20
|
end
|
23
21
|
end
|
data/test/super_test.rb
CHANGED
@@ -22,7 +22,6 @@ end
|
|
22
22
|
|
23
23
|
describe Casting, 'modules using delegate_super' do
|
24
24
|
it 'call the method from the next delegate with the same arguments' do
|
25
|
-
skip 'extending objects not used in this version of Ruby' unless test_rebinding_methods?
|
26
25
|
client = TestPerson.new.extend(Casting::Client)
|
27
26
|
client.delegate_missing_methods
|
28
27
|
client.cast_as(AnyWay, ThisWay, ThatWay)
|
data/test/test_helper.rb
CHANGED
@@ -81,18 +81,3 @@ end
|
|
81
81
|
def test_person
|
82
82
|
TestPerson.new
|
83
83
|
end
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
# This is a different implementation of module_method_rebinding?
|
88
|
-
# created in order to check that the behavior of the code is correct.
|
89
|
-
#
|
90
|
-
# This method is used in tests and module_method_rebinding? is used
|
91
|
-
# in the library code.
|
92
|
-
def test_rebinding_methods?
|
93
|
-
unbound = Enumerable.instance_method(:count)
|
94
|
-
unbound.bind(Object.new)
|
95
|
-
true
|
96
|
-
rescue TypeError
|
97
|
-
false
|
98
|
-
end
|