delegate_matcher 0.2 → 0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +19 -5
- data/lib/delegate_matcher/delegate.rb +6 -5
- data/lib/delegate_matcher/delegate_matcher.rb +1 -0
- data/lib/delegate_matcher/stub_delegate.rb +0 -2
- data/lib/delegate_matcher/version.rb +1 -1
- data/spec/lib/active_support_delegation_spec.rb +5 -1
- data/spec/lib/forwardable_delegation_spec.rb +6 -1
- 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: b01a0155696471c53b2e5d758ee3acfb321fe843
|
4
|
+
data.tar.gz: 5c153cefb6be729954baf5f1a71251f929ab7d15
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d2bbb26693117c40694240b49c5f1ecc192aaa0d6bdd5a3b1dbecb6e5c1bfc4b4b6e40a6a462143139001f036726dea5ff865ffb90a2b394f15107ee411f2c9e
|
7
|
+
data.tar.gz: a52c23c8dea9252b407dc9aead40ace210095d19de17ab469547bd8514589f1e0c2ccfbf9e8c2648f43820ab533b2a4d23a4212908c50a4e25b6b12a89ac6419
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -54,7 +54,6 @@ end
|
|
54
54
|
```
|
55
55
|
|
56
56
|
### Delegate Method Name
|
57
|
-
|
58
57
|
If the name of the method being invoked on the delegate is different from the method being called you
|
59
58
|
can check this using the `with_prefix` method (based on Active Support `delegate` method) or the
|
60
59
|
`as` method.
|
@@ -67,7 +66,7 @@ describe Post do
|
|
67
66
|
end
|
68
67
|
```
|
69
68
|
|
70
|
-
**Note**: if you are delegating to an object (i.e. `to` is not a string or symbol) then a prefix of `''` will be used
|
69
|
+
**Note**: if you are delegating to an object (i.e. `to` is not a string or symbol) then a prefix of `''` will be used:
|
71
70
|
|
72
71
|
```ruby
|
73
72
|
describe Post do
|
@@ -76,6 +75,15 @@ describe Post do
|
|
76
75
|
end
|
77
76
|
```
|
78
77
|
|
78
|
+
You can test delegation to a chain of objects by using a `to` that can be `eval`'d.
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
describe Post do
|
82
|
+
it { should delegate(:name_length).to(:'author.name').as(:length) } # name_length => author.name.length
|
83
|
+
it { should delegate(:length).to(:'author.name').with_prefix(:name) } # name_length => author.name.length
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
79
87
|
### Handling Nil Delegates
|
80
88
|
If you expect the delegate to return `nil` when the delegate is `nil` rather than raising an error
|
81
89
|
then you can check this using the `allow_nil` method.
|
@@ -172,7 +180,7 @@ You can test delegation based on the [delegate](http://api.rubyonrails.org/class
|
|
172
180
|
|
173
181
|
class Author
|
174
182
|
def name
|
175
|
-
'Catherine Asaro'
|
183
|
+
@name ||= 'Catherine Asaro'
|
176
184
|
end
|
177
185
|
end
|
178
186
|
|
@@ -193,6 +201,9 @@ You can test delegation based on the [delegate](http://api.rubyonrails.org/class
|
|
193
201
|
it { should delegate(:name).to(:class).with_prefix }
|
194
202
|
it { should delegate(:count).to(:@@authors) }
|
195
203
|
it { should delegate(:first).to(:GENRES) }
|
204
|
+
|
205
|
+
it { should delegate(:name_length).to(:'author.name').as(:length) }
|
206
|
+
it { should delegate(:length).to(:'author.name').with_prefix(:name) }
|
196
207
|
end
|
197
208
|
```
|
198
209
|
However, don't use the following features as they are not supported by the delegate method:
|
@@ -219,7 +230,7 @@ You can test delegation based on the [Forwardable](http://ruby-doc.org/stdlib-2.
|
|
219
230
|
|
220
231
|
class Author
|
221
232
|
def name
|
222
|
-
'Catherine Asaro'
|
233
|
+
@name ||= 'Catherine Asaro'
|
223
234
|
end
|
224
235
|
end
|
225
236
|
|
@@ -235,6 +246,9 @@ You can test delegation based on the [Forwardable](http://ruby-doc.org/stdlib-2.
|
|
235
246
|
it { should delegate(:name).to(:author).with_block }
|
236
247
|
it { should delegate(:name).to(:author).with_prefix }
|
237
248
|
it { should delegate(:writer).to(:author).as(:name) }
|
249
|
+
|
250
|
+
it { should delegate(:name_length).to(:'author.name').as(:length) }
|
251
|
+
it { should delegate(:length).to(:'author.name').with_prefix(:name) }
|
238
252
|
end
|
239
253
|
```
|
240
254
|
However, don't use the following features as they are not supported by the Forwardable module:
|
@@ -242,7 +256,7 @@ However, don't use the following features as they are not supported by the Forwa
|
|
242
256
|
* delegation to class variables
|
243
257
|
* delegation to constants
|
244
258
|
* delegation to objects
|
245
|
-
* different arguments passed to delegate
|
259
|
+
* different arguments passed to the delegate
|
246
260
|
|
247
261
|
## Contributing
|
248
262
|
1. Fork it ( https://github.com/dwhelan/delegate_matcher/fork )
|
@@ -15,17 +15,18 @@ module RSpec
|
|
15
15
|
|
16
16
|
# rubocop:disable Metrics/AbcSize
|
17
17
|
def receiver
|
18
|
+
klass = subject.class
|
18
19
|
@receiver ||= case
|
19
20
|
when a_class_variable?
|
20
|
-
|
21
|
+
klass.class_variable_get(name)
|
21
22
|
when an_instance_variable?
|
22
23
|
subject.instance_variable_get(name)
|
23
24
|
when a_constant?
|
24
|
-
|
25
|
+
klass.const_get(name)
|
25
26
|
when a_method?
|
26
|
-
fail "#{subject.inspect} does not respond to #{name}" unless subject.respond_to?(name, true)
|
27
|
-
fail "#{subject.inspect}'s' #{name} method expects parameters" unless [0, -1].include?(subject.method(name).arity)
|
28
27
|
subject.send(name)
|
28
|
+
when a_reference?
|
29
|
+
subject.instance_eval(name)
|
29
30
|
else # is an object
|
30
31
|
to
|
31
32
|
end
|
@@ -69,7 +70,7 @@ module RSpec
|
|
69
70
|
end
|
70
71
|
|
71
72
|
def a_method?
|
72
|
-
a_reference?
|
73
|
+
a_reference? && subject.respond_to?(name, true)
|
73
74
|
end
|
74
75
|
|
75
76
|
def a_reference?
|
@@ -15,6 +15,7 @@ module ActiveSupportDelegation
|
|
15
15
|
delegate :name, to: :class, prefix: true
|
16
16
|
delegate :count, to: :@@authors
|
17
17
|
delegate :first, to: :GENRES
|
18
|
+
delegate :length, to: :'author.name', prefix: :name
|
18
19
|
|
19
20
|
def initialize
|
20
21
|
@author = Author.new
|
@@ -23,7 +24,7 @@ module ActiveSupportDelegation
|
|
23
24
|
|
24
25
|
class Author
|
25
26
|
def name
|
26
|
-
'Catherine Asaro'
|
27
|
+
@name ||= 'Catherine Asaro'
|
27
28
|
end
|
28
29
|
end
|
29
30
|
|
@@ -44,5 +45,8 @@ module ActiveSupportDelegation
|
|
44
45
|
it { should delegate(:name).to(:class).with_prefix }
|
45
46
|
it { should delegate(:count).to(:@@authors) }
|
46
47
|
it { should delegate(:first).to(:GENRES) }
|
48
|
+
|
49
|
+
it { should delegate(:name_length).to(:'author.name').as(:length) }
|
50
|
+
it { should delegate(:length).to(:'author.name').with_prefix(:name) }
|
47
51
|
end
|
48
52
|
end
|
@@ -13,11 +13,13 @@ module ForwardableDelegation
|
|
13
13
|
def_delegator :author, :name
|
14
14
|
def_delegator :author, :name, :author_name
|
15
15
|
def_delegator :author, :name, :writer
|
16
|
+
|
17
|
+
def_delegator :'author.name', :length, :name_length
|
16
18
|
end
|
17
19
|
|
18
20
|
class Author
|
19
21
|
def name
|
20
|
-
'Catherine Asaro'
|
22
|
+
@name ||= 'Catherine Asaro'
|
21
23
|
end
|
22
24
|
end
|
23
25
|
|
@@ -33,5 +35,8 @@ module ForwardableDelegation
|
|
33
35
|
it { should delegate(:name).to(:author).with_block }
|
34
36
|
it { should delegate(:name).to(:author).with_prefix }
|
35
37
|
it { should delegate(:writer).to(:author).as(:name) }
|
38
|
+
|
39
|
+
it { should delegate(:name_length).to(:'author.name').as(:length) }
|
40
|
+
it { should delegate(:length).to(:'author.name').with_prefix(:name) }
|
36
41
|
end
|
37
42
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: delegate_matcher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.3'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Declan Whelan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-12-
|
11
|
+
date: 2015-12-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: proc_extensions
|