simple_date_fix 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +20 -2
- data/lib/simple_date_fix/item_collection.rb +17 -0
- data/lib/simple_date_fix/version.rb +1 -1
- data/spec/simple_date_fix/queries_spec.rb +25 -0
- data/spec/spec_helper.rb +2 -0
- metadata +1 -1
data/README.md
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
# SimpleDateFix
|
2
2
|
|
3
|
-
|
3
|
+
Fixes for date queries using AWS::Record::Model. This gem is based on
|
4
|
+
work done by [daniel-nelson](https://github.com/daniel-nelson) in [this
|
5
|
+
pull request to the 'aws-sdk'
|
6
|
+
gem.](https://github.com/aws/aws-sdk-ruby/pull/22)
|
7
|
+
Thanks, Daniel!
|
4
8
|
|
5
9
|
## Installation
|
6
10
|
|
@@ -18,7 +22,21 @@ Or install it yourself as:
|
|
18
22
|
|
19
23
|
## Usage
|
20
24
|
|
21
|
-
|
25
|
+
Now you can query your `datetime_attr`s.
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
class Event < AWS::Record::Model
|
29
|
+
datetime_attr :timestamp
|
30
|
+
end
|
31
|
+
|
32
|
+
e = Event.create!(:timestamp => Time.now - 6.hours)
|
33
|
+
|
34
|
+
Event.where("timestamp > ?",Time.now - 7.hours).first
|
35
|
+
# => e
|
36
|
+
|
37
|
+
Event.where("timestamp > ?",Time.now - 5.hours).first
|
38
|
+
# => nil
|
39
|
+
```
|
22
40
|
|
23
41
|
## Contributing
|
24
42
|
|
@@ -17,6 +17,23 @@ module AWS
|
|
17
17
|
str
|
18
18
|
end
|
19
19
|
|
20
|
+
# We modify this method to serialize DateTime attrs the same way that they
|
21
|
+
# are stored. This allows queries to actually work.
|
22
|
+
# @private
|
23
|
+
protected
|
24
|
+
def coerce_substitution(subst)
|
25
|
+
if subst.kind_of?(Array)
|
26
|
+
"(" +
|
27
|
+
subst.flatten.map { |s| coerce_substitution(s) }.join(", ") + ")"
|
28
|
+
elsif subst.is_a?(DateTime)
|
29
|
+
"'" + AWS::Record::Attributes::DateTimeAttr.serialize(subst) + "'"
|
30
|
+
elsif subst.is_a?(Time)
|
31
|
+
coerce_substitution(subst.to_datetime)
|
32
|
+
else
|
33
|
+
"'" + subst.to_s.gsub("'", "''") + "'"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
20
37
|
end
|
21
38
|
end
|
22
39
|
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
+
|
3
4
|
describe 'model validations' do
|
4
5
|
|
5
6
|
let(:klass) { Class.new(AWS::Record::Base) }
|
@@ -21,4 +22,28 @@ describe 'model validations' do
|
|
21
22
|
end
|
22
23
|
end
|
23
24
|
|
25
|
+
describe 'queyring the timestamp' do
|
26
|
+
before(:each) do
|
27
|
+
@obj = klass.new :timestamp => (Time.now - 3.hours).utc
|
28
|
+
@obj.save
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should return the object when we query for timestamps newer than 4 hours ago" do
|
32
|
+
AWS::SimpleDB.consistent_reads do
|
33
|
+
test_stamp = (Time.now - 4.hours).utc
|
34
|
+
obj = klass.where("timestamp > ?",test_stamp).first
|
35
|
+
obj.id.should == @obj.id
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should not return the object when we query for timestamps newer than 2 hours ago" do
|
40
|
+
AWS::SimpleDB.consistent_reads do
|
41
|
+
test_stamp = (Time.now - 2.hours).utc
|
42
|
+
obj = klass.where("timestamp > ?",test_stamp).first
|
43
|
+
obj.should be_nil
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
24
49
|
end
|
data/spec/spec_helper.rb
CHANGED