class RSpecApi::Matchers::Filter::Matcher

Attributes

compare_with[RW]
field[RW]
value[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/rspec-api/matchers/filter/matcher.rb, line 9
def initialize(options = {})
  @field = options[:by]
  @value = options[:value]
  @compare_with = options.fetch :compare_with, :==
end

Public Instance Methods

description() click to toggle source
# File lib/rspec-api/matchers/filter/matcher.rb, line 19
def description
  if field
    %Q(be filtered by #{field}#{compare_with}#{value})
  else
    %Q(be filtered)
  end
end
matches?(response) click to toggle source
# File lib/rspec-api/matchers/filter/matcher.rb, line 15
def matches?(response)
  super && all_objects_have_field? && has_one_object? && is_filtered?
end

Private Instance Methods

all_objects_have_field?() click to toggle source
# File lib/rspec-api/matchers/filter/matcher.rb, line 29
def all_objects_have_field?
  if field
    json.all?{|item| item.is_a?(Hash) && item.key?(field)}
  else
    true
  end
end
has_one_object?() click to toggle source
# File lib/rspec-api/matchers/filter/matcher.rb, line 37
def has_one_object?
  if json.length < 1
    msg = "Cannot test filtering on an array with #{json.length} items"
    raise RSpec::Core::Pending::PendingDeclaredInExample.new msg
  else
    true
  end
end
is_filtered?() click to toggle source
# File lib/rspec-api/matchers/filter/matcher.rb, line 46
def is_filtered?
  values = json.map{|item| item[field]}
  values.all? do |v|
    if compare_with.is_a?(Proc)
      compare_with.call value, v
    elsif compare_with.is_a?(Symbol)
      v.send compare_with, value
    end
  end
end
reverse?() click to toggle source
# File lib/rspec-api/matchers/filter/matcher.rb, line 57
def reverse?
  ['desc', 'descending'].include? verse.to_s
end