casting 0.6.9 → 0.7.0

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: 817477d1f96444e97690553a806289ef4abfce2f
4
- data.tar.gz: f50a67f05c760518b3e2b1b93e817db06c8a2adf
3
+ metadata.gz: 0cca3576453e1acf20799cd80afe5a2e9e4c39cc
4
+ data.tar.gz: f892e78276d01554fa159d893507c61fcb4dcf37
5
5
  SHA512:
6
- metadata.gz: 19dd0a57a2a785282fd3eb7bdce9ab973c332b13530000f8419a3fbc6ee69476fddd585073bf72d9b16318c960cdfe9c7c04b496071323a734bd2874e4977a9e
7
- data.tar.gz: 88f35215738b381bdac0893fd3e4946c435e8aed8b62876e454f2674b316f53c31b07a93a956d412cc36d93190b2d5fd7ae648885d7f20e9ab29d013f91a52b3
6
+ metadata.gz: 68f289b4009fd5330f856fda1ce71f190ab8317fbf0aa343b9b4244afc6bcdb613be34457c3b98ceb120b620e889511b285099d1006cd6f679be874a6a0a1251
7
+ data.tar.gz: 75e7c54b13bad49081fd15597adc4891f3ebfdb5c66b423b0278ecbfd8a3dd48ff22ab1fcc6a2b40b7dae19a29d5e31d1a5a3b1a0986f9250c67e2c3c52c3950
@@ -10,7 +10,7 @@ module Casting
10
10
  Casting::Client.set_delegation_strategy(self, *which.reverse)
11
11
  end
12
12
 
13
- unless base.instance_methods.include?('delegate')
13
+ unless base.method_defined?('delegate')
14
14
  add_delegate_method_to(base)
15
15
  end
16
16
  end
@@ -27,11 +27,7 @@ module Casting
27
27
 
28
28
  def method_class_delegate(meth)
29
29
  __class_delegates__.find{|attendant|
30
- if Module === attendant
31
- attendant.instance_methods
32
- else
33
- attendant.methods
34
- end.include?(meth)
30
+ attendant.method_defined?(meth)
35
31
  }
36
32
  end
37
33
  end
@@ -1,17 +1,3 @@
1
- # Some features are only available in versions of Ruby
2
- # where this method is true
3
- unless defined?(self.module_method_rebinding?)
4
- def module_method_rebinding?
5
- return @__module_method_rebinding__ if defined?(@__module_method_rebinding__)
6
- sample_method = Enumerable.instance_method(:to_a)
7
- @__module_method_rebinding__ = begin
8
- !!sample_method.bind(Object.new)
9
- rescue TypeError
10
- false
11
- end
12
- end
13
- end
14
-
15
1
  module Casting
16
2
 
17
3
  class MissingAttendant < StandardError
@@ -36,11 +22,10 @@ module Casting
36
22
  end
37
23
 
38
24
  def to(object_or_module)
39
- @attendant = method_carrier(object_or_module)
25
+ @attendant = object_or_module
40
26
  begin
41
27
  check_valid_type
42
28
  rescue TypeError
43
- raise unless module_method_rebinding?
44
29
  @attendant = method_module || raise
45
30
  end
46
31
  self
@@ -69,19 +54,7 @@ module Casting
69
54
  begin
70
55
  !client.nil? && delegated_method.bind(client)
71
56
  rescue TypeError
72
- raise TypeError.new("`to' argument must be a module or an instance of #{client.class}")
73
- end
74
- end
75
-
76
- def method_carrier(object_or_module)
77
- if Module === object_or_module
78
- if module_method_rebinding?
79
- object_or_module
80
- else
81
- client.clone.extend(object_or_module)
82
- end
83
- else
84
- object_or_module
57
+ raise TypeError.new("`to' argument must be a module or an object with #{delegated_method_name} defined in a module")
85
58
  end
86
59
  end
