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 +1 -0
- data/gemfiles/rails-3.0.gemfile +1 -0
- data/gemfiles/rails-3.0.gemfile.lock +2 -0
- data/gemfiles/rails-3.1.gemfile +1 -0
- data/gemfiles/rails-3.1.gemfile.lock +2 -0
- data/gemfiles/rails-edge.gemfile +1 -0
- data/gemfiles/rails-edge.gemfile.lock +2 -0
- data/lib/mongomodel/support/types.rb +2 -0
- data/lib/mongomodel/support/types/date_time.rb +39 -0
- data/lib/mongomodel/support/types/time.rb +4 -0
- data/lib/mongomodel/version.rb +1 -1
- data/spec/mongomodel/attributes/store_spec.rb +27 -4
- data/spec/mongomodel/concerns/attribute_methods/multi_parameter_assignment_spec.rb +18 -3
- data/spec/mongomodel/concerns/attributes_spec.rb +1 -0
- data/spec/spec_helper.rb +1 -0
- metadata +16 -15
data/Gemfile
CHANGED
data/gemfiles/rails-3.0.gemfile
CHANGED
data/gemfiles/rails-3.1.gemfile
CHANGED
data/gemfiles/rails-edge.gemfile
CHANGED
@@ -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)
|
data/lib/mongomodel/version.rb
CHANGED
@@ -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.
|
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
|
-
|
145
|
-
|
146
|
-
|
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
|
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
|
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
|
|
data/spec/spec_helper.rb
CHANGED
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.
|
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-
|
12
|
+
date: 2011-09-26 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
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: *
|
24
|
+
version_requirements: *2156338320
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: activemodel
|
27
|
-
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: *
|
35
|
+
version_requirements: *2156336540
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: mongo
|
38
|
-
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: *
|
46
|
+
version_requirements: *2156334360
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: will_paginate
|
49
|
-
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: *
|
57
|
+
version_requirements: *2156331960
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: bundler
|
60
|
-
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: *
|
68
|
+
version_requirements: *2156329880
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rspec
|
71
|
-
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: *
|
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:
|
263
|
+
hash: 1437842966940713292
|
263
264
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
264
265
|
none: false
|
265
266
|
requirements:
|