mongomodel 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -4,3 +4,4 @@ gemspec
4
4
 
5
5
  gem "appraisal", "~> 0.3.6"
6
6
  gem "bson_ext", "~> 1.4"
7
+ gem "tzinfo"
@@ -4,6 +4,7 @@ source "http://rubygems.org"
4
4
 
5
5
  gem "appraisal", "~> 0.3.6"
6
6
  gem "bson_ext", "~> 1.4"
7
+ gem "tzinfo"
7
8
  gem "activesupport", "3.0.10"
8
9
  gem "activemodel", "3.0.10"
9
10
 
@@ -58,6 +58,7 @@ GEM
58
58
  diff-lcs (~> 1.1.2)
59
59
  rspec-mocks (2.6.0)
60
60
  term-ansicolor (1.0.5)
61
+ tzinfo (0.3.29)
61
62
  will_paginate (2.3.16)
62
63
 
63
64
  PLATFORMS
@@ -71,3 +72,4 @@ DEPENDENCIES
71
72
  bundler (>= 1.0.0)
72
73
  mongomodel!
73
74
  rspec (~> 2.6.0)
75
+ tzinfo
@@ -4,6 +4,7 @@ source "http://rubygems.org"
4
4
 
5
5
  gem "appraisal", "~> 0.3.6"
6
6
  gem "bson_ext", "~> 1.4"
7
+ gem "tzinfo"
7
8
  gem "activesupport", "3.1.0"
8
9
  gem "activemodel", "3.1.0"
9
10
 
@@ -38,6 +38,7 @@ GEM
38
38
  rspec-expectations (2.6.0)
39
39
  diff-lcs (~> 1.1.2)
40
40
  rspec-mocks (2.6.0)
41
+ tzinfo (0.3.29)
41
42
  will_paginate (2.3.16)
42
43
 
43
44
  PLATFORMS
@@ -51,3 +52,4 @@ DEPENDENCIES
51
52
  bundler (>= 1.0.0)
52
53
  mongomodel!
53
54
  rspec (~> 2.6.0)
55
+ tzinfo
@@ -4,6 +4,7 @@ source "http://rubygems.org"
4
4
 
5
5
  gem "appraisal", "~> 0.3.6"
6
6
  gem "bson_ext", "~> 1.4"
7
+ gem "tzinfo"
7
8
  gem "activesupport", :git=>"https://github.com/rails/rails.git"
8
9
  gem "activemodel", :git=>"https://github.com/rails/rails.git"
9
10
 
@@ -44,6 +44,7 @@ GEM
44
44
  rspec-expectations (2.6.0)
45
45
  diff-lcs (~> 1.1.2)
46
46
  rspec-mocks (2.6.0)
47
+ tzinfo (0.3.29)
47
48
  will_paginate (2.3.16)
48
49
 
49
50
  PLATFORMS
@@ -57,3 +58,4 @@ DEPENDENCIES
57
58
  bundler (>= 1.0.0)
58
59
  mongomodel!
59
60
  rspec (~> 2.6.0)
61
+ tzinfo
@@ -9,6 +9,7 @@ require 'mongomodel/support/types/boolean'
9
9
  require 'mongomodel/support/types/symbol'
10
10
  require 'mongomodel/support/types/date'
11
11
  require 'mongomodel/support/types/time'
12
+ require 'mongomodel/support/types/date_time'
12
13
  require 'mongomodel/support/types/custom'
13
14
  require 'mongomodel/support/types/array'
14
15
  require 'mongomodel/support/types/set'
@@ -25,6 +26,7 @@ module MongoModel
25
26
  ::Symbol => Types::Symbol.new,
26
27
  ::Date => Types::Date.new,
27
28
  ::Time => Types::Time.new,
29
+ ::DateTime => Types::DateTime.new,
28
30
  ::Array => Types::Array.new,
29
31
  ::Set => Types::Set.new,
30
32
  ::Hash => Types::Hash.new,
@@ -0,0 +1,39 @@
1
+ require 'active_support/core_ext/date_time/conversions'
2
+ require 'active_support/core_ext/string/conversions'
3
+
4
+ module MongoModel
5
+ module Types
6
+ class DateTime < Object
7
+ def cast(value)
8
+ case value
9
+ when ::Array
10
+ ::DateTime.civil(*value)
11
+ when ::Hash
12
+ cast("#{value[:date]} #{value[:time]}")
13
+ when ::String
14
+ cast(::DateTime.parse(value))
15
+ else
16
+ value.to_datetime.change(:usec => 0)
17
+ end
18
+ rescue
19
+ nil
20
+ end
21
+
22
+ def to_mongo(value)
23
+ to_time(value.utc) if value
24
+ end
25
+
26
+ def from_mongo(value)
27
+ time = value.respond_to?(:in_time_zone) ? value.in_time_zone : value
28
+ time.to_datetime
29
+ end
30
+
31
+ private
32
+ # Define our own to_time method as DateTime.to_time in ActiveSupport may return
33
+ # the DateTime object unchanged, whereas BSON expects an actual Time object.
34
+ def to_time(dt)
35
+ ::Time.utc_time(dt.year, dt.month, dt.day, dt.hour, dt.min, dt.sec)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -9,6 +9,10 @@ module MongoModel
9
9
  when ::Array
