Created
March 12, 2010 17:03
-
-
Save bkeepers/330527 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Hash | |
def rmerge!(other_hash) | |
merge!(other_hash) do |key, oldval, newval| | |
oldval.class == self.class ? oldval.rmerge!(newval) : newval | |
end | |
end | |
def rmerge(other_hash) | |
r = {} | |
merge(other_hash) do |key, oldval, newval| | |
r[key] = oldval.class == self.class ? oldval.rmerge(newval) : newval | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Object | |
def to_mm_criteria(value) | |
SymbolOperator.new(self, 'eq').to_mm_criteria(value) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module MongoMapper | |
class Query | |
def initialize | |
@criteria = {} | |
end | |
def where(criteria) | |
criteria.each do |key, value| | |
@criteria.rmerge!(key.to_mm_criteria(value)) | |
end | |
self | |
end | |
def criteria | |
@criteria | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test_helper' | |
require 'models' | |
class QueryTest < Test::Unit::TestCase | |
include MongoMapper | |
context "where" do | |
should "generate criteria" do | |
Query.new.where(:foo => 'bar').criteria.should == {:foo => {'$eq' => 'bar'}} | |
end | |
should "merge criteria" do | |
Query.new.where(:foo => 'bar').where(:hello => 'world').criteria.should == { | |
:foo => {'$eq' => 'bar'}, :hello => {'$eq' => 'world'} | |
} | |
end | |
should "merge criteria on same attribute" do | |
Query.new.where(:foo => 'bar').where(:foo.ne => 'baz').criteria.should == { | |
:foo => {'$eq' => 'bar', '$ne' => 'baz'} | |
} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment