casting 0.6.6 → 0.6.7
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.
- checksums.yaml +4 -4
- data/lib/casting/client.rb +2 -2
- data/lib/casting/delegation.rb +2 -2
- data/lib/casting/missing_method_client.rb +11 -7
- data/lib/casting/prepared_delegation.rb +5 -4
- data/lib/casting/version.rb +1 -1
- data/lib/casting.rb +1 -0
- data/test/client_test.rb +17 -0
- data/test/missing_method_client_test.rb +18 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4ffec94418d01d7ecc13e0f30923c57c2bcf9eeb
|
4
|
+
data.tar.gz: 448fba5864860e5e8c06f21fe9bc11e31ba9f8d6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6cd3187516eb76ff46c0a31759d961ae835d65b85ffd504620fdc9bd132d55328f2f63c1ab8b925559044509a36d7a4426634fdb21fe3f8d63e48fb841c30b2c
|
7
|
+
data.tar.gz: 2f93492b5429e9549fc011057cd79568624d710bf3eef66a768fe6ea2540c01a2cec2a0e5e99bb8b3a4ad821630c445560dc41f4c76da8ca1668151a242ae8d1
|
data/lib/casting/client.rb
CHANGED
@@ -25,9 +25,9 @@ module Casting
|
|
25
25
|
Casting::Delegation.new(delegated_method_name, self)
|
26
26
|
end
|
27
27
|
|
28
|
-
def cast(delegated_method_name, attendant, *args)
|
28
|
+
def cast(delegated_method_name, attendant, *args, &block)
|
29
29
|
validate_attendant(attendant)
|
30
|
-
delegation(delegated_method_name).to(attendant).with(*args).call
|
30
|
+
delegation(delegated_method_name).to(attendant).with(*args, &block).call
|
31
31
|
end
|
32
32
|
|
33
33
|
def delegate_missing_methods(*which)
|
data/lib/casting/delegation.rb
CHANGED
@@ -3,16 +3,20 @@ require 'casting/method_consolidator'
|
|
3
3
|
module Casting
|
4
4
|
module MissingMethodClient
|
5
5
|
|
6
|
-
def cast_as(
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
def cast_as(*attendants)
|
7
|
+
attendants.each do |attendant|
|
8
|
+
validate_attendant(attendant)
|
9
|
+
attendant.cast_object(self) if attendant.respond_to?(:cast_object)
|
10
|
+
__delegates__.unshift(attendant)
|
11
|
+
end
|
10
12
|
self
|
11
13
|
end
|
12
14
|
|
13
|
-
def uncast
|
14
|
-
|
15
|
-
|
15
|
+
def uncast(count=1)
|
16
|
+
count.times do
|
17
|
+
attendant = __delegates__.shift
|
18
|
+
attendant.uncast_object(self) if attendant.respond_to?(:uncast_object)
|
19
|
+
end
|
16
20
|
self
|
17
21
|
end
|
18
22
|
|
@@ -25,8 +25,8 @@ module Casting
|
|
25
25
|
class PreparedDelegation
|
26
26
|
|
27
27
|
attr_reader :client
|
28
|
-
attr_reader :delegated_method_name, :attendant, :arguments
|
29
|
-
private :delegated_method_name, :attendant, :arguments
|
28
|
+
attr_reader :delegated_method_name, :attendant, :arguments, :block
|
29
|
+
private :delegated_method_name, :attendant, :arguments, :block
|
30
30
|
|
31
31
|
def initialize(settings)
|
32
32
|
@delegated_method_name = settings[:delegated_method_name]
|
@@ -46,8 +46,9 @@ module Casting
|
|
46
46
|
self
|
47
47
|
end
|
48
48
|
|
49
|
-
def with(*args)
|
49
|
+
def with(*args, &block)
|
50
50
|
@arguments = args
|
51
|
+
@block = block
|
51
52
|
self
|
52
53
|
end
|
53
54
|
|
@@ -56,7 +57,7 @@ module Casting
|
|
56
57
|
raise MissingAttendant.new unless attendant
|
57
58
|
|
58
59
|
if arguments
|
59
|
-
delegated_method.bind(client).call(*arguments)
|
60
|
+
delegated_method.bind(client).call(*arguments, &block)
|
60
61
|
else
|
61
62
|
delegated_method.bind(client).call
|
62
63
|
end
|
data/lib/casting/version.rb
CHANGED
data/lib/casting.rb
CHANGED
data/test/client_test.rb
CHANGED
@@ -31,6 +31,23 @@ describe Casting::Client do
|
|
31
31
|
assert_equal 'hello,goodbye', client.delegate('verbose', attendant, 'hello', 'goodbye')
|
32
32
|
end
|
33
33
|
|
34
|
+
it 'executes delegated methods with a block' do
|
35
|
+
client = TestPerson.new
|
36
|
+
client.extend(Casting::Client)
|
37
|
+
mod = Module.new
|
38
|
+
mod.module_eval do
|
39
|
+
def blocky(arg, &block)
|
40
|
+
block.call(arg, self)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
output = client.delegate('blocky', mod, 'argument') do |arg, me|
|
45
|
+
%{#{arg} from #{me.name}}
|
46
|
+
end
|
47
|
+
|
48
|
+
assert_equal 'argument from name from TestPerson', output
|
49
|
+
end
|
50
|
+
|
34
51
|
it 'passes the object as the client for delegation' do
|
35
52
|
client = Object.new
|
36
53
|
client.extend(Casting::Client)
|
@@ -1,5 +1,17 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
+
module One
|
4
|
+
def similar
|
5
|
+
"from One"
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
module Two
|
10
|
+
def similar
|
11
|
+
"from Two"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
3
15
|
describe Casting::MissingMethodClient, '#cast_as' do
|
4
16
|
let(:client){
|
5
17
|
test_person.extend(Casting::Client, Casting::MissingMethodClient)
|
@@ -33,6 +45,12 @@ describe Casting::MissingMethodClient, '#cast_as' do
|
|
33
45
|
|
34
46
|
assert_equal 'hello', jim.cast_as(TestPerson::Greeter).greet
|
35
47
|
end
|
48
|
+
|
49
|
+
it "delegates methods to the last module added containing the method" do
|
50
|
+
jim = test_person.extend(Casting::Client, Casting::MissingMethodClient)
|
51
|
+
|
52
|
+
assert_equal "from Two", jim.cast_as(One, Two).similar
|
53
|
+
end
|
36
54
|
end
|
37
55
|
|
38
56
|
describe Casting::MissingMethodClient, '#uncast' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: casting
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jim Gay
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-06-18 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |-
|
14
14
|
Casting assists in method delegation which preserves the binding of 'self' to the object receiving a message.
|