87
60
 
@@ -93,7 +66,7 @@ module Casting
93
66
  if Module === attendant
94
67
  attendant.instance_method(delegated_method_name)
95
68
  else
96
- attendant.method(delegated_method_name).unbind
69
+ attendant.method(delegated_method_name).owner.instance_method(delegated_method_name)
97
70
  end
98
71
  rescue NameError => e
99
72
  raise InvalidAttendant.new(e.message)
@@ -1,3 +1,3 @@
1
1
  module Casting
2
- VERSION = '0.6.9'
2
+ VERSION = '0.7.0'
3
3
  end
@@ -2,8 +2,6 @@ require 'test_helper'
2
2
 
3
3
  describe Casting, '.delegating' do
4
4
  it 'delegates missing methods to object delegates' do
5
- skip 'extending objects not used in this version of Ruby' if test_rebinding_methods?
6
-
7
5
  client = test_person
8
6
  client.extend(Casting::Client)
9
7
  client.delegate_missing_methods
@@ -26,8 +24,6 @@ end
26
24
  describe Casting::Delegation do
27
25
 
28
26
  it 'calls a method defined on another object of the same type' do
29
- skip 'extending objects not used in this version of Ruby' if test_rebinding_methods?
30
-
31
27
  client = test_person
32
28
  attendant = test_person
33
29
  attendant.extend(TestPerson::Greeter)
@@ -36,8 +32,6 @@ describe Casting::Delegation do
36
32
  end
37
33
 
38
34
  it 'passes arguments to a delegated method' do
39
- skip 'extending objects not used in this version of Ruby' if test_rebinding_methods?
40
-
41
35
  client = test_person
42
36
  attendant = test_person
43
37
  attendant.extend(TestPerson::Verbose)
@@ -46,16 +40,12 @@ describe Casting::Delegation do
46
40
  end
47
41
 
48
42
  it 'delegates when given a module' do
49
- skip 'extending objects not used in this version of Ruby' if test_rebinding_methods?
50
-
51
43
  client = test_person
52
44
  delegation = Casting::Delegation.new('greet', client).to(TestPerson::Greeter)
53
45
  assert_equal 'hello', delegation.call
54
46
  end
55
47
 
56
48
  it 'does not delegate when given a class' do
57
- skip 'extending objects not used in this version of Ruby' if test_rebinding_methods?
58
-
59
49
  client = test_person
