Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Range class support #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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