casting 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/casting.rb +48 -1
- data/lib/casting/version.rb +1 -1
- data/test/casting_test.rb +37 -0
- data/test/test_helper.rb +1 -1
- metadata +13 -8
- checksums.yaml +0 -7
data/lib/casting.rb
CHANGED
@@ -2,6 +2,19 @@ require 'redcard'
|
|
2
2
|
|
3
3
|
module Casting
|
4
4
|
|
5
|
+
def self.delegating(assignments)
|
6
|
+
assignments.each do |object, mod|
|
7
|
+
object.send(:instance_variable_set, :@__previous_delegate__, object.instance_variable_get(:@__current_delegate__))
|
8
|
+
object.send(:instance_variable_set, :@__current_delegate__, mod)
|
9
|
+
end
|
10
|
+
yield
|
11
|
+
ensure
|
12
|
+
assignments.each do |object, mod|
|
13
|
+
object.send(:instance_variable_set, :@__current_delegate__, object.instance_variable_get(:@__previous_delegate__))
|
14
|
+
object.send(:remove_instance_variable, :@__previous_delegate__)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
5
18
|
class MissingAttendant < StandardError
|
6
19
|
def message
|
7
20
|
"You must set your attendant object using `to'."
|
@@ -12,6 +25,13 @@ module Casting
|
|
12
25
|
end
|
13
26
|
|
14
27
|
module Client
|
28
|
+
|
29
|
+
def self.included(base)
|
30
|
+
def base.delegate_missing_methods
|
31
|
+
self.send(:include, ::Casting::MissingMethodClient)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
15
35
|
def delegation(delegated_method_name)
|
16
36
|
Casting::Delegation.new(delegated_method_name, self)
|
17
37
|
end
|
@@ -19,6 +39,32 @@ module Casting
|
|
19
39
|
def delegate(delegated_method_name, attendant, *args)
|
20
40
|
delegation(delegated_method_name).to(attendant).with(*args).call
|
21
41
|
end
|
42
|
+
|
43
|
+
def delegate_missing_methods
|
44
|
+
self.extend ::Casting::MissingMethodClient
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
module MissingMethodClient
|
49
|
+
def method_missing(meth, *args, &block)
|
50
|
+
if delegate_has_method?(meth)
|
51
|
+
delegate(meth, @__current_delegate__, *args, &block)
|
52
|
+
else
|
53
|
+
super
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def delegate_has_method?(meth)
|
60
|
+
return false unless @__current_delegate__
|
61
|
+
|
62
|
+
if Module === @__current_delegate__
|
63
|
+
@__current_delegate__.instance_methods
|
64
|
+
else
|
65
|
+
@__current_delegate__.methods
|
66
|
+
end.include?(meth)
|
67
|
+
end
|
22
68
|
end
|
23
69
|
|
24
70
|
class PreparedDelegation
|
@@ -95,6 +141,7 @@ module Casting
|
|
95
141
|
rescue NameError => e
|
96
142
|
raise InvalidAttendant.new(e.message)
|
97
143
|
end
|
144
|
+
|
98
145
|
end
|
99
146
|
|
100
147
|
class Delegation
|
@@ -125,4 +172,4 @@ module Casting
|
|
125
172
|
end
|
126
173
|
|
127
174
|
end
|
128
|
-
end
|
175
|
+
end
|
data/lib/casting/version.rb
CHANGED
data/test/casting_test.rb
CHANGED
@@ -1,6 +1,43 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
require 'casting'
|
3
3
|
|
4
|
+
BlockTestPerson = Struct.new(:name)
|
5
|
+
BlockTestPerson.send(:include, Casting::Client)
|
6
|
+
BlockTestPerson.delegate_missing_methods
|
7
|
+
|
8
|
+
describe Casting, '.delegating' do
|
9
|
+
it 'delegates missing methods for the objects inisde the block' do
|
10
|
+
client = BlockTestPerson.new('Jim')
|
11
|
+
verbose_client = BlockTestPerson.new('Amy')
|
12
|
+
|
13
|
+
assert_raises(NoMethodError){
|
14
|
+
client.greet
|
15
|
+
}
|
16
|
+
Casting.delegating(client => TestPerson::Greeter, verbose_client => TestPerson::Verbose) do
|
17
|
+
assert_equal 'hello', client.greet
|
18
|
+
assert_equal 'this,that', verbose_client.verbose('this','that')
|
19
|
+
end
|
20
|
+
assert_raises(NoMethodError){
|
21
|
+
client.greet
|
22
|
+
}
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'delegates missing methods on altered objects inside the block' do
|
26
|
+
client = test_person.extend(Casting::Client)
|
27
|
+
client.delegate_missing_methods
|
28
|
+
|
29
|
+
assert_raises(NoMethodError){
|
30
|
+
client.greet
|
31
|
+
}
|
32
|
+
Casting.delegating(client => TestPerson::Greeter) do
|
33
|
+
assert_equal 'hello', client.greet
|
34
|
+
end
|
35
|
+
assert_raises(NoMethodError){
|
36
|
+
client.greet
|
37
|
+
}
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
4
41
|
describe Casting::Delegation do
|
5
42
|
|
6
43
|
it 'initializes with method name and object' do
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,18 +1,20 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: casting
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
7
|
authors:
|
7
8
|
- Jim Gay
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date: 2013-05-
|
12
|
+
date: 2013-05-22 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: redcard
|
15
16
|
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
16
18
|
requirements:
|
17
19
|
- - ~>
|
18
20
|
- !ruby/object:Gem::Version
|
@@ -20,6 +22,7 @@ dependencies:
|
|
20
22
|
type: :runtime
|
21
23
|
prerelease: false
|
22
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
23
26
|
requirements:
|
24
27
|
- - ~>
|
25
28
|
- !ruby/object:Gem::Version
|
@@ -35,29 +38,31 @@ files:
|
|
35
38
|
- lib/casting/version.rb
|
36
39
|
- test/test_helper.rb
|
37
40
|
- test/casting_test.rb
|
38
|
-
homepage:
|
41
|
+
homepage: http://github.com/saturnflyer/casting
|
39
42
|
licenses: []
|
40
|
-
metadata: {}
|
41
43
|
post_install_message:
|
42
44
|
rdoc_options: []
|
43
45
|
require_paths:
|
44
46
|
- lib
|
45
47
|
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
46
49
|
requirements:
|
47
|
-
- - '>='
|
50
|
+
- - ! '>='
|
48
51
|
- !ruby/object:Gem::Version
|
49
52
|
version: '0'
|
50
53
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
51
55
|
requirements:
|
52
|
-
- - '>='
|
56
|
+
- - ! '>='
|
53
57
|
- !ruby/object:Gem::Version
|
54
58
|
version: '0'
|
55
59
|
requirements: []
|
56
60
|
rubyforge_project:
|
57
|
-
rubygems_version:
|
61
|
+
rubygems_version: 1.8.23
|
58
62
|
signing_key:
|
59
|
-
specification_version:
|
63
|
+
specification_version: 3
|
60
64
|
summary: Proper method delegation.
|
61
65
|
test_files:
|
62
66
|
- test/test_helper.rb
|
63
67
|
- test/casting_test.rb
|
68
|
+
has_rdoc:
|
checksums.yaml
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
---
|
2
|
-
SHA1:
|
3
|
-
metadata.gz: 4cd9c772929ade9385c386920a6983f76b7d4499
|
4
|
-
data.tar.gz: 30a9a0733b90d6b228bee245177c70ef59c75ea5
|
5
|
-
SHA512:
|
6
|
-
metadata.gz: 9f8fe2c025d614134d088537c2afe2f9513c7d474ab1d9e02d19e57cd2c0a03497ed35dab6bdacf268892dea6c167090abe2f48b39032c2cbf8daeaf27cbef98
|
7
|
-
data.tar.gz: 6df689773211f4dab302fee36785709c883addb19672c0c662f9df0d4c9a3b8f8e2064f7d2625d222e8c1221d8f5b3b64b5f113722a033fc677288314415fdbe
|