casting 0.6.1 → 0.6.2

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: 7a049ec2ac612c4ec99f2a08de7050e45242a69a
4
- data.tar.gz: fd53cbfb8d14d07a077f001f6d85037e3a3a4a33
3
+ metadata.gz: 83a70a78a12f99c533069a99925cd2fff9e7f1f9
4
+ data.tar.gz: eb390d0896a0e88a266bbcc6ff7a638af357523e
5
5
  SHA512:
6
- metadata.gz: b1ef3ecf6e9fe204ad27b6a5274c021df0f8d4ce7bae9543abe4a57fc7cf7dba54061f3c1bf7b296c9faf008fb75d9b188497619ad0bf3fefe0e05391e2b75e1
7
- data.tar.gz: 0a8b280a22b0c34d6cae5b1774ae8c879ab35a9a1080317abf6e3abb657bd2aabf26bbd5ff80864fa8b03517631a29805906f8ccbedc6e6771e39c23946b074b
6
+ metadata.gz: a6169cca406169f595e33fd5fe9fd85884c7a501e4369fc0c0d665f3a6da178a98c16c7d285d06998c3ab97958fff4d8a82e740ee39e726c0e412d48e9df349e
7
+ data.tar.gz: 6378fe6a71115ac772e4d5b0b1d3c24924a1133d44ce778497476ff613a2784e7d994b9b2eece23c6dca9d28b8ea8a7d1b64e200f838578f7bded9a958bbff2c
@@ -26,6 +26,7 @@ module Casting
26
26
  end
27
27
 
28
28
  def cast(delegated_method_name, attendant, *args)
29
+ validate_attendant(attendant)
29
30
  delegation(delegated_method_name).to(attendant).with(*args).call
30
31
  end
31
32
 
@@ -35,6 +36,12 @@ module Casting
35
36
 
36
37
  private
37
38
 
39
+ def validate_attendant(attendant)
40
+ if attendant == self
41
+ raise Casting::InvalidAttendant.new('client can not delegate to itself')
42
+ end
43
+ end
44
+
38
45
  def self.set_delegation_strategy(base, *which)
39
46
  which = [:instance] if which.empty?
