casting 0.4.0 → 0.4.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/lib/casting.rb +15 -4
- data/lib/casting/version.rb +1 -1
- data/test/casting_19_test.rb +68 -0
- data/test/casting_20_test.rb +24 -0
- data/test/casting_test.rb +26 -0
- data/test/test_helper.rb +4 -1
- metadata +10 -3
data/lib/casting.rb
CHANGED
@@ -30,13 +30,23 @@ module Casting
|
|
30
30
|
def base.delegate_missing_methods
|
31
31
|
self.send(:include, ::Casting::MissingMethodClient)
|
32
32
|
end
|
33
|
+
|
34
|
+
unless base.instance_methods.include?('delegate')
|
35
|
+
base.class_eval{ alias_method :delegate, :cast }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.extended(base)
|
40
|
+
unless base.respond_to?('delegate')
|
41
|
+
base.singleton_class.class_eval{ alias_method :delegate, :cast }
|
42
|
+
end
|
33
43
|
end
|
34
44
|
|
35
45
|
def delegation(delegated_method_name)
|
36
46
|
Casting::Delegation.new(delegated_method_name, self)
|
37
47
|
end
|
38
48
|
|
39
|
-
def
|
49
|
+
def cast(delegated_method_name, attendant, *args)
|
40
50
|
delegation(delegated_method_name).to(attendant).with(*args).call
|
41
51
|
end
|
42
52
|
|
@@ -96,7 +106,8 @@ module Casting
|
|
96
106
|
self
|
97
107
|
end
|
98
108
|
|
99
|
-
def call
|
109
|
+
def call(*args)
|
110
|
+
@arguments = args unless args.empty?
|
100
111
|
raise MissingAttendant.new unless attendant
|
101
112
|
|
102
113
|
if arguments
|
@@ -167,8 +178,8 @@ module Casting
|
|
167
178
|
self
|
168
179
|
end
|
169
180
|
|
170
|
-
def call
|
171
|
-
prepared_delegation.call
|
181
|
+
def call(*args)
|
182
|
+
prepared_delegation.call(*args)
|
172
183
|
end
|
173
184
|
|
174
185
|
end
|
data/lib/casting/version.rb
CHANGED
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'casting'
|
3
|
+
|
4
|
+
unless RedCard.check '2.0'
|
5
|
+
|
6
|
+
describe Casting, '.delegating' do
|
7
|
+
it 'delegates missing methods to object delegates' do
|
8
|
+
client = test_person
|
9
|
+
client.extend(Casting::Client)
|
10
|
+
client.delegate_missing_methods
|
11
|
+
|
12
|
+
attendant = test_person
|
13
|
+
attendant.extend(TestPerson::Greeter)
|
14
|
+
|
15
|
+
assert_raises(NoMethodError){
|
16
|
+
client.greet
|
17
|
+
}
|
18
|
+
Casting.delegating(client => attendant) do
|
19
|
+
assert_equal 'hello', client.greet
|
20
|
+
end
|
21
|
+
assert_raises(NoMethodError){
|
22
|
+
client.greet
|
23
|
+
}
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe Casting::Delegation do
|
28
|
+
|
29
|
+
# it 'errors with a method defined on another object not of the same module type' do
|
30
|
+
# client = test_person
|
31
|
+
# attendant = test_person
|
32
|
+
# attendant.extend(TestPerson::Greeter)
|
33
|
+
# assert_raises(TypeError){
|
34
|
+
# Casting::Delegation.new('greet', client).to(attendant)
|
35
|
+
# }
|
36
|
+
# end
|
37
|
+
|
38
|
+
it 'calls a method defined on another object of the same type' do
|
39
|
+
client = test_person
|
40
|
+
attendant = test_person
|
41
|
+
attendant.extend(TestPerson::Greeter)
|
42
|
+
delegation = Casting::Delegation.new('greet', client).to(attendant)
|
43
|
+
assert_equal 'hello', delegation.call
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'passes arguments to a delegated method' do
|
47
|
+
client = test_person
|
48
|
+
attendant = test_person
|
49
|
+
attendant.extend(TestPerson::Verbose)
|
50
|
+
delegation = Casting::Delegation.new('verbose', client).to(attendant).with('arg1','arg2')
|
51
|
+
assert_equal 'arg1,arg2', delegation.call
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'delegates when given a module' do
|
55
|
+
client = test_person
|
56
|
+
delegation = Casting::Delegation.new('greet', client).to(TestPerson::Greeter)
|
57
|
+
assert_equal 'hello', delegation.call
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'does not delegate when given a class' do
|
61
|
+
client = test_person
|
62
|
+
assert_raises(TypeError){
|
63
|
+
Casting::Delegation.new('class_defined', client).to(Unrelated)
|
64
|
+
}
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
end # RedCard
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'casting'
|
3
|
+
|
4
|
+
if RedCard.check '2.0'
|
5
|
+
|
6
|
+
describe Casting::Delegation do
|
7
|
+
|
8
|
+
it 'finds the module defining a method and uses it to delegate' do
|
9
|
+
client = test_person
|
10
|
+
attendant = Unrelated.new
|
11
|
+
delegation = Casting::Delegation.new('unrelated', client).to(attendant)
|
12
|
+
assert_equal attendant.unrelated, delegation.call
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'does not delegate to methods defined in classes' do
|
16
|
+
client = test_person
|
17
|
+
attendant = Unrelated.new
|
18
|
+
assert_raises(TypeError){
|
19
|
+
Casting::Delegation.new('class_defined', client).to(attendant)
|
20
|
+
}
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end # RedCard
|
data/test/casting_test.rb
CHANGED
@@ -91,9 +91,35 @@ describe Casting::Delegation do
|
|
91
91
|
|
92
92
|
assert_equal attendant_output, delegation_output
|
93
93
|
end
|
94
|
+
|
95
|
+
it 'prefers `call` arguments over `with`' do
|
96
|
+
client = test_person
|
97
|
+
attendant = TestPerson::Verbose
|
98
|
+
|
99
|
+
delegation = Casting::Delegation.new('verbose', client).to(attendant)
|
100
|
+
|
101
|
+
attendant_output = attendant.verbose('call', 'args')
|
102
|
+
delegation_output = delegation.with('hello', 'goodbye').call('call','args')
|
103
|
+
|
104
|
+
assert_equal attendant_output, delegation_output
|
105
|
+
end
|
94
106
|
end
|
95
107
|
|
96
108
|
describe Casting::Client do
|
109
|
+
it 'will not override an existing `delegate` method' do
|
110
|
+
client = TestPerson.new
|
111
|
+
def client.delegate
|
112
|
+
'existing delegate method'
|
113
|
+
end
|
114
|
+
client.extend(Casting::Client)
|
115
|
+
|
116
|
+
attendant = TestPerson::Greeter
|
117
|
+
|
118
|
+
assert_equal 'existing delegate method', client.delegate
|
119
|
+
|
120
|
+
assert_equal 'hello', client.cast('greet', attendant)
|
121
|
+
end
|
122
|
+
|
97
123
|
it 'adds a delegate method to call a method on an attendant' do
|
98
124
|
client = TestPerson.new
|
99
125
|
client.extend(Casting::Client)
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: casting
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-05-
|
12
|
+
date: 2013-05-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: redcard
|
@@ -27,7 +27,10 @@ dependencies:
|
|
27
27
|
- - ~>
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: 1.1.0
|
30
|
-
description:
|
30
|
+
description: ! "Casting assists in method delegation which preserves the binding of
|
31
|
+
'self' to the object receiving a message.\n\n This allows you to define behavior
|
32
|
+
and apply it to an object temporarily without extending the object's super class
|
33
|
+
structure."
|
31
34
|
email:
|
32
35
|
- jim@saturnflyer.com
|
33
36
|
executables: []
|
@@ -38,6 +41,8 @@ files:
|
|
38
41
|
- lib/casting/version.rb
|
39
42
|
- test/test_helper.rb
|
40
43
|
- test/casting_test.rb
|
44
|
+
- test/casting_19_test.rb
|
45
|
+
- test/casting_20_test.rb
|
41
46
|
homepage: http://github.com/saturnflyer/casting
|
42
47
|
licenses: []
|
43
48
|
post_install_message:
|
@@ -65,4 +70,6 @@ summary: Proper method delegation.
|
|
65
70
|
test_files:
|
66
71
|
- test/test_helper.rb
|
67
72
|
- test/casting_test.rb
|
73
|
+
- test/casting_19_test.rb
|
74
|
+
- test/casting_20_test.rb
|
68
75
|
has_rdoc:
|