mongo_doc 0.6.26 → 0.6.27
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.textile +14 -203
- data/Rakefile +0 -134
- data/lib/mongo_doc/collection.rb +4 -0
- data/lib/mongo_doc/finders.rb +1 -0
- data/lib/mongo_doc/timestamps.rb +4 -3
- data/lib/mongo_doc/version.rb +3 -0
- data/lib/mongo_doc.rb +0 -5
- metadata +25 -212
- data/.document +0 -5
- data/.gitignore +0 -8
- data/.rspec +0 -2
- data/.rvmrc +0 -1
- data/.watchr +0 -32
- data/Gemfile +0 -3
- data/Gemfile.lock +0 -72
- data/HISTORY.md +0 -11
- data/VERSION +0 -1
- data/examples/simple_document.rb +0 -46
- data/examples/simple_object.rb +0 -34
- data/features/collections.feature +0 -9
- data/features/connection.feature +0 -13
- data/features/db_references.feature +0 -21
- data/features/embed_hash.feature +0 -16
- data/features/finders.feature +0 -76
- data/features/indexes.feature +0 -28
- data/features/mongodb.yml +0 -7
- data/features/mongodoc_base.feature +0 -128
- data/features/new_record.feature +0 -36
- data/features/partial_updates.feature +0 -95
- data/features/polymorphic_collections.feature +0 -16
- data/features/references.feature +0 -28
- data/features/removing_documents.feature +0 -68
- data/features/saving_an_object.feature +0 -15
- data/features/scopes.feature +0 -66
- data/features/step_definitions/collection_steps.rb +0 -17
- data/features/step_definitions/connection_steps.rb +0 -13
- data/features/step_definitions/document_steps.rb +0 -155
- data/features/step_definitions/documents.rb +0 -61
- data/features/step_definitions/embed_hash_steps.rb +0 -6
- data/features/step_definitions/field_steps.rb +0 -28
- data/features/step_definitions/finder_steps.rb +0 -15
- data/features/step_definitions/index_steps.rb +0 -10
- data/features/step_definitions/json_steps.rb +0 -9
- data/features/step_definitions/object_steps.rb +0 -50
- data/features/step_definitions/objects.rb +0 -24
- data/features/step_definitions/partial_update_steps.rb +0 -31
- data/features/step_definitions/query_steps.rb +0 -69
- data/features/step_definitions/removing_documents_steps.rb +0 -14
- data/features/step_definitions/scope_steps.rb +0 -18
- data/features/step_definitions/string_casting_steps.rb +0 -29
- data/features/step_definitions/util_steps.rb +0 -7
- data/features/string_casting.feature +0 -10
- data/features/support/support.rb +0 -12
- data/features/timestamps.feature +0 -14
- data/features/using_criteria.feature +0 -148
- data/mongo_doc.gemspec +0 -272
- data/mongodb.example.yml +0 -14
- data/perf/mongo_doc_object.rb +0 -83
- data/perf/mongo_document.rb +0 -84
- data/perf/ruby_driver.rb +0 -49
- data/script/console +0 -8
- data/spec/active_model_behavior.rb +0 -64
- data/spec/array_including_argument_matcher.rb +0 -62
- data/spec/associations/collection_proxy_spec.rb +0 -233
- data/spec/associations/document_proxy_spec.rb +0 -45
- data/spec/associations/hash_proxy_spec.rb +0 -181
- data/spec/associations/proxy_base_spec.rb +0 -92
- data/spec/associations_spec.rb +0 -218
- data/spec/attributes_accessor_spec.rb +0 -39
- data/spec/attributes_spec.rb +0 -184
- data/spec/bson_matchers.rb +0 -54
- data/spec/bson_spec.rb +0 -196
- data/spec/collection_spec.rb +0 -169
- data/spec/connection_spec.rb +0 -169
- data/spec/contexts/ids_spec.rb +0 -49
- data/spec/contexts/mongo_spec.rb +0 -241
- data/spec/contexts_spec.rb +0 -56
- data/spec/criteria/optional_spec.rb +0 -26
- data/spec/criteria_spec.rb +0 -73
- data/spec/cursor_spec.rb +0 -91
- data/spec/database_cleaner_spec.rb +0 -24
- data/spec/document_ext.rb +0 -9
- data/spec/document_spec.rb +0 -569
- data/spec/embedded_save_spec.rb +0 -73
- data/spec/ext_spec.rb +0 -100
- data/spec/finders_spec.rb +0 -61
- data/spec/hash_matchers.rb +0 -29
- data/spec/index_spec.rb +0 -79
- data/spec/matchers_spec.rb +0 -342
- data/spec/mongodb.yml +0 -6
- data/spec/mongodb_pairs.yml +0 -8
- data/spec/new_record_spec.rb +0 -128
- data/spec/polymorphic_collection_spec.rb +0 -47
- data/spec/references_many_spec.rb +0 -214
- data/spec/references_spec.rb +0 -121
- data/spec/root_spec.rb +0 -41
- data/spec/scope_spec.rb +0 -88
- data/spec/spec_helper.rb +0 -16
- data/spec/timestamps_spec.rb +0 -154
- data/spec/update_spec.rb +0 -167
- data/spec/validations_spec.rb +0 -30
data/README.textile
CHANGED
@@ -1,211 +1,22 @@
|
|
1
1
|
h1. MongoDoc
|
2
2
|
|
3
|
-
Version: 0.6.
|
3
|
+
Version: 0.6.27 2011/03/05
|
4
4
|
|
5
|
-
|
5
|
+
I use MongoDoc in production on my own apps, and have been using (lightly)
|
6
|
+
MongoMapper and (heavily) Mongoid since 2008. By Summer 2010, I had come to the
|
7
|
+
conclusion that something more 'native' was the right way to go, and I began
|
8
|
+
to use more and more features of MongoDB directly in my code. After more than
|
9
|
+
six months, I am convinced that 'native' is the right approach.
|
6
10
|
|
7
|
-
|
11
|
+
I considered changing MongoDoc to match this 'native' approach, even going so
|
12
|
+
far as commiting to it in an email on September 4th, 2010 (Oy!). Instead, I
|
13
|
+
slowed down and have finally decided to stop further development of MongoDoc. I
|
14
|
+
will make bugfixes to MongoDoc as needed, but will no longer be adding features
|
15
|
+
(excepting some that had not yet been pushed to the public repo).
|
8
16
|
|
9
|
-
|
10
|
-
|
11
|
-
* install MongoDB (at least 1.4.0)
|
12
|
-
* install the Ruby driver @gem install mongo@
|
13
|
-
* install the native extensions for BSON @gem install bson_ext@
|
14
|
-
* install MongoDoc @gem install mongo_doc@
|
15
|
-
* run an example from this directory @ruby -Ilib examples/simple_object.rb@
|
16
|
-
|
17
|
-
h2. Introduction
|
18
|
-
|
19
|
-
MongoDoc is a simple and easy to use ActiveRecord-like object mapper for "mongoDB":http://www.mongodb.org in Ruby.
|
20
|
-
|
21
|
-
MongoDoc is _also_ an extension of the "Mongo Ruby Driver":http://github.com/mongodb/mongo-ruby-driver making it a snap to get Ruby in and out of mongoDB.
|
22
|
-
|
23
|
-
MongoDoc is *not* ActiveRecord for mongoDB. We do not have callbacks, nor do we have dynamic finders. We do have associations, named scopes, and other features.
|
24
|
-
|
25
|
-
MongoDoc *is* simple, easy-to-use, and fast. And it works with Rails 3.
|
26
|
-
|
27
|
-
MongoDoc is designed to work with document data, if you are looking to map relational data in mongoDB, you will have to look elsewhere.
|
28
|
-
|
29
|
-
h2. Ruby objects in mongoDB
|
30
|
-
|
31
|
-
Lets just get right into it and save some Ruby objects in mongoDB!
|
32
|
-
|
33
|
-
bc.. class Contact
|
34
|
-
attr_accessor :name, :addresses, :interests
|
35
|
-
end
|
36
|
-
|
37
|
-
class Address
|
38
|
-
attr_accessor :street, :city, :state, :zip, :phone_number
|
39
|
-
end
|
40
|
-
|
41
|
-
p. With MongoDoc, instead of saving JSON[2], we can save an object directly:
|
42
|
-
|
43
|
-
bc.. contact = Contact.new
|
44
|
-
contact.name = 'Hashrocket'
|
45
|
-
contact.interests = ['ruby', 'rails', 'agile']
|
46
|
-
|
47
|
-
address = Address.new
|
48
|
-
address.street = '320 First Street North, #712'
|
49
|
-
address.city = 'Jacksonville Beach'
|
50
|
-
address.state = 'FL'
|
51
|
-
address.zip = '32250'
|
52
|
-
address.phone_number = '877 885 8846'
|
53
|
-
contact.addresses = [address]
|
54
|
-
|
55
|
-
collection.save(contact)
|
56
|
-
|
57
|
-
p. We can query using the powerful mongoDB query syntax, and have it return Ruby objects:
|
58
|
-
|
59
|
-
bc.. in_fl = collection.where('addresses.state' => 'FL')
|
60
|
-
in_fl_hashrocket = in_fl.where('name' => /rocket/)
|
61
|
-
puts in_fl_hashrocket.first.addresses.first.phone_number
|
62
|
-
|
63
|
-
p. Take a look in the examples directory for more code.
|
64
|
-
|
65
|
-
h2. Mapping Documents
|
66
|
-
|
67
|
-
MongoDoc provides ActiveRecord-like persistence, associations, named scopes, and validations (from "Validatable":http://github.com/durran/validatable) as well as a mongoDB query language (from "Mongoid":http://mongoid.org/home). MongoDoc also plays nicely with Rails.
|
68
|
-
|
69
|
-
@MongoDoc::Document@ provides all these features as a mixin. A @MongoDoc::Document@ can either be a top-level mongoDB document, or an embedded document contained within a top-level document. Top-level documents are stored in collections named after their class: @Contact@ objects are stored in the 'contacts' collection (much like ActiveRecord).
|
70
|
-
|
71
|
-
Lets define a @Contact@ document with an @Address@ embedded document:
|
72
|
-
|
73
|
-
bc.. class Address
|
74
|
-
include MongoDoc::Document
|
75
|
-
|
76
|
-
attr_accessor :street
|
77
|
-
attr_accessor :city
|
78
|
-
attr_accessor :state
|
79
|
-
attr_accessor :zip_code
|
80
|
-
attr_accessor :phone_number
|
81
|
-
end
|
82
|
-
|
83
|
-
class Contact
|
84
|
-
include MongoDoc::Document
|
85
|
-
|
86
|
-
attr_accessor :name
|
87
|
-
attr_accessor :interests
|
88
|
-
embed_many :addresses
|
89
|
-
|
90
|
-
scope :in_state, lambda {|state| where('addresses.state' => state)}
|
91
|
-
end
|
92
|
-
|
93
|
-
p. Since a mongoDB document has no fixed schema, we define the composition of a document directly in our classes. We can also specify associations using @embed@, @embed_many@, and @embed_hash@ (similar to ActiveRecord's @has_one@ and @has_many@.
|
94
|
-
|
95
|
-
Building and saving a document is easy:
|
96
|
-
|
97
|
-
bc.. contact = Contact.new(:name => 'Hashrocket', :interests => ['ruby', 'rails', 'agile'])
|
98
|
-
contact.addresses << Address.new(:street => '320 1st Street North, #712',
|
99
|
-
:city => 'Jacksonville Beach',
|
100
|
-
:state => 'FL',
|
101
|
-
:zip_code => '32250',
|
102
|
-
:phone_number => '877 885 8846')
|
103
|
-
contact.save
|
104
|
-
|
105
|
-
p. Now that we have some data, we can query using our named scope:
|
106
|
-
|
107
|
-
bc.. hashrocket_in_fl = Contact.in_state('FL').where(:name => /rocket/)
|
108
|
-
hashrocket_address = hashrocket_in_fl.first.addresses.first
|
109
|
-
|
110
|
-
p. And we can even perform partial updates:
|
111
|
-
|
112
|
-
bc. hashrocket_address.update_attributes(:street => '320 First Street North, #712')
|
113
|
-
|
114
|
-
h2. Installation
|
115
|
-
|
116
|
-
MongoDoc *requires* mongoDB v1.4.0 or later.
|
117
|
-
|
118
|
-
bc. % gem install mongo_doc
|
119
|
-
|
120
|
-
h2. Connecting
|
121
|
-
|
122
|
-
By default, MongoDoc will read its configuration from @./mongodb.yml@. If that file does not exist, it will attempt to connect to a standard MongoDB local server setup and use a database name of @"mongodoc"@.
|
123
|
-
|
124
|
-
h3. With Rails
|
125
|
-
|
126
|
-
If you are using Rails, MongoDoc will look for its configuration in @config/mongodb.yml@. If that file does not exist, it will attempt to connect to a standard MongoDB local server setup and use a database name of @#{Rails.root.basename}_#{Rails.env}@.
|
127
|
-
|
128
|
-
h3. Database configuration file
|
129
|
-
|
130
|
-
The file is similar to the Rails database.yml file, with environment definitions containing the database configuration attributes. For example:
|
131
|
-
|
132
|
-
bc. development:
|
133
|
-
name: development
|
134
|
-
host: localhost
|
135
|
-
port: 27017
|
136
|
-
options:
|
137
|
-
auto_reconnect: true
|
138
|
-
test:
|
139
|
-
name: test
|
140
|
-
host: localhost
|
141
|
-
port: 27017
|
142
|
-
options:
|
143
|
-
auto_reconnect: true
|
144
|
-
|
145
|
-
If you are not using Rails, the default environment is @development@ and you can set the current environment in your code:
|
146
|
-
|
147
|
-
bc. MongoDoc::Connection.env = 'test'
|
148
|
-
|
149
|
-
You can also change the location of the configuration file:
|
150
|
-
|
151
|
-
bc. MongoDoc::Connection.config_path = './config/mongodb.yml'
|
152
|
-
|
153
|
-
h3. Programmatically setting the database connection information
|
154
|
-
|
155
|
-
Finally, if you do not want to use the database configuration file, you can also set the database name, host, port, options, and strict values directly; for example, to set the database name to @stats@:
|
156
|
-
|
157
|
-
bc. MongoDoc::Connection.name = 'stats'
|
158
|
-
|
159
|
-
And if that is not enough control, perhaps you are using authentication, just set the connection directly:
|
160
|
-
|
161
|
-
bc. MongoDoc::Connection.connection = Mongo::Connection.from_uri("mongodb://user:password@localhost/db")
|
162
|
-
|
163
|
-
h2. Cleaning the database during testing
|
164
|
-
|
165
|
-
When testing you may need to clean up and return the database to an empty state. @MongoDoc::DatabaseCleaner.clean_database@ is a helper that can be used to do this, for example with @RSpec@ (note the @require@):
|
166
|
-
|
167
|
-
bc. require 'mongo_doc/database_cleaner'
|
168
|
-
RSpec.configure do |config|
|
169
|
-
config.before(:each) do
|
170
|
-
MongoDoc::DatabaseCleaner.clean_database
|
171
|
-
end
|
172
|
-
end
|
173
|
-
|
174
|
-
h2. Credits
|
175
|
-
|
176
|
-
Les Hill, leshill on github
|
177
|
-
|
178
|
-
h3. Thanks
|
179
|
-
|
180
|
-
Thanks to Sandro and Durran for some great conversations and some lovely code.
|
181
|
-
|
182
|
-
Thanks to Elliot for pushing me to get Rails 3 support
|
183
|
-
|
184
|
-
|
185
|
-
h2. Hacking on MongoDoc
|
186
|
-
|
187
|
-
* Clone the project locally.
|
188
|
-
* Use RVM, you will be happier.
|
189
|
-
* Run: @gem install bundler --pre@
|
190
|
-
* Run: @bundle install@
|
191
|
-
* Make sure MongoDB is running.
|
192
|
-
* Run the specs and features (they should all pass!): @rake spec && rake features@
|
193
|
-
|
194
|
-
h2. Note on Patches/Pull Requests
|
195
|
-
|
196
|
-
* Fork the project.
|
197
|
-
* Make your feature addition or bug fix.
|
198
|
-
* Add tests for it. This is important so I don't break it in a
|
199
|
-
future version unintentionally.
|
200
|
-
* Commit, do not mess with rakefile, version, or history.
|
201
|
-
(if you want to have your own version, that is fine but
|
202
|
-
bump version in a commit by itself I can ignore when I pull)
|
203
|
-
* Send me a pull request. Bonus points for topic branches.
|
17
|
+
If you want a full-featured ODM for Ruby, take a look at
|
18
|
+
"mongoid":https://github.com/mongoid/mongoid .
|
204
19
|
|
205
20
|
h2. Copyright
|
206
21
|
|
207
|
-
Copyright (c) 2009 -
|
208
|
-
|
209
|
-
fn1. If you are using Rails 2.3.x you need to be using the @mongo_doc_rails2@ gem from the rails2 branch on "github":http://github.com/leshill/mongodoc/tree/rails2
|
210
|
-
|
211
|
-
fn2. The Ruby driver exposes an API that understands JSON.
|
22
|
+
Copyright (c) 2009 - 2011 Les Hill. See LICENSE for details.
|
data/Rakefile
CHANGED
@@ -1,31 +1,5 @@
|
|
1
1
|
require 'rake'
|
2
2
|
|
3
|
-
begin
|
4
|
-
require 'jeweler'
|
5
|
-
Jeweler::Tasks.new do |gem|
|
6
|
-
gem.name = "mongo_doc"
|
7
|
-
gem.summary = "ODM for MongoDB"
|
8
|
-
gem.description = "ODM for MongoDB"
|
9
|
-
gem.email = "leshill@gmail.com"
|
10
|
-
gem.homepage = "http://github.com/leshill/mongodoc"
|
11
|
-
gem.authors = ["Les Hill"]
|
12
|
-
gem.files.include('lib/**/*.task')
|
13
|
-
gem.add_dependency "activesupport", ">= 3.0.0"
|
14
|
-
gem.add_dependency "activemodel", ">=3.0.0"
|
15
|
-
gem.add_dependency "bson", "= 1.0.4"
|
16
|
-
gem.add_dependency "bson_ext", "= 1.0.4"
|
17
|
-
gem.add_dependency "mongo", ">= 1.0.0"
|
18
|
-
gem.add_dependency "tzinfo", ">= 0.3.22"
|
19
|
-
gem.add_development_dependency "cucumber", ">= 0.8.5"
|
20
|
-
gem.add_development_dependency "jeweler", ">= 1.4.0"
|
21
|
-
gem.add_development_dependency "rspec", ">= 2.0.0"
|
22
|
-
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
23
|
-
end
|
24
|
-
Jeweler::GemcutterTasks.new
|
25
|
-
rescue LoadError
|
26
|
-
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
27
|
-
end
|
28
|
-
|
29
3
|
require 'cucumber/rake/task'
|
30
4
|
Cucumber::Rake::Task.new(:features) do |t|
|
31
5
|
t.cucumber_opts = "--format pretty --tag ~@wip"
|
@@ -47,8 +21,6 @@ RSpec::Core::RakeTask.new(:rcov) do |spec|
|
|
47
21
|
spec.rcov = true
|
48
22
|
end
|
49
23
|
|
50
|
-
task :spec => :check_dependencies
|
51
|
-
|
52
24
|
task :default => :spec
|
53
25
|
|
54
26
|
require 'rake/rdoctask'
|
@@ -64,109 +36,3 @@ Rake::RDocTask.new do |rdoc|
|
|
64
36
|
rdoc.rdoc_files.include('README*')
|
65
37
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
66
38
|
end
|
67
|
-
|
68
|
-
namespace :mongoid do
|
69
|
-
desc 'Sync criteria from Mongoid'
|
70
|
-
task :sync do
|
71
|
-
require 'pathname'
|
72
|
-
|
73
|
-
src_dir = Pathname.new('../durran-mongoid/lib/mongoid')
|
74
|
-
dest_dir = Pathname.new('lib/mongoid')
|
75
|
-
dest_dir.mkpath
|
76
|
-
%w(criteria.rb contexts/paging.rb contexts/enumerable.rb criterion extensions/symbol/inflections.rb extensions/hash/criteria_helpers.rb matchers).each do |f|
|
77
|
-
src = src_dir + f
|
78
|
-
if src.directory?
|
79
|
-
FileUtils.cp_r(src, dest_dir)
|
80
|
-
else
|
81
|
-
dest = dest_dir + f
|
82
|
-
dest.dirname.mkpath
|
83
|
-
FileUtils.cp(src, dest)
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
namespace :bench do
|
90
|
-
desc 'Run benchmark for MongoDoc'
|
91
|
-
task 'mongo_doc' do
|
92
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
|
93
|
-
require 'perf/mongo_document'
|
94
|
-
benchmark("MongoDoc saving documents", MongoDocument.new)
|
95
|
-
end
|
96
|
-
|
97
|
-
desc 'Run benchmark for MongoDoc Object'
|
98
|
-
task 'mongo_object' do
|
99
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
|
100
|
-
require 'perf/mongo_doc_object'
|
101
|
-
benchmark("MongoDoc saving objects", MongoDocObject.new)
|
102
|
-
end
|
103
|
-
|
104
|
-
desc 'Run profiler for driver'
|
105
|
-
task 'driver' do
|
106
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
|
107
|
-
require 'perf/ruby_driver'
|
108
|
-
benchmark("Ruby driver saving hashes", RubyDriver.new)
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
namespace :prof do
|
113
|
-
desc 'Run profiler for MongoDoc'
|
114
|
-
task 'mongo_doc' do
|
115
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
|
116
|
-
require 'perf/mongo_document'
|
117
|
-
profile("MongoDoc saving documents", MongoDocument.new)
|
118
|
-
end
|
119
|
-
|
120
|
-
desc 'Run profiler for MongoDoc Object'
|
121
|
-
task 'mongo_object' do
|
122
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
|
123
|
-
require 'perf/mongo_doc_object'
|
124
|
-
profile("MongoDoc saving objects", MongoDocObject.new)
|
125
|
-
end
|
126
|
-
|
127
|
-
desc 'Run profiler for driver'
|
128
|
-
task 'driver' do
|
129
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
|
130
|
-
require 'perf/ruby_driver'
|
131
|
-
profile("Ruby driver saving hashes", RubyDriver.new)
|
132
|
-
end
|
133
|
-
|
134
|
-
def benchmark(what, runner)
|
135
|
-
puts "Benchmark: " + what
|
136
|
-
|
137
|
-
runner.generate(10000)
|
138
|
-
|
139
|
-
Benchmark.bm do |bm|
|
140
|
-
bm.report(what + " writes") do
|
141
|
-
runner.writes(10000)
|
142
|
-
end
|
143
|
-
end
|
144
|
-
|
145
|
-
Benchmark.bm do |bm|
|
146
|
-
bm.report(what + " reads") do
|
147
|
-
runner.reads(10000)
|
148
|
-
end
|
149
|
-
end
|
150
|
-
|
151
|
-
Benchmark.bm do |bm|
|
152
|
-
bm.report(what + " query") do
|
153
|
-
runner.query_all
|
154
|
-
end
|
155
|
-
end
|
156
|
-
end
|
157
|
-
|
158
|
-
def profile(what, runner)
|
159
|
-
puts "Profiling: " + what
|
160
|
-
RubyProf.start
|
161
|
-
|
162
|
-
runner.generate(1000)
|
163
|
-
runner.writes(1000)
|
164
|
-
runner.reads(1000)
|
165
|
-
runner.query_all
|
166
|
-
|
167
|
-
result = RubyProf.stop
|
168
|
-
printer = RubyProf::FlatPrinter.new(result)
|
169
|
-
printer.print(STDOUT, 0)
|
170
|
-
end
|
171
|
-
end
|
172
|
-
|
data/lib/mongo_doc/collection.rb
CHANGED
@@ -43,6 +43,10 @@ module MongoDoc
|
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
+
def find_and_modify(opts)
|
47
|
+
MongoDoc::BSON.decode(_collection.find_and_modify(opts))
|
48
|
+
end
|
49
|
+
|
46
50
|
def find_one(spec_or_object_id = nil, options = {})
|
47
51
|
MongoDoc::BSON.decode(_collection.find_one(spec_or_object_id, options))
|
48
52
|
end
|
data/lib/mongo_doc/finders.rb
CHANGED
@@ -28,6 +28,7 @@ module MongoDoc
|
|
28
28
|
# <tt>Person.find_one(:conditions => {:age.gt => 25}, :order_by => [[:name,
|
29
29
|
# :asc]])</tt>
|
30
30
|
def find_one(conditions_or_id)
|
31
|
+
return nil if conditions_or_id.nil?
|
31
32
|
if Hash === conditions_or_id
|
32
33
|
Mongoid::Criteria.translate(self, conditions_or_id).one
|
33
34
|
else
|
data/lib/mongo_doc/timestamps.rb
CHANGED
@@ -12,7 +12,7 @@ module MongoDoc
|
|
12
12
|
[:created_at, :updated_at].each do |name|
|
13
13
|
_keys << name unless _keys.include?(name)
|
14
14
|
attr_reader name
|
15
|
-
|
15
|
+
class_eval(<<-RUBY, __FILE__, __LINE__)
|
16
16
|
def #{name}=(value) # def created_at=(value)
|
17
17
|
if value.kind_of?(String) # if value.kind_of?(String)
|
18
18
|
value = Time.cast_from_string(value) # value = Time.cast_from_string(value)
|
@@ -22,7 +22,7 @@ module MongoDoc
|
|
22
22
|
RUBY
|
23
23
|
end
|
24
24
|
|
25
|
-
|
25
|
+
class_eval(<<-RUBY, __FILE__, __LINE__)
|
26
26
|
def _save(safe)
|
27
27
|
if new_record?
|
28
28
|
self.created_at = self.updated_at = Time.now
|
@@ -43,12 +43,13 @@ module MongoDoc
|
|
43
43
|
def _update(selector, data, safe)
|
44
44
|
original_updated_at = updated_at
|
45
45
|
self.updated_at = Time.now
|
46
|
+
data[:updated_at] = updated_at
|
46
47
|
super
|
47
48
|
rescue Mongo::MongoDBError => e
|
48
49
|
self.updated_at = original_updated_at
|
49
50
|
raise e
|
50
51
|
end
|
51
|
-
|
52
|
+
RUBY
|
52
53
|
end
|
53
54
|
end
|
54
55
|
end
|