Skip to content

Commit 723442a

Browse files
committed
Allow Proc as comparison operator for :be_filtered
1 parent b6460e5 commit 723442a

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
rspec-api-matchers (0.6.2)
4+
rspec-api-matchers (0.6.3)
55
activesupport
66
rack
77
rspec

lib/rspec-api/matchers/filter/matcher.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,13 @@ def has_one_object?
4545

4646
def is_filtered?
4747
values = json.map{|item| item[field]}
48-
values.all?{|v| v.send compare_with, value}
48+
values.all? do |v|
49+
if compare_with.is_a?(Proc)
50+
compare_with.call value, v
51+
elsif compare_with.is_a?(Symbol)
52+
v.send compare_with, value
53+
end
54+
end
4955
end
5056

5157
def reverse?

lib/rspec-api/matchers/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module RSpecApi
22
module Matchers
3-
VERSION = '0.6.2'
3+
VERSION = '0.6.3'
44
end
55
end

spec/rspec-api/matchers/be_filtered_spec.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,15 @@
3333
context 'given a JSON collection with 2 items with id < 5' do
3434
before { response.body = '[{"id":2},{"id":3,"name":"two"}]' }
3535

36-
it 'passes given the right comparison method' do
36+
it 'passes given the right comparison method (symbol)' do
3737
expect(response).to be_filtered by: :id, value: 5, compare_with: :<
3838
end
3939

40+
it 'passes given the right comparison method (proc)' do
41+
cmp = -> expected, actual {actual < expected}
42+
expect(response).to be_filtered by: :id, value: 5, compare_with: cmp
43+
end
44+
4045
it 'fails given the wrong comparison method' do
4146
expect {
4247
expect(response).to be_filtered by: :id, value: 5, compare_with: :>

0 commit comments

Comments
 (0)