delegate_matcher 0.2 → 0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d9489b0d0927d6b2af4766edaf7edc2d4788867e
4
- data.tar.gz: d27938e9a9fe51eeb68dbfccdc807caf07883d0a
3
+ metadata.gz: b01a0155696471c53b2e5d758ee3acfb321fe843
4
+ data.tar.gz: 5c153cefb6be729954baf5f1a71251f929ab7d15
5
5
  SHA512:
6
- metadata.gz: ea5fb6b338a5f50a1ef0b1f07d5b3c3d54e54a054b406c6cd7536213509cf04e2a1925da61707311e55a8016405150e5f32c27dedcca19d1ae75021daef464e9
7
- data.tar.gz: 9ebfe282eb0fd612c71cf5ba18618c6ff51a5025f7ecac20795a788d8de5c5acef7b3a9fcfc88ab174050964ccf945703159a20ab8f0068bf8b1b7c290724409
6
+ metadata.gz: d2bbb26693117c40694240b49c5f1ecc192aaa0d6bdd5a3b1dbecb6e5c1bfc4b4b6e40a6a462143139001f036726dea5ff865ffb90a2b394f15107ee411f2c9e
7
+ data.tar.gz: a52c23c8dea9252b407dc9aead40ace210095d19de17ab469547bd8514589f1e0c2ccfbf9e8c2648f43820ab533b2a4d23a4212908c50a4e25b6b12a89ac6419
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- delegate_matcher (0.2)
4
+ delegate_matcher (0.3)
5
5
  proc_extensions (~> 0.1)
6
6
 
7
7
  GEM
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
- subject.class.class_variable_get(name)
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
- subject.class.const_get(name)
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?
@@ -54,3 +54,4 @@ module RSpec
54
54
  end
55
55
 
56
56
  # TODO: Handle nested delegation strings "a.b.c"
57
+ # TODO: Test SingleForwardable
@@ -13,12 +13,10 @@ module RSpec
13
13
  private
14
14
 
15
15
  def stub_receiver
16
- # binding.pry
17
16
  allow(receiver).to receive(expected.as) do |*args, &block|
18
17
  self.args = args
19
18
  self.block = block
20
19
  self.received = true
21
- # binding.pry
22
20
  return_value
23
21
  end
24
22
  end
@@ -1,3 +1,3 @@
1
1
  module DelegateMatcher
2
- VERSION ||= '0.2'.freeze
2
+ VERSION ||= '0.3'.freeze
3
3
  end
@@ -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.2'
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-15 00:00:00.000000000 Z
11
+ date: 2015-12-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: proc_extensions