10
10
  base = ::Time.zone ? ::Time.zone : ::Time
11
11
  base.local(*value)
12
+ when ::Hash
13
+ cast("#{value[:date]} #{value[:time]}")
14
+ when ::String
15
+ cast(::Time.parse(value))
12
16
  else
13
17
  time = value.to_time
14
18
  time.change(:usec => (time.usec / 1000.0).floor * 1000)
@@ -1,3 +1,3 @@
1
1
  module MongoModel
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  end
@@ -14,6 +14,7 @@ module MongoModel
14
14
  properties[:array] = MongoModel::Properties::Property.new(:array, Array)
15
15
  properties[:date] = MongoModel::Properties::Property.new(:date, Date)
16
16
  properties[:time] = MongoModel::Properties::Property.new(:time, Time)
17
+ properties[:datetime] = MongoModel::Properties::Property.new(:datetime, DateTime)
17
18
  properties[:rational] = MongoModel::Properties::Property.new(:rational, Rational)
18
19
  properties[:custom] = MongoModel::Properties::Property.new(:custom, CustomClass)
19
20
  properties[:custom_default] = MongoModel::Properties::Property.new(:custom_default, CustomClassWithDefault)
@@ -121,6 +122,7 @@ module MongoModel
121
122
  Time.local(2008, 12, 3, 0, 0, 0, 0) => Date.civil(2008, 12, 3),
122
123
  "2009/3/4" => Date.civil(2009, 3, 4),
123
124
  "Sat Jan 01 20:15:01 UTC 2000" => Date.civil(2000, 1, 1),
125
+ "2004-05-12" => Date.civil(2004, 5, 12),
124
126
  nil => nil
125
127
  },
126
128
  :time =>
@@ -128,7 +130,21 @@ module MongoModel
128
130
  Time.local(2008, 5, 14, 1, 2, 3, 123456) => Time.local(2008, 5, 14, 1, 2, 3, 123000),
129
131
  Date.civil(2009, 11, 15) => Time.local(2009, 11, 15, 0, 0, 0, 0),
130
132
  "Sat Jan 01 20:15:01.123456 UTC 2000" => Time.utc(2000, 1, 1, 20, 15, 1, 123000),
131
- "2009/3/4" => Time.utc(2009, 3, 4, 0, 0, 0, 0),
133
+ "2009/3/4" => Time.local(2009, 3, 4, 0, 0, 0, 0),
134
+ "09:34" => lambda { |t| t.hour == 9 && t.min == 34 },
135
+ "5:21pm" => lambda { |t| t.hour == 17 && t.min == 21 },
136
+ { :date => "2005-11-04", :time => "4:55pm" } => Time.local(2005, 11, 4, 16, 55, 0),
137
+ nil => nil
138
+ },
139
+ :datetime =>
140
+ {
141
+ Time.local(2008, 5, 14, 1, 2, 3, 123456) => Time.local(2008, 5, 14, 1, 2, 3, 0).to_datetime,
142
+ Date.civil(2009, 11, 15) => DateTime.civil(2009, 11, 15, 0, 0, 0, 0),
143
+ "Sat Jan 01 20:15:01.123456 UTC 2000" => DateTime.civil(2000, 1, 1, 20, 15, 1, 0),
144
+ "2009/3/4" => DateTime.civil(2009, 3, 4, 0, 0, 0, 0),
145
+ "09:34" => lambda { |t| t.hour == 9 && t.min == 34 },
146
+ "5:21pm" => lambda { |t| t.hour == 17 && t.min == 21 },
147
+ { :date => "2005-11-04", :time => "4:55pm" } => DateTime.civil(2005, 11, 4, 16, 55, 0),
132
148
  nil => nil
133
149
  },
134
150
  :rational =>
@@ -141,9 +157,16 @@ module MongoModel
141
157
  TypeCastExamples.each do |type, examples|
142
158
  context "assigning to #{type} property" do
143
159
  examples.each do |assigned, expected|
144
- it "should cast #{assigned.inspect} to #{expected.inspect}" do
145
- subject[type] = assigned
146
- subject[type].should == expected
160
+ if expected.is_a?(Proc)
161
+ it "should cast #{assigned.inspect} to match proc" do
162
+ subject[type] = assigned
163
+ expected.call(subject[type]).should be_true
164
+ end
165
+ else
166
+ it "should cast #{assigned.inspect} to #{expected.inspect}" do
167
+ subject[type] = assigned
168
+ subject[type].should == expected
169
+ end
147
170
  end