60
50
  assert_raises(TypeError){
61
51
  Casting::Delegation.new('class_defined', client).to(Unrelated)
@@ -3,8 +3,6 @@ require 'test_helper'
3
3
  describe Casting::Delegation do
4
4
 
5
5
  it 'finds the module defining a method and uses it to delegate' do
6
- skip 'not able to bind module methods in this version of Ruby' unless test_rebinding_methods?
7
-
8
6
  client = test_person
9
7
  attendant = Unrelated.new
10
8
  delegation = Casting::Delegation.new('unrelated', client).to(attendant)
@@ -12,8 +10,6 @@ describe Casting::Delegation do
12
10
  end
13
11
 
14
12
  it 'does not delegate to methods defined in classes' do
15
- skip 'not able to bind module methods in this version of Ruby' unless test_rebinding_methods?
16
-
17
13
  client = test_person
18
14
  attendant = Unrelated.new
19
15
  assert_raises(TypeError){
@@ -65,4 +65,17 @@ describe Casting::Client do
65
65
  client.delegate('to_s', client)
66
66
  }
67
67
  end
68
+
69
+ it 'does not delegate singleton methods' do
70
+ client = test_person.extend(Casting::Client)
71
+ client.delegate_missing_methods
72
+ attendant = test_person
73
+
74
+ def attendant.hello
75
+ 'hello'
76
+ end
77
+ assert_raises(TypeError){
78
+ client.delegate('hello', attendant)
79
+ }
80
+ end
68
81
  end
@@ -8,9 +8,12 @@ describe Casting::Delegation do
8
8
 
9
9
  it 'raises an error when calling without an attendant object' do
10
10
  delegation = Casting::Delegation.new('some_method', Object.new)
11
- assert_raises(Casting::MissingAttendant){
12
- delegation.call
13
- }
11
+ begin
12
+ delegation.call
13
+ rescue StandardError => e
14
+ end
15
+ assert_kind_of Casting::MissingAttendant, e
16
+ assert_equal "You must set your attendant object using `to'.", e.message
14
17
  end
15
18
 
16
19
  it 'raises an error when setting an invalid attendant type' do
@@ -77,6 +77,19 @@ describe Casting::MissingMethodClient, '#uncast' do
77
77
 
78
78
  assert_equal 'name from TestPerson', jim.uncast.name
79
79
  end
80
+
81
+ it "removes the specified number of delegates" do
82
+ jim = test_person.extend(Casting::Client, Casting::MissingMethodClient)
83
+ jim.cast_as(TestPerson::Greeter, TestPerson::Verbose)
84
+
85
+ assert_includes(jim.delegated_methods(true), :psst)
86
+ assert_includes(jim.delegated_methods(true), :verbose)
87
+
88
+ jim.uncast(2)
89
+
90
+ refute_includes(jim.delegated_methods(true), :psst)
91
+ refute_includes(jim.delegated_methods(true), :verbose)
92
+ end
80
93
  end
81
94
 
82
95
  describe Casting::MissingMethodClient, '#delegated_methods' do
@@ -1,23 +1,21 @@
1
1
  require 'test_helper'
2
2
 
3
- if test_rebinding_methods?
4
- describe Casting::Null do
5
- it 'will answer to any method with nil' do
6
- client = TestPerson.new
7
- client.extend(Casting::Client)
8
- attendant = Casting::Null
3
+ describe Casting::Null do
4
+ it 'will answer to any method with nil' do
5
+ client = TestPerson.new
6
+ client.extend(Casting::Client)
7
+ attendant = Casting::Null
9
8
 
10
- assert_nil client.delegate('greet', attendant)
11
- end
9
+ assert_nil client.delegate('greet', attendant)
12
10
  end
11
+ end
13
12
 
14
- describe Casting::Blank do
15
- it 'will answer to any method with an empty string' do
16
- client = TestPerson.new
17
- client.extend(Casting::Client)
18
- attendant = Casting::Blank
13
+ describe Casting::Blank do
14
+ it 'will answer to any method with an empty string' do
15
+ client = TestPerson.new
16
+ client.extend(Casting::Client)
17
+ attendant = Casting::Blank
19
18
 
20
- assert_empty client.delegate('greet', attendant)
21
- end
19
+ assert_empty client.delegate('greet', attendant)
22
20
  end
23
21
  end
@@ -22,7 +22,6 @@ end
22
22
 
23
23
  describe Casting, 'modules using delegate_super' do
24
24
  it 'call the method from the next delegate with the same arguments' do
25
- skip 'extending objects not used in this version of Ruby' unless test_rebinding_methods?
26
25
  client = TestPerson.new.extend(Casting::Client)
27
26
  client.delegate_missing_methods
28
27
  client.cast_as(AnyWay, ThisWay, ThatWay)
@@ -81,18 +81,3 @@ end
81
81
  def test_person
82
82
  TestPerson.new
83
83
  end
84
-
85
-
86
-
87
- # This is a different implementation of module_method_rebinding?
88
- # created in order to check that the behavior of the code is correct.
89
- #
90
- # This method is used in tests and module_method_rebinding? is used
91
- # in the library code.
92
- def test_rebinding_methods?
93
- unbound = Enumerable.instance_method(:count)
94
- unbound.bind(Object.new)
95
- true
96
- rescue TypeError
97
- false
98
- end
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.6.9
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Gay