simple_date_fix 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,6 +1,10 @@
1
1
  # SimpleDateFix
2
2
 
3
- TODO: Write a gem description
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
- TODO: Write usage instructions here
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,3 +1,3 @@
1
1
  module SimpleDateFix
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  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
@@ -7,6 +7,8 @@
7
7
  #
8
8
  require 'aws-sdk'
9
9
  require 'support/aws_init'
10
+ require 'active_support/core_ext/numeric/time'
11
+ require 'active_support/core_ext/date_time/calculations'
10
12
 
11
13
  require "#{File.dirname(__FILE__)}/../lib/simple_date_fix"
12
14
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_date_fix
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: