mongo_doc 0.6.3 → 0.6.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +1 -1
- data/VERSION +1 -1
- data/features/step_definitions/document_steps.rb +6 -0
- data/features/step_definitions/documents.rb +8 -0
- data/features/step_definitions/field_steps.rb +4 -0
- data/features/timestamps.feature +14 -0
- data/lib/mongo_doc/document.rb +2 -1
- data/lib/mongo_doc/ext/numeric.rb +2 -2
- data/lib/mongo_doc/timestamps.rb +54 -0
- data/lib/mongo_doc.rb +1 -1
- data/mongo_doc.gemspec +7 -2
- data/spec/ext_spec.rb +5 -5
- data/spec/timestamps_spec.rb +154 -0
- metadata +9 -4
data/README.textile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.6.
|
1
|
+
0.6.4
|
@@ -123,6 +123,12 @@ Then /^the document '(.+)' does not roundtrip$/ do |name|
|
|
123
123
|
from_db.should_not == object
|
124
124
|
end
|
125
125
|
|
126
|
+
Then /^the document '(\w+)' is reloaded$/ do |name|
|
127
|
+
object = instance_variable_get("@#{name}")
|
128
|
+
from_db = object.class.find_one(object._id)
|
129
|
+
instance_variable_set("@#{name}", from_db)
|
130
|
+
end
|
131
|
+
|
126
132
|
Then /^the last return value is (.+)$/ do |bool_val|
|
127
133
|
@last_return.should send("be_#{bool_val}")
|
128
134
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
Feature: Timestamps
|
2
|
+
|
3
|
+
Background:
|
4
|
+
Given an empty Person document collection
|
5
|
+
And a Person document named 'Fry' :
|
6
|
+
| Name |
|
7
|
+
| Philip J. Fry |
|
8
|
+
|
9
|
+
Scenario: Creation time
|
10
|
+
When I save the document 'Fry'
|
11
|
+
And the document 'Fry' is reloaded
|
12
|
+
Then the field created_at of the document 'Fry' is not nil
|
13
|
+
And the field updated_at of the document 'Fry' is not nil
|
14
|
+
|
data/lib/mongo_doc/document.rb
CHANGED
@@ -6,6 +6,7 @@ require 'mongo_doc/criteria'
|
|
6
6
|
require 'mongo_doc/finders'
|
7
7
|
require 'mongo_doc/index'
|
8
8
|
require 'mongo_doc/scope'
|
9
|
+
require 'mongo_doc/timestamps'
|
9
10
|
require 'active_model/naming'
|
10
11
|
require 'active_model/translation'
|
11
12
|
require 'active_model/deprecated_error_methods'
|
@@ -31,6 +32,7 @@ module MongoDoc
|
|
31
32
|
extend Finders
|
32
33
|
extend Index
|
33
34
|
extend Scope
|
35
|
+
extend Timestamps
|
34
36
|
include ::ActiveModel::Validations
|
35
37
|
extend ::ActiveModel::Naming
|
36
38
|
extend Validations
|
@@ -103,7 +105,6 @@ module MongoDoc
|
|
103
105
|
end
|
104
106
|
|
105
107
|
def update_attributes!(attrs)
|
106
|
-
strict = attrs.delete(:__strict__)
|
107
108
|
self.attributes = attrs
|
108
109
|
return save! if new_record?
|
109
110
|
raise DocumentInvalidError unless valid?
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module MongoDoc
|
2
|
+
module Timestamps
|
3
|
+
|
4
|
+
# Create automatic timestamps on a +root+ Document. Timestamps are not
|
5
|
+
# implemented for embedded documents.
|
6
|
+
#
|
7
|
+
# Two timestamps fields are created: +created_at+, +updated_at+
|
8
|
+
#
|
9
|
+
# +created_at+:: set on initial save only
|
10
|
+
# +updated_at+:: set on every save
|
11
|
+
def timestamps!
|
12
|
+
[:created_at, :updated_at].each do |name|
|
13
|
+
_keys << name unless _keys.include?(name)
|
14
|
+
attr_reader name
|
15
|
+
module_eval(<<-RUBY, __FILE__, __LINE__)
|
16
|
+
def #{name}=(value) # def created_at=(value)
|
17
|
+
if value.kind_of?(String) # if value.kind_of?(String)
|
18
|
+
value = Time.cast_from_string(value) # value = Time.cast_from_string(value)
|
19
|
+
end # end
|
20
|
+
@#{name} = value.nil? ? nil : value.utc # @created_at = value.nil? ? nil : value.utc
|
21
|
+
end # end
|
22
|
+
RUBY
|
23
|
+
end
|
24
|
+
|
25
|
+
module_eval do
|
26
|
+
def _save(safe)
|
27
|
+
if new_record?
|
28
|
+
self.created_at = self.updated_at = Time.now
|
29
|
+
else
|
30
|
+
original_updated_at = updated_at
|
31
|
+
self.updated_at = Time.now
|
32
|
+
end
|
33
|
+
super
|
34
|
+
rescue Mongo::MongoDBError => e
|
35
|
+
if new_record?
|
36
|
+
self.created_at = self.updated_at = nil
|
37
|
+
else
|
38
|
+
self.updated_at = original_updated_at
|
39
|
+
end
|
40
|
+
raise e
|
41
|
+
end
|
42
|
+
|
43
|
+
def _update(selector, data, safe)
|
44
|
+
original_updated_at = updated_at
|
45
|
+
self.updated_at = Time.now
|
46
|
+
super
|
47
|
+
rescue Mongo::MongoDBError => e
|
48
|
+
self.updated_at = original_updated_at
|
49
|
+
raise e
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
data/lib/mongo_doc.rb
CHANGED
data/mongo_doc.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongo_doc}
|
8
|
-
s.version = "0.6.
|
8
|
+
s.version = "0.6.4"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Les Hill"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-07-07}
|
13
13
|
s.description = %q{ODM for MongoDB}
|
14
14
|
s.email = %q{leshill@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -47,6 +47,7 @@ Gem::Specification.new do |s|
|
|
47
47
|
"features/step_definitions/document_steps.rb",
|
48
48
|
"features/step_definitions/documents.rb",
|
49
49
|
"features/step_definitions/embed_hash_steps.rb",
|
50
|
+
"features/step_definitions/field_steps.rb",
|
50
51
|
"features/step_definitions/finder_steps.rb",
|
51
52
|
"features/step_definitions/index_steps.rb",
|
52
53
|
"features/step_definitions/json_steps.rb",
|
@@ -60,6 +61,7 @@ Gem::Specification.new do |s|
|
|
60
61
|
"features/step_definitions/util_steps.rb",
|
61
62
|
"features/string_casting.feature",
|
62
63
|
"features/support/support.rb",
|
64
|
+
"features/timestamps.feature",
|
63
65
|
"features/using_criteria.feature",
|
64
66
|
"lib/mongo_doc.rb",
|
65
67
|
"lib/mongo_doc/associations.rb",
|
@@ -102,6 +104,7 @@ Gem::Specification.new do |s|
|
|
102
104
|
"lib/mongo_doc/railties/config.rb",
|
103
105
|
"lib/mongo_doc/root.rb",
|
104
106
|
"lib/mongo_doc/scope.rb",
|
107
|
+
"lib/mongo_doc/timestamps.rb",
|
105
108
|
"lib/mongo_doc/validations.rb",
|
106
109
|
"lib/mongo_doc/validations/validates_embedded.rb",
|
107
110
|
"lib/mongoid/contexts/enumerable.rb",
|
@@ -162,6 +165,7 @@ Gem::Specification.new do |s|
|
|
162
165
|
"spec/root_spec.rb",
|
163
166
|
"spec/scope_spec.rb",
|
164
167
|
"spec/spec_helper.rb",
|
168
|
+
"spec/timestamps_spec.rb",
|
165
169
|
"spec/validations_spec.rb"
|
166
170
|
]
|
167
171
|
s.homepage = %q{http://github.com/leshill/mongodoc}
|
@@ -200,6 +204,7 @@ Gem::Specification.new do |s|
|
|
200
204
|
"spec/root_spec.rb",
|
201
205
|
"spec/scope_spec.rb",
|
202
206
|
"spec/spec_helper.rb",
|
207
|
+
"spec/timestamps_spec.rb",
|
203
208
|
"spec/validations_spec.rb",
|
204
209
|
"examples/simple_document.rb",
|
205
210
|
"examples/simple_object.rb"
|
data/spec/ext_spec.rb
CHANGED
@@ -46,15 +46,15 @@ describe "Ruby Object Extensions" do
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
|
-
context "
|
50
|
-
context "
|
49
|
+
context "Numbers" do
|
50
|
+
context "Float" do
|
51
51
|
it "returns nil for a blank string" do
|
52
|
-
|
52
|
+
Float.cast_from_string('').should be_nil
|
53
53
|
end
|
54
54
|
|
55
55
|
it "converts from a string to a BigDecimal" do
|
56
|
-
|
57
|
-
|
56
|
+
float = "12345.6789".to_f
|
57
|
+
Float.cast_from_string(float.to_s).should == float
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
@@ -0,0 +1,154 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MongoDoc::Timestamps do
|
4
|
+
class TimestampsTest
|
5
|
+
include MongoDoc::Document
|
6
|
+
|
7
|
+
attr_accessor :name
|
8
|
+
|
9
|
+
timestamps!
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:collection) { stub(:save => ::BSON::ObjectID.new, :update => true) }
|
13
|
+
let(:document) { TimestampsTest.new }
|
14
|
+
|
15
|
+
before do
|
16
|
+
TimestampsTest.stub(:collection).and_return(collection)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "has a created_at attribute" do
|
20
|
+
document.should respond_to(:created_at)
|
21
|
+
document.should respond_to(:created_at=)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "has an updated_at attribute" do
|
25
|
+
document.should respond_to(:updated_at)
|
26
|
+
document.should respond_to(:updated_at=)
|
27
|
+
end
|
28
|
+
|
29
|
+
context "on initial save" do
|
30
|
+
context "that suceeeds" do
|
31
|
+
it "sets the created_at timestamp" do
|
32
|
+
document.save
|
33
|
+
document.created_at.should_not be_nil
|
34
|
+
end
|
35
|
+
|
36
|
+
it "sets the updated_at timestamp" do
|
37
|
+
document.save
|
38
|
+
document.updated_at.should_not be_nil
|
39
|
+
end
|
40
|
+
|
41
|
+
it "the created_at and updated_at timestamps are the same" do
|
42
|
+
document.save
|
43
|
+
document.created_at.should == document.updated_at
|
44
|
+
end
|
45
|
+
|
46
|
+
it "sets the timezone to UTC" do
|
47
|
+
document.save
|
48
|
+
document.created_at.zone.should == "UTC"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context "that fails" do
|
53
|
+
before do
|
54
|
+
collection.stub(:save).and_raise(Mongo::MongoDBError.new)
|
55
|
+
end
|
56
|
+
|
57
|
+
it "does not set the created_at timestamp" do
|
58
|
+
document.save rescue nil
|
59
|
+
document.created_at.should be_nil
|
60
|
+
end
|
61
|
+
|
62
|
+
it "does not set the updated_at timestamp" do
|
63
|
+
document.save rescue nil
|
64
|
+
document.updated_at.should be_nil
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context "on subsequent save" do
|
70
|
+
before do
|
71
|
+
document.save
|
72
|
+
end
|
73
|
+
|
74
|
+
context "that suceeeds" do
|
75
|
+
let!(:original_created_at) { document.created_at }
|
76
|
+
|
77
|
+
it "leaves the created_at timestamp unchanged" do
|
78
|
+
document.save
|
79
|
+
document.created_at.should == original_created_at
|
80
|
+
end
|
81
|
+
|
82
|
+
it "sets the updated_at timestamp" do
|
83
|
+
document.save
|
84
|
+
document.updated_at.should_not == original_created_at
|
85
|
+
end
|
86
|
+
|
87
|
+
it "the created_at and updated_at timestamps are not the same" do
|
88
|
+
document.save
|
89
|
+
document.created_at.should_not == document.updated_at
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
context "that fails" do
|
94
|
+
let!(:original_created_at) { document.created_at }
|
95
|
+
|
96
|
+
before do
|
97
|
+
collection.stub(:save).and_raise(Mongo::MongoDBError.new)
|
98
|
+
end
|
99
|
+
|
100
|
+
it "leaves the created_at timestamp unchanged" do
|
101
|
+
document.save rescue nil
|
102
|
+
document.created_at.should == original_created_at
|
103
|
+
end
|
104
|
+
|
105
|
+
it "leaves the updated_at timestamp unchanged" do
|
106
|
+
document.save rescue nil
|
107
|
+
document.updated_at.should == original_created_at
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
context "on update attributes" do
|
113
|
+
before do
|
114
|
+
document.save
|
115
|
+
end
|
116
|
+
|
117
|
+
context "that suceeeds" do
|
118
|
+
let!(:original_created_at) { document.created_at }
|
119
|
+
|
120
|
+
it "leaves the created_at timestamp unchanged" do
|
121
|
+
document.update_attributes(:name => 'name')
|
122
|
+
document.created_at.should == original_created_at
|
123
|
+
end
|
124
|
+
|
125
|
+
it "sets the updated_at timestamp" do
|
126
|
+
document.update_attributes(:name => 'name')
|
127
|
+
document.updated_at.should_not == original_created_at
|
128
|
+
end
|
129
|
+
|
130
|
+
it "the created_at and updated_at timestamps are not the same" do
|
131
|
+
document.update_attributes(:name => 'name')
|
132
|
+
document.created_at.should_not == document.updated_at
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
context "that fails" do
|
137
|
+
let!(:original_created_at) { document.created_at }
|
138
|
+
|
139
|
+
before do
|
140
|
+
collection.stub(:update).and_raise(Mongo::MongoDBError.new)
|
141
|
+
end
|
142
|
+
|
143
|
+
it "leaves the created_at timestamp unchanged" do
|
144
|
+
document.update_attributes(:name => 'name') rescue nil
|
145
|
+
document.created_at.should == original_created_at
|
146
|
+
end
|
147
|
+
|
148
|
+
it "leaves the updated_at timestamp unchanged" do
|
149
|
+
document.update_attributes(:name => 'name') rescue nil
|
150
|
+
document.updated_at.should == original_created_at
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo_doc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 15
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 6
|
9
|
-
-
|
10
|
-
version: 0.6.
|
9
|
+
- 4
|
10
|
+
version: 0.6.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Les Hill
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-07-07 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -176,6 +176,7 @@ files:
|
|
176
176
|
- features/step_definitions/document_steps.rb
|
177
177
|
- features/step_definitions/documents.rb
|
178
178
|
- features/step_definitions/embed_hash_steps.rb
|
179
|
+
- features/step_definitions/field_steps.rb
|
179
180
|
- features/step_definitions/finder_steps.rb
|
180
181
|
- features/step_definitions/index_steps.rb
|
181
182
|
- features/step_definitions/json_steps.rb
|
@@ -189,6 +190,7 @@ files:
|
|
189
190
|
- features/step_definitions/util_steps.rb
|
190
191
|
- features/string_casting.feature
|
191
192
|
- features/support/support.rb
|
193
|
+
- features/timestamps.feature
|
192
194
|
- features/using_criteria.feature
|
193
195
|
- lib/mongo_doc.rb
|
194
196
|
- lib/mongo_doc/associations.rb
|
@@ -231,6 +233,7 @@ files:
|
|
231
233
|
- lib/mongo_doc/railties/config.rb
|
232
234
|
- lib/mongo_doc/root.rb
|
233
235
|
- lib/mongo_doc/scope.rb
|
236
|
+
- lib/mongo_doc/timestamps.rb
|
234
237
|
- lib/mongo_doc/validations.rb
|
235
238
|
- lib/mongo_doc/validations/validates_embedded.rb
|
236
239
|
- lib/mongoid/contexts/enumerable.rb
|
@@ -291,6 +294,7 @@ files:
|
|
291
294
|
- spec/root_spec.rb
|
292
295
|
- spec/scope_spec.rb
|
293
296
|
- spec/spec_helper.rb
|
297
|
+
- spec/timestamps_spec.rb
|
294
298
|
- spec/validations_spec.rb
|
295
299
|
has_rdoc: true
|
296
300
|
homepage: http://github.com/leshill/mongodoc
|
@@ -357,6 +361,7 @@ test_files:
|
|
357
361
|
- spec/root_spec.rb
|
358
362
|
- spec/scope_spec.rb
|
359
363
|
- spec/spec_helper.rb
|
364
|
+
- spec/timestamps_spec.rb
|
360
365
|
- spec/validations_spec.rb
|
361
366
|
- examples/simple_document.rb
|
362
367
|
- examples/simple_object.rb
|