class Data::Criteria

Constants

VERSION

Public Class Methods

new(opts = {}) click to toggle source
# File lib/data/criteria.rb, line 8
def initialize(opts = {})
  @matchers = {}
  add opts
end

Public Instance Methods

add(opts) click to toggle source
# File lib/data/criteria.rb, line 13
def add(opts)
  opts.each do |key, expected|
    matcher = expected.respond_to?(:call) ? expected : MatcherFactory.create(expected)
    add_matcher key, matcher
  end
  self
end
match_all?(hash) click to toggle source
# File lib/data/criteria.rb, line 21
def match_all?(hash)
  hash = HashProxy.new(hash)
  @matchers.all? do |key, matchers|
    matchers.all?{|matcher| hash.key?(key) && matcher.call(hash[key]) }
  end
end
match_any?(hash) click to toggle source
# File lib/data/criteria.rb, line 28
def match_any?(hash)
  hash = HashProxy.new(hash)
  @matchers.any? do |key, matchers|
    matchers.any?{|matcher| hash.key?(key) && matcher.call(hash[key]) }
  end
end

Private Instance Methods

add_matcher(key, matcher) click to toggle source
# File lib/data/criteria.rb, line 37
def add_matcher(key, matcher)
  @matchers[key] = @matchers.fetch(key, []).push(matcher)
end