Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use symbols everywhere for modifiers.

Doing this exposed an issue where modifiers were being doubled up in
nesting. Fixed that as well.
  • Loading branch information...
commit 6d347f66fb8b65fb67de1a39bb8f2ddd25aae7f3 1 parent 7823894
@jnunemaker jnunemaker authored
View
2  lib/plucky/criteria_hash.rb
@@ -36,7 +36,7 @@ def []=(key, value)
normalized_key = normalized_key(key)
if key.is_a?(SymbolOperator)
- operator = "$#{key.operator}"
+ operator = :"$#{key.operator}"
normalized_value = normalized_value(normalized_key, operator, value)
source[normalized_key] ||= {}
source[normalized_key][operator] = normalized_value
View
8 lib/plucky/normalizers/criteria_hash_value.rb
@@ -26,9 +26,9 @@ def call(parent_key, key, value)
if nesting_operator?(key)
value.map { |v| criteria_hash_class.new(v, options).to_hash }
- elsif parent_key == key
+ elsif parent_key == key && !modifier?(key)
# we're not nested and not the value for a symbol operator
- {'$in' => value.to_a}
+ {:$in => value.to_a}
else
# we are a value for a symbol operator or nested hash
value.to_a
@@ -74,6 +74,10 @@ def object_id?(key)
def nesting_operator?(key)
NestingOperators.include?(key)
end
+
+ def modifier?(key)
+ Plucky.modifier?(key)
+ end
end
end
end
View
40 spec/plucky/criteria_hash_spec.rb
@@ -77,7 +77,7 @@
criteria = described_class.new
criteria[:age.gt] = 20
criteria[:age.lt] = 10
- criteria.source[:age].should eq({'$gt' => 20, '$lt' => 10})
+ criteria.source[:age].should eq({:$gt => 20, :$lt => 10})
end
end
end
@@ -86,50 +86,50 @@
it "works when no keys match" do
c1 = described_class.new(:foo => 'bar')
c2 = described_class.new(:baz => 'wick')
- c1.merge(c2).should == described_class.new(:foo => 'bar', :baz => 'wick')
+ c1.merge(c2).source.should eq(:foo => 'bar', :baz => 'wick')
end
it "turns matching keys with simple values into array" do
c1 = described_class.new(:foo => 'bar')
c2 = described_class.new(:foo => 'baz')
- c1.merge(c2).should == described_class.new(:foo => {'$in' => %w[bar baz]})
+ c1.merge(c2).source.should eq(:foo => {:$in => %w[bar baz]})
end
it "uniques matching key values" do
c1 = described_class.new(:foo => 'bar')
c2 = described_class.new(:foo => 'bar')
- c1.merge(c2).should == described_class.new(:foo => {'$in' => %w[bar]})
+ c1.merge(c2).source.should eq(:foo => {:$in => %w[bar]})
end
it "correctly merges arrays and non-arrays" do
c1 = described_class.new(:foo => 'bar')
c2 = described_class.new(:foo => %w[bar baz])
- c1.merge(c2).should == described_class.new(:foo => {'$in' => %w[bar baz]})
- c2.merge(c1).should == described_class.new(:foo => {'$in' => %w[bar baz]})
+ c1.merge(c2).source.should eq(:foo => {:$in => %w[bar baz]})
+ c2.merge(c1).source.should eq(:foo => {:$in => %w[bar baz]})
end
it "is able to merge two modifier hashes" do
- c1 = described_class.new('$in' => [1, 2])
- c2 = described_class.new('$in' => [2, 3])
- c1.merge(c2).should == described_class.new('$in' => [1, 2, 3])
+ c1 = described_class.new(:$in => [1, 2])
+ c2 = described_class.new(:$in => [2, 3])
+ c1.merge(c2).source.should eq(:$in => [1, 2, 3])
end
it "is able to merge two modifier hashes with hash values" do
- c1 = described_class.new(:arr => {'$elemMatch' => {:foo => 'bar'}})
- c2 = described_class.new(:arr => {'$elemMatch' => {:omg => 'ponies'}})
- c1.merge(c2).should == described_class.new(:arr => {'$elemMatch' => {:foo => 'bar', :omg => 'ponies'}})
+ c1 = described_class.new(:arr => {:$elemMatch => {:foo => 'bar'}})
+ c2 = described_class.new(:arr => {:$elemMatch => {:omg => 'ponies'}})
+ c1.merge(c2).source.should eq(:arr => {:$elemMatch => {:foo => 'bar', :omg => 'ponies'}})
end
it "merges matching keys with a single modifier" do
- c1 = described_class.new(:foo => {'$in' => [1, 2, 3]})
- c2 = described_class.new(:foo => {'$in' => [1, 4, 5]})
- c1.merge(c2).should == described_class.new(:foo => {'$in' => [1, 2, 3, 4, 5]})
+ c1 = described_class.new(:foo => {:$in => [1, 2, 3]})
+ c2 = described_class.new(:foo => {:$in => [1, 4, 5]})
+ c1.merge(c2).source.should eq(:foo => {:$in => [1, 2, 3, 4, 5]})
end
it "merges matching keys with multiple modifiers" do
- c1 = described_class.new(:foo => {'$in' => [1, 2, 3]})
- c2 = described_class.new(:foo => {'$all' => [1, 4, 5]})
- c1.merge(c2).should == described_class.new(:foo => {'$in' => [1, 2, 3], '$all' => [1, 4, 5]})
+ c1 = described_class.new(:foo => {:$in => [1, 2, 3]})
+ c2 = described_class.new(:foo => {:$all => [1, 4, 5]})
+ c1.merge(c2).source.should eq(:foo => {:$in => [1, 2, 3], :$all => [1, 4, 5]})
end
it "does not update mergee" do
@@ -141,11 +141,11 @@
end
context "#merge!" do
- it "merges and replace" do
+ it "updates mergee" do
c1 = described_class.new(:foo => 'bar')
c2 = described_class.new(:foo => 'baz')
c1.merge!(c2)
- c1[:foo].should == {'$in' => ['bar', 'baz']}
+ c1[:foo].should == {:$in => ['bar', 'baz']}
end
end
View
26 spec/plucky/normalizers/criteria_hash_value_spec.rb
@@ -44,10 +44,16 @@
context "with an array" do
it "defaults to $in" do
actual = [1,2,3]
- expected = {'$in' => [1,2,3]}
+ expected = {:$in => [1,2,3]}
subject.call(:foo, :foo, actual).should eq(expected)
end
+ it "does not double up $in" do
+ actual = [1, 2, 3]
+ expected = [1, 2, 3]
+ subject.call(:$in, :$in, actual).should eq(expected)
+ end
+
it "uses existing modifier if present" do
actual = {'$all' => [1,2,3]}
expected = {'$all' => [1,2,3]}
@@ -78,7 +84,7 @@
it "defaults to $in even with ObjectId keys" do
actual = [1,2,3]
- expected = {'$in' => [1,2,3]}
+ expected = {:$in => [1,2,3]}
criteria_hash.object_ids = [:mistake_id]
subject.call(:mistake_id, :mistake_id, actual).should eq(expected)
end
@@ -87,7 +93,7 @@
context "with a set" do
it "defaults to $in and convert to array" do
actual = [1,2,3].to_set
- expected = {'$in' => [1,2,3]}
+ expected = {:$in => [1,2,3]}
subject.call(:numbers, :numbers, actual).should eq(expected)
end
@@ -129,13 +135,13 @@
let(:oids) { [oid1.to_s, oid2.to_s] }
it "converts strings to object ids" do
- actual = {'$in' => oids}
- expected = {'$in' => [oid1, oid2]}
+ actual = {:$in => oids}
+ expected = {:$in => [oid1, oid2]}
subject.call(:_id, :_id, actual).should eq(expected)
end
it "does not modify original array of string ids" do
- subject.call(:_id, :_id, {'$in' => oids})
+ subject.call(:_id, :_id, {:$in => oids})
oids.should == [oid1.to_s, oid2.to_s]
end
end
@@ -150,9 +156,9 @@
context "with #{operator}" do
it "works with symbol operators" do
nested1 = {:age.gt => 12, :age.lt => 20}
- translated1 = {:age => {'$gt' => 12, '$lt' => 20 }}
+ translated1 = {:age => {:$gt => 12, :$lt => 20 }}
nested2 = {:type.nin => ['friend', 'enemy']}
- translated2 = {:type => {'$nin' => ['friend', 'enemy']}}
+ translated2 = {:type => {:$nin => ['friend', 'enemy']}}
value = [nested1, nested2]
expected = [translated1, translated2]
@@ -173,9 +179,9 @@
context "doubly nested" do
it "works with symbol operators" do
nested1 = {:age.gt => 12, :age.lt => 20}
- translated1 = {:age => {'$gt' => 12, '$lt' => 20}}
+ translated1 = {:age => {:$gt => 12, :$lt => 20}}
nested2 = {:type.nin => ['friend', 'enemy']}
- translated2 = {:type => {'$nin' => ['friend', 'enemy']}}
+ translated2 = {:type => {:$nin => ['friend', 'enemy']}}
nested3 = {'$and' => [nested2]}
translated3 = {:$and => [translated2]}
expected = [translated1, translated3]
View
6 spec/plucky/query_spec.rb
@@ -574,14 +574,14 @@
subject.
where(:moo => 'cow').
where(:foo.in => ['bar']).
- criteria.should == Plucky::CriteriaHash.new(:moo => 'cow', :foo => {'$in' => ['bar']})
+ criteria.should == Plucky::CriteriaHash.new(:moo => 'cow', :foo => {:$in => ['bar']})
end
it "normalizes merged criteria" do
subject.
where(:foo => 'bar').
where(:foo => 'baz').
- criteria.should == Plucky::CriteriaHash.new(:foo => {'$in' => %w[bar baz]})
+ criteria.should == Plucky::CriteriaHash.new(:foo => {:$in => %w[bar baz]})
end
it "returns new instance of query" do
@@ -712,7 +712,7 @@
query2 = described_class.new(@collection, :foo => 'baz', :fent => 'wick')
new_query = query1.merge(query2)
new_query.criteria[:fent].should == 'wick'
- new_query.criteria[:foo].should == {'$in' => %w[bar baz]}
+ new_query.criteria[:foo].should == {:$in => %w[bar baz]}
end
it "does not affect either of the merged queries" do

0 comments on commit 6d347f6

Please sign in to comment.
Something went wrong with that request. Please try again.