Skip to content

Loading…

add Range class support #40

Open
wants to merge 1 commit into from

1 participant

@miyucy

Sometime, I want to write as like below:

Foo.where(created_at: Time.zone.now.all_day)

But, it can not. so I want to add range class support to plucky.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 31, 2014
  1. @miyucy

    add Range class support

    miyucy committed
View
6 lib/plucky/normalizers/criteria_hash_value.rb
@@ -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
View
18 spec/plucky/normalizers/criteria_hash_value_spec.rb
@@ -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 }
View
8 spec/plucky/query_spec.rb
@@ -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').
Something went wrong with that request. Please try again.