Skip to content

Commit

Permalink
add Range class support
Browse files Browse the repository at this point in the history
  • Loading branch information
miyucy committed Mar 31, 2014
1 parent ba4e60e commit 2497035
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/plucky/normalizers/criteria_hash_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ def call(parent_key, key, value)
value
when Regexp
Regexp.new(value)
when Range
if value.exclude_end?
{ :$gte => value.first, :$lt => value.last }
else
{ :$gte => value.first, :$lte => value.last }
end
else
value
end
Expand Down
18 changes: 18 additions & 0 deletions spec/plucky/normalizers/criteria_hash_value_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,24 @@
end
end

context 'with a range' do
context 'with end' do
it 'convert to comapre operator' do
actual = 1...3
expected = { :$gte => 1, :$lt => 3 }
subject.call(:numbers, :numbers, actual).should eq(expected)
end
end

context 'exclusive end' do
it 'convert to comapre operator' do
actual = 1..3
expected = { :$gte => 1, :$lte => 3 }
subject.call(:numbers, :numbers, actual).should eq(expected)
end
end
end

context "with string object ids for string keys" do
let(:object_id) { BSON::ObjectId.new }

Expand Down
8 changes: 8 additions & 0 deletions spec/plucky/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,14 @@
subject.where(:name => /^c/i).all.should == [@chris]
end

it "works with range value" do
subject.where(:age => (28..29)).all.should =~ [@steve, @john]
end

it "works with exclusive end range value" do
subject.where(:age => (28...29)).all.should == [@john]
end

it "updates criteria" do
subject.
where(:moo => 'cow').
Expand Down

0 comments on commit 2497035

Please sign in to comment.