casting 0.6.6 → 0.6.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: da160756c7cff2e170c043b6c2d7ae07f9a7371c
4
- data.tar.gz: 9de07d8f49befa9d26204725323295078eb4d271
3
+ metadata.gz: 4ffec94418d01d7ecc13e0f30923c57c2bcf9eeb
4
+ data.tar.gz: 448fba5864860e5e8c06f21fe9bc11e31ba9f8d6
5
5
  SHA512:
6
- metadata.gz: e28a7b18d312c8e341293c7c0654564b8dd1ce6fd06fd2950ca43cf66b700ecb76ae1d9654dc6aa01cd6433bae9db06677e29abd26183b79fcfc5ec6edfa7a1d
7
- data.tar.gz: 839c8de909e12c87a68184542db35a0eb9b6498dff6922019cc50129b4c97c5acab29746d16722c661a4c26cd5263a78b26b6c8e23ac08f60dd391563e0ae7fa
6
+ metadata.gz: 6cd3187516eb76ff46c0a31759d961ae835d65b85ffd504620fdc9bd132d55328f2f63c1ab8b925559044509a36d7a4426634fdb21fe3f8d63e48fb841c30b2c
7
+ data.tar.gz: 2f93492b5429e9549fc011057cd79568624d710bf3eef66a768fe6ea2540c01a2cec2a0e5e99bb8b3a4ad821630c445560dc41f4c76da8ca1668151a242ae8d1
@@ -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)
@@ -19,8 +19,8 @@ module Casting
19
19
  self
20
20
  end
21
21
 
22
- def with(*args)
23
- prepared_delegation.with(*args)
22
+ def with(*args, &block)
23
+ prepared_delegation.with(*args, &block)
24
24
  self
25
25
  end
26
26
 
@@ -3,16 +3,20 @@ require 'casting/method_consolidator'
3
3
  module Casting
4
4
  module MissingMethodClient
5
5
 
6
- def cast_as(attendant)
7
- validate_attendant(attendant)
8
- attendant.cast_object(self) if attendant.respond_to?(:cast_object)
9
- __delegates__.unshift(attendant)
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
- attendant = __delegates__.shift
15
- attendant.uncast_object(self) if attendant.respond_to?(:uncast_object)
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
@@ -1,3 +1,3 @@
1
1
  module Casting
2
- VERSION = '0.6.6'
2
+ VERSION = '0.6.7'
3
3
  end
data/lib/casting.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'casting/client'
2
+ require 'casting/super_delegate'
2
3
 
3
4
  module Casting
4
5
 
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.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-02-08 00:00:00.000000000 Z
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.