Class: Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ Matcher

Returns a new instance of Matcher.



4
5
6
# File 'lib/matcher.rb', line 4

def initialize(query)
  @query = query
end

Instance Method Details

#match(obj) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/matcher.rb', line 8

def match(obj)
  if @query.length > 1
    # We always have an implied $and for multiple entries
    matcher = AndMatcher.new({"$and" => @query.map{|k,v| {k => v}}})
  elsif @query.length == 0
    matcher = EmptyMatcher.new(@query)
  else 
    # we only have one entry
    case @query.keys.first
    when "$or"
      matcher = OrMatcher.new(@query)
    when "$nor"
      matcher = NorMatcher.new(@query)
    when "$and"
      matcher = AndMatcher.new(@query)
    else
      matcher = AttrMatcher.new(@query)
    end
  end
  return matcher.match(obj)
end