Skip to content

Commit

Permalink
adding support for directives
Browse files Browse the repository at this point in the history
  • Loading branch information
morkeleb committed Jun 22, 2013
1 parent f727ed6 commit 9dc96b9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/embedded-mongo/backend/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,18 @@ def sort_cmp(sort, x, y)

def selector_match?(selector, doc)
raise NotImplementedError.new('Does not current support $where queries') if selector.has_key?('$where')
selector.all? { |k, v| partial_match?(v, doc[k]) }
selector.all? do |k, v|
if(k == "$or")
v.any? do |partial_selector|
selector_match?(partial_selector, doc)
end
else
partial_match?(v, doc[k])
end
end
end


def partial_match?(partial_selector, value)
EmbeddedMongo.log.debug("partial_match? #{partial_selector.inspect} #{value.inspect}")
case partial_selector
Expand All @@ -170,7 +179,7 @@ def partial_match?(partial_selector, value)
raise NotImplementedError.new("Cannot mix $ directives with non: #{partial_selector.inspect}") if has_non_directive?(partial_selector)
partial_selector.all? do |k, v|
directive_match?(k, v, value)
end
end
end
else
raise "Unsupported selector #{partial_selector.inspect}"
Expand Down
11 changes: 11 additions & 0 deletions test/functional/interface_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,15 @@ def test_sort
assert_equal(10, res2[1]['a'])
assert_equal(nil, res2[2]['a'])
end

def test_or_support
@foo_collection.insert({ 'e' => 10 })
@foo_collection.insert({ 'e' => 20 })
@foo_collection.insert({ 'e' => 30 })

cursor1 = @foo_collection.find(:$or=>[{'e'=>10}, {'e'=>20}])
res1 = cursor1.to_a
assert_equal(2, res1.length)
p res1
end
end

0 comments on commit 9dc96b9

Please sign in to comment.