148
171
  end
149
172
  end
@@ -5,12 +5,13 @@ module MongoModel
5
5
  define_class(:TestDocument, described_class) do
6
6
  property :timestamp, Time
7
7
  property :datestamp, Date
8
+ property :datetime, DateTime
8
9
  end
9
10
 
10
11
  subject { TestDocument.new }
11
12
 
12
- describe "multiparameter assignment" do
13
- context "setting a timestamp" do
13
+ describe "multiparameter assignment from select" do
14
+ context "setting a Time" do
14
15
  it "should combine and assign parameters as Time" do
15
16
  subject.attributes = {
16
17
  "timestamp(1i)" => "2009",
@@ -24,7 +25,7 @@ module MongoModel
24
25
  end
25
26
  end
26
27
 
27
- context "setting a datestamp" do
28
+ context "setting a Date" do
28
29
  it "should combine and assign parameters as Date" do
29
30
  subject.attributes = {
30
31
  "datestamp(1i)" => "2008",
@@ -35,6 +36,20 @@ module MongoModel
35
36
  subject.datestamp.should == Date.new(2008, 4, 9)
36
37
  end
37
38
  end
39
+
40
+ context "setting a DateTime" do
41
+ it "should combine and assign parameters as DateTime" do
42
+ subject.attributes = {
43
+ "datetime(1i)" => "2009",
44
+ "datetime(2i)" => "10",
45
+ "datetime(3i)" => "5",
46
+ "datetime(4i)" => "14",
47
+ "datetime(5i)" => "35"
48
+ }
49
+
50
+ subject.datetime.should == DateTime.civil(2009, 10, 5, 14, 35)
51
+ end
52
+ end
38
53
  end
39
54
  end
40
55
  end
@@ -12,6 +12,7 @@ module MongoModel
12
12
  Hash => { :rabbit => 'hat', 'hello' => 12345 }.with_indifferent_access,
13
13
  Date => Date.today,
14
14
  Time => Types::Time.new.cast(Time.now),
15
+ DateTime => Types::DateTime.new.cast(DateTime.now),
15
16
  CustomClass => CustomClass.new('hello')
16
17
  }
17
18
 
@@ -18,5 +18,6 @@ RSpec.configure do |config|
18
18
 
19
19
  config.before(:each) do
20
20
  MongoModel.database.collections.each { |c| c.drop rescue c.remove }
21
+ Time.zone = "Australia/Adelaide"
21
22
  end
22
23
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongomodel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-22 00:00:00.000000000Z
12
+ date: 2011-09-26 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &2154652940 !ruby/object:Gem::Requirement
16
+ requirement: &2156338320 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2154652940
24
+ version_requirements: *2156338320
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activemodel
27
- requirement: &2154652440 !ruby/object:Gem::Requirement
27
+ requirement: &2156336540 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '3.0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2154652440
35
+ version_requirements: *2156336540
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: mongo
38
- requirement: &2154641960 !ruby/object:Gem::Requirement
38
+ requirement: &2156334360 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '1.4'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *2154641960
46
+ version_requirements: *2156334360
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: will_paginate
49
- requirement: &2154641460 !ruby/object:Gem::Requirement
49
+ requirement: &2156331960 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 2.3.15
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *2154641460
57
+ version_requirements: *2156331960
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: bundler
60
- requirement: &2154640860 !ruby/object:Gem::Requirement
60
+ requirement: &2156329880 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 1.0.0
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *2154640860
68
+ version_requirements: *2156329880
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rspec
71
- requirement: &2154640200 !ruby/object:Gem::Requirement
71
+ requirement: &2156294220 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ~>
@@ -76,7 +76,7 @@ dependencies:
76
76
  version: 2.6.0
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *2154640200
79
+ version_requirements: *2156294220
80
80
  description: MongoModel is a MongoDB ORM for Ruby/Rails similar to ActiveRecord and
81
81
  DataMapper.
82
82
  email:
@@ -175,6 +175,7 @@ files:
175
175
  - lib/mongomodel/support/types/boolean.rb
176
176
  - lib/mongomodel/support/types/custom.rb
177
177
  - lib/mongomodel/support/types/date.rb
178
+ - lib/mongomodel/support/types/date_time.rb
178
179
  - lib/mongomodel/support/types/float.rb
179
180
  - lib/mongomodel/support/types/hash.rb
180
181
  - lib/mongomodel/support/types/integer.rb
@@ -259,7 +260,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
259
260
  version: '0'
260
261
  segments:
261
262
  - 0
262
- hash: 4407793988902722039
263
+ hash: 1437842966940713292
263
264
  required_rubygems_version: !ruby/object:Gem::Requirement
264
265
  none: false
265
266
  requirements: