Class: Data::Criteria

Inherits:
Object
  • Object
show all
Defined in:
lib/data/criteria.rb,
lib/data/criteria/matcher.rb,
lib/data/criteria/version.rb,
lib/data/criteria/hash_proxy.rb,
lib/data/criteria/matcher_factory.rb

Defined Under Namespace

Classes: BaseMatcher, EqMatcher, HashProxy, InMatcher, MatcherFactory, NumericComparisonMatcher, RegexpMatcher

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Criteria

Returns a new instance of Criteria.



8
9
10
11
# File 'lib/data/criteria.rb', line 8

def initialize(opts = {})
  @matchers = {}
  add opts
end

Instance Method Details

#add(opts) ⇒ Object



13
14
15
16
17
18
19
# 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) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
# 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) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
# 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