40
47
  which.map!{|selection|
@@ -1,7 +1,10 @@
1
+ require 'casting/method_consolidator'
2
+
1
3
  module Casting
2
4
  module MissingMethodClient
3
5
 
4
6
  def cast_as(attendant)
7
+ validate_attendant(attendant)
5
8
  __delegates__.unshift(attendant)
6
9
  self
7
10
  end
@@ -11,6 +14,30 @@ module Casting
11
14
  self
12
15
  end
13
16
 
17
+ def delegated_methods(all=true)
18
+ __delegates__.flat_map{|attendant|
19
+ attendant_methods(attendant, all)
20
+ }
21
+ end
22
+
23
+ def delegated_public_methods(include_super=true)
24
+ __delegates__.flat_map{|attendant|
25
+ attendant_public_methods(attendant, include_super)
26
+ }
27
+ end
28
+
29
+ def delegated_protected_methods(include_super=true)
30
+ __delegates__.flat_map{|attendant|
31
+ attendant_protected_methods(attendant, include_super)
32
+ }
33
+ end
34
+
35
+ def delegated_private_methods(include_super=true)
36
+ __delegates__.flat_map{|attendant|
37
+ attendant_private_methods(attendant, include_super)
38
+ }
39
+ end
40
+
14
41
  private
15
42
 
16
43
  def __delegates__
@@ -31,12 +58,38 @@ module Casting
31
58
 
32
59
  def method_delegate(meth)
33
60
  __delegates__.find{|attendant|
34
- if Module === attendant
35
- attendant.instance_methods
36
- else
37
- attendant.methods
38
- end.include?(meth)
61
+ attendant_methods(attendant).include?(meth)
39
62
  }
40
63
  end
64
+
65
+ def attendant_methods(attendant, all=true)
66
+ collection = attendant_public_methods(attendant) + attendant_protected_methods(attendant)
67
+ collection += attendant_private_methods(attendant) if all
68
+ collection
69
+ end
70
+
71
+ def attendant_public_methods(attendant, include_super=true)
72
+ if Module === attendant
73
+ attendant.public_instance_methods(include_super)
74
+ else
75
+ attendant.public_methods(include_super)
76
+ end
77
+ end
78
+
79
+ def attendant_protected_methods(attendant, include_super=true)
80
+ if Module === attendant
81
+ attendant.protected_instance_methods(include_super)
82
+ else
83
+ attendant.protected_methods(include_super)
84
+ end
85
+ end
86
+
87
+ def attendant_private_methods(attendant, include_super=true)
88
+ if Module === attendant
89
+ attendant.private_instance_methods(include_super)
90
+ else
91
+ attendant.private_methods(include_super)
92
+ end
93
+ end
41
94
  end
42
95
  end
@@ -1,3 +1,3 @@
1
1
  module Casting
2
- VERSION = '0.6.1'
2
+ VERSION = '0.6.2'
3
3
  end
@@ -39,4 +39,13 @@ describe Casting::Client do
39
39
 
40
40
  assert_equal client, delegation.client
41
41
  end
42
+
43
+ it 'refuses to delegate to itself' do
44
+ client = TestPerson.new
45
+ client.extend(Casting::Client)
46
+
47
+ assert_raises(Casting::InvalidAttendant){
48
+ client.delegate('to_s', client)
49
+ }
50
+ end
42
51
  end
@@ -1,10 +1,9 @@
1
1
  require 'test_helper'
2
- require 'casting/client'
3
2
 
4
3
  describe Casting::MissingMethodClient, '#cast_as' do
5
- def client
6
- @client ||= test_person.extend(Casting::Client, Casting::MissingMethodClient)
7
- end
4
+ let(:client){
5
+ test_person.extend(Casting::Client, Casting::MissingMethodClient)
6
+ }
8
7
 
9
8
  it "sets the object's delegate for missing methods" do
10
9
  client.cast_as(TestPerson::Greeter)
@@ -12,13 +11,23 @@ describe Casting::MissingMethodClient, '#cast_as' do
12
11
  end
13
12
 
14
13
  it "delegates to objects of the same type" do
14
+ # avoid using another client
15
+ client = test_person
15
16
  client.extend(TestPerson::Greeter)
16
17
  attendant = client.clone
18
+ client.extend(Casting::Client, Casting::MissingMethodClient)
19
+
17
20
  client.singleton_class.send(:undef_method, :greet)
18
21
  client.cast_as(attendant)
19
22
  assert_equal 'hello', client.greet
20
23
  end
21
24
 
25
+ it "raises an error when given the client object" do
26
+ assert_raises(Casting::InvalidAttendant){
27
+ client.cast_as(client)
28
+ }
29
+ end
30
+
22
31
  it "returns the object for further operation" do
23
32
  jim = test_person.extend(Casting::Client, Casting::MissingMethodClient)
24
33
 
@@ -27,9 +36,9 @@ describe Casting::MissingMethodClient, '#cast_as' do
27
36
  end
28
37
 
29
38
  describe Casting::MissingMethodClient, '#uncast' do
30
- def client
31
- @client ||= test_person.extend(Casting::Client, Casting::MissingMethodClient)
32
- end
39
+ let(:client){
40
+ test_person.extend(Casting::Client, Casting::MissingMethodClient)
41
+ }
33
42
 
34
43
  it "removes the last added delegate" do
35
44
  client.cast_as(TestPerson::Greeter)
@@ -50,4 +59,114 @@ describe Casting::MissingMethodClient, '#uncast' do
50
59
 
51
60
  assert_equal 'name from TestPerson', jim.uncast.name
52
61
  end
62
+ end
63
+
64
+ describe Casting::MissingMethodClient, '#delegated_methods' do
65
+ let(:client){
66
+ object = test_person.extend(Casting::Client, Casting::MissingMethodClient)
67
+ object.cast_as(TestPerson::Greeter)
68
+ object
69
+ }
70
+
71
+ it "returns all instance methods including private from the object's delegates" do
72
+ assert_includes(client.delegated_methods(true), :psst)
73
+ end
74
+
75
+ it "returns all public instance methods from the object and it's delegates" do
76
+ refute_includes(client.delegated_methods(false), :psst)
77
+ end
78
+
79
+ it "returns all protected instance methods from the object and it's delegates" do
80
+ assert_includes(client.delegated_methods(true), :hey)
81
+ end
82
+ end
83
+
84
+ describe Casting::MissingMethodClient, '#delegated_public_methods' do
85
+ let(:client){
86
+ object = test_person.extend(Casting::Client, Casting::MissingMethodClient)
87
+ object.cast_as(TestPerson::Greeter)
88
+ object
89
+ }
90
+
91
+ it "returns all public methods from the object's delegates" do
92
+ assert_includes(client.delegated_public_methods, :greet)
93
+ end
94
+
95
+ it "excludes all private methods from the object's delegates" do
96
+ refute_includes(client.delegated_public_methods, :psst)
97
+ end
98
+
99
+ it "excludes all protected methods from the object's delegates" do
100
+ refute_includes(client.delegated_public_methods, :hey)
101
+ end
102
+
103
+ it "includes methods from superclasses" do
104
+ client.cast_as(Nested)
105
+ assert_includes(client.delegated_public_methods(true), :nested_deep)
106
+ end
107
+
108
+ it "excludes methods from superclasses" do
109
+ client.cast_as(Nested)
110
+ refute_includes(client.delegated_public_methods(false), :nested_deep)
111
+ end
112
+ end
113
+
114
+ describe Casting::MissingMethodClient, '#delegated_protected_methods' do
115
+ let(:client){
116
+ object = test_person.extend(Casting::Client, Casting::MissingMethodClient)
117
+ object.cast_as(TestPerson::Greeter)
118
+ object
119
+ }
120
+
121
+ it "excludes all public methods from the object's delegates" do
122
+ refute_includes(client.delegated_protected_methods, :greet)
123
+ end
124
+
125
+ it "excludes all private methods from the object's delegates" do
126
+ refute_includes(client.delegated_protected_methods, :psst)
127
+ end
128
+
129
+ it "includes all protected methods from the object's delegates" do
130
+ assert_includes(client.delegated_protected_methods, :hey)
131
+ end
132
+
133
+ it "includes methods from superclasses" do
134
+ client.cast_as(Nested)
135
+ assert_includes(client.delegated_protected_methods(true), :protected_nested_deep)
136
+ end
137
+
138
+ it "excludes methods from superclasses" do
139
+ client.cast_as(Nested)
140
+ refute_includes(client.delegated_protected_methods(false), :protected_nested_deep)
141
+ end
142
+ end
143
+
144
+ describe Casting::MissingMethodClient, '#delegated_private_methods' do
145
+ let(:client){
146
+ object = test_person.extend(Casting::Client, Casting::MissingMethodClient)
147
+ object.cast_as(TestPerson::Greeter)
148
+ object
149
+ }
150
+
151
+ it "excludes all public methods from the object's delegates" do
152
+ refute_includes(client.delegated_private_methods, :greet)
153
+ end
154
+
155
+ it "includes all private methods from the object's delegates" do
156
+ assert_includes(client.delegated_private_methods, :psst)
157
+ end
158
+
159
+ it "excludes all protected methods from the object's delegates" do
160
+ refute_includes(client.delegated_private_methods, :hey)
161
+ end
162
+
163
+ it "includes methods from superclasses" do
164
+ client.cast_as(Nested)
165
+ assert_includes(client.delegated_private_methods(true), :private_nested_deep)
166
+ end
167
+
168
+ it "excludes methods from superclasses" do
169
+ client.cast_as(Nested)
170
+ refute_includes(client.delegated_private_methods(false), :private_nested_deep)
171
+ end
53
172
  end
@@ -4,7 +4,9 @@ SimpleCov.start do
4
4
  end
5
5
 
6
6
  require 'coveralls'
7
- Coveralls.wear!
7
+ if ENV['COVERALLS']
8
+ Coveralls.wear!
9
+ end
8
10
 
9
11
  require 'minitest/autorun'
10
12
  require 'casting'
@@ -22,6 +24,14 @@ class TestPerson
22
24
  def greet
23
25
  'hello'
24
26
  end
27
+
28
+ protected
29
+
30
+ def hey; end
31
+
32
+ private
33
+
34
+ def psst; end
25
35
  end
26
36
 
27
37
  module Verbose
@@ -50,6 +60,24 @@ class Unrelated
50
60
  end
51
61
  end
52
62
 
63
+ module Deep
64
+ def nested_deep; end
65
+ protected
66
+ def protected_nested_deep; end
67
+ private
68
+ def private_nested_deep; end
69
+ end
70
+
71
+ module Nested
72
+ include Deep
73
+
74
+ def nested; end
75
+ protected
76
+ def protected_nested; end
77
+ private
78
+ def private_nested; end
79
+ end
80
+
53
81
  def test_person
54
82
  TestPerson.new
55
83
  end
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.1
4
+ version: 0.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Gay
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-05 00:00:00.000000000 Z
11
+ date: 2013-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redcard
@@ -82,3 +82,4 @@ test_files:
82
82
  - test/client_test.rb
83
83
  - test/delegation_test.rb
84
84
  - test/missing_method_client_test.rb
85
+ has_rdoc: