mongo_doc_rails2 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +8 -0
- data/HISTORY.md +11 -0
- data/LICENSE +20 -0
- data/README.textile +185 -0
- data/Rakefile +188 -0
- data/TODO +40 -0
- data/VERSION +1 -0
- data/data/.gitignore +2 -0
- data/examples/simple_document.rb +46 -0
- data/examples/simple_object.rb +34 -0
- data/features/collections.feature +9 -0
- data/features/embed_hash.feature +16 -0
- data/features/finders.feature +76 -0
- data/features/indexes.feature +28 -0
- data/features/mongodb.yml +7 -0
- data/features/mongodoc_base.feature +128 -0
- data/features/new_record.feature +36 -0
- data/features/partial_updates.feature +95 -0
- data/features/removing_documents.feature +68 -0
- data/features/saving_an_object.feature +15 -0
- data/features/scopes.feature +66 -0
- data/features/step_definitions/collection_steps.rb +17 -0
- data/features/step_definitions/document_steps.rb +149 -0
- data/features/step_definitions/documents.rb +40 -0
- data/features/step_definitions/embed_hash_steps.rb +6 -0
- data/features/step_definitions/finder_steps.rb +15 -0
- data/features/step_definitions/index_steps.rb +10 -0
- data/features/step_definitions/json_steps.rb +9 -0
- data/features/step_definitions/object_steps.rb +50 -0
- data/features/step_definitions/objects.rb +24 -0
- data/features/step_definitions/partial_update_steps.rb +31 -0
- data/features/step_definitions/query_steps.rb +66 -0
- data/features/step_definitions/removing_documents_steps.rb +14 -0
- data/features/step_definitions/scope_steps.rb +18 -0
- data/features/step_definitions/string_casting_steps.rb +29 -0
- data/features/step_definitions/util_steps.rb +7 -0
- data/features/string_casting.feature +10 -0
- data/features/support/support.rb +10 -0
- data/features/using_criteria.feature +142 -0
- data/lib/mongo_doc.rb +12 -0
- data/lib/mongo_doc/associations.rb +109 -0
- data/lib/mongo_doc/associations/collection_proxy.rb +121 -0
- data/lib/mongo_doc/associations/document_proxy.rb +65 -0
- data/lib/mongo_doc/associations/hash_proxy.rb +102 -0
- data/lib/mongo_doc/associations/proxy_base.rb +48 -0
- data/lib/mongo_doc/attributes.rb +84 -0
- data/lib/mongo_doc/bson.rb +31 -0
- data/lib/mongo_doc/collection.rb +82 -0
- data/lib/mongo_doc/connection.rb +88 -0
- data/lib/mongo_doc/contexts.rb +31 -0
- data/lib/mongo_doc/contexts/ids.rb +41 -0
- data/lib/mongo_doc/contexts/mongo.rb +272 -0
- data/lib/mongo_doc/criteria.rb +70 -0
- data/lib/mongo_doc/cursor.rb +32 -0
- data/lib/mongo_doc/document.rb +205 -0
- data/lib/mongo_doc/ext.rb +16 -0
- data/lib/mongo_doc/ext/array.rb +5 -0
- data/lib/mongo_doc/ext/binary.rb +7 -0
- data/lib/mongo_doc/ext/boolean_class.rb +17 -0
- data/lib/mongo_doc/ext/date.rb +19 -0
- data/lib/mongo_doc/ext/date_time.rb +17 -0
- data/lib/mongo_doc/ext/dbref.rb +7 -0
- data/lib/mongo_doc/ext/hash.rb +7 -0
- data/lib/mongo_doc/ext/min_max_keys.rb +13 -0
- data/lib/mongo_doc/ext/nil_class.rb +5 -0
- data/lib/mongo_doc/ext/numeric.rb +17 -0
- data/lib/mongo_doc/ext/object.rb +19 -0
- data/lib/mongo_doc/ext/object_id.rb +7 -0
- data/lib/mongo_doc/ext/regexp.rb +5 -0
- data/lib/mongo_doc/ext/string.rb +5 -0
- data/lib/mongo_doc/ext/symbol.rb +5 -0
- data/lib/mongo_doc/ext/time.rb +9 -0
- data/lib/mongo_doc/finders.rb +38 -0
- data/lib/mongo_doc/index.rb +46 -0
- data/lib/mongo_doc/matchers.rb +35 -0
- data/lib/mongo_doc/root.rb +26 -0
- data/lib/mongo_doc/scope.rb +64 -0
- data/lib/mongo_doc/validations.rb +12 -0
- data/lib/mongo_doc/validations/macros.rb +11 -0
- data/lib/mongo_doc/validations/validates_embedded.rb +13 -0
- data/lib/mongoid/contexts/enumerable.rb +151 -0
- data/lib/mongoid/contexts/paging.rb +42 -0
- data/lib/mongoid/criteria.rb +239 -0
- data/lib/mongoid/criterion/complex.rb +21 -0
- data/lib/mongoid/criterion/exclusion.rb +65 -0
- data/lib/mongoid/criterion/inclusion.rb +93 -0
- data/lib/mongoid/criterion/optional.rb +136 -0
- data/lib/mongoid/extensions/hash/criteria_helpers.rb +20 -0
- data/lib/mongoid/extensions/symbol/inflections.rb +36 -0
- data/lib/mongoid/matchers/all.rb +11 -0
- data/lib/mongoid/matchers/default.rb +26 -0
- data/lib/mongoid/matchers/exists.rb +13 -0
- data/lib/mongoid/matchers/gt.rb +11 -0
- data/lib/mongoid/matchers/gte.rb +11 -0
- data/lib/mongoid/matchers/in.rb +11 -0
- data/lib/mongoid/matchers/lt.rb +11 -0
- data/lib/mongoid/matchers/lte.rb +11 -0
- data/lib/mongoid/matchers/ne.rb +11 -0
- data/lib/mongoid/matchers/nin.rb +11 -0
- data/lib/mongoid/matchers/size.rb +11 -0
- data/mongo_doc_rails2.gemspec +237 -0
- data/mongod.example.yml +2 -0
- data/mongodb.example.yml +14 -0
- data/perf/mongo_doc_object.rb +83 -0
- data/perf/mongo_document.rb +84 -0
- data/perf/ruby_driver.rb +49 -0
- data/script/console +8 -0
- data/spec/array_including_argument_matcher.rb +62 -0
- data/spec/associations/collection_proxy_spec.rb +233 -0
- data/spec/associations/document_proxy_spec.rb +45 -0
- data/spec/associations/hash_proxy_spec.rb +181 -0
- data/spec/associations/proxy_base_spec.rb +92 -0
- data/spec/associations_spec.rb +218 -0
- data/spec/attributes_accessor_spec.rb +33 -0
- data/spec/attributes_spec.rb +145 -0
- data/spec/bson_matchers.rb +54 -0
- data/spec/bson_spec.rb +196 -0
- data/spec/collection_spec.rb +169 -0
- data/spec/connection_spec.rb +147 -0
- data/spec/contexts/ids_spec.rb +49 -0
- data/spec/contexts/mongo_spec.rb +235 -0
- data/spec/contexts_spec.rb +56 -0
- data/spec/criteria_spec.rb +69 -0
- data/spec/cursor_spec.rb +91 -0
- data/spec/document_ext.rb +9 -0
- data/spec/document_spec.rb +553 -0
- data/spec/embedded_save_spec.rb +73 -0
- data/spec/ext_spec.rb +89 -0
- data/spec/finders_spec.rb +61 -0
- data/spec/hash_matchers.rb +27 -0
- data/spec/index_spec.rb +79 -0
- data/spec/matchers_spec.rb +342 -0
- data/spec/mongodb.yml +6 -0
- data/spec/mongodb_pairs.yml +8 -0
- data/spec/new_record_spec.rb +128 -0
- data/spec/root_spec.rb +41 -0
- data/spec/scope_spec.rb +79 -0
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +14 -0
- data/spec/validations_spec.rb +30 -0
- metadata +346 -0
data/.document
ADDED
data/.gitignore
ADDED
data/HISTORY.md
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
## 0.5.0 to 0.5.5
|
2
|
+
|
3
|
+
* update to ruby driver 0.20.1 and the new bson gems
|
4
|
+
* require MongoDB 1.4
|
5
|
+
* can be used with ActiveSupport 2 or 3
|
6
|
+
* Rails3 support with [MongoDoc-Rails](http://github.com/leshill/mongo_doc-rails) thanks to elliotcm
|
7
|
+
|
8
|
+
## 0.4.2
|
9
|
+
|
10
|
+
* `Document#update_attributes` uses $ positional operator when used from within an embed_many association
|
11
|
+
* Require MongoDB 1.3.4 or greater
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Les Hill
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.textile
ADDED
@@ -0,0 +1,185 @@
|
|
1
|
+
h1. MongoDoc
|
2
|
+
|
3
|
+
Version: Hurricane (0.6.1) 2010/05/10
|
4
|
+
|
5
|
+
h2. Notes
|
6
|
+
|
7
|
+
This version of MongoDoc is for Rails v2.3.x[1].
|
8
|
+
|
9
|
+
h2. Quick Start
|
10
|
+
|
11
|
+
* install MongoDB (at least 1.4.0)
|
12
|
+
* install the Ruby driver @gem install mongo@
|
13
|
+
* install MongoDoc @gem install mongo_doc_rails2@
|
14
|
+
* run an example from this directory @ruby -Ilib examples/simple_object.rb@
|
15
|
+
|
16
|
+
h2. Introduction
|
17
|
+
|
18
|
+
MongoDoc is a simple and easy to use ActiveRecord-like object mapper for "mongoDB":http://www.mongodb.org in Ruby.
|
19
|
+
|
20
|
+
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.
|
21
|
+
|
22
|
+
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.
|
23
|
+
|
24
|
+
MongoDoc *is* simple, easy-to-use, and fast. This version works with Rails 2.3.x.
|
25
|
+
|
26
|
+
MongoDoc is designed to work with document data, if you are looking to map relational data in mongoDB, you will have to look elsewhere.
|
27
|
+
|
28
|
+
h2. Ruby objects in mongoDB
|
29
|
+
|
30
|
+
Lets just get right into it and save some Ruby objects in mongoDB!
|
31
|
+
|
32
|
+
bc.. class Contact
|
33
|
+
attr_accessor :name, :addresses, :interests
|
34
|
+
end
|
35
|
+
|
36
|
+
class Address
|
37
|
+
attr_accessor :street, :city, :state, :zip, :phone_number
|
38
|
+
end
|
39
|
+
|
40
|
+
p. With MongoDoc, instead of saving JSON[2], we can save an object directly:
|
41
|
+
|
42
|
+
bc.. contact = Contact.new
|
43
|
+
contact.name = 'Hashrocket'
|
44
|
+
contact.interests = ['ruby', 'rails', 'agile']
|
45
|
+
|
46
|
+
address = Address.new
|
47
|
+
address.street = '320 First Street North, #712'
|
48
|
+
address.city = 'Jacksonville Beach'
|
49
|
+
address.state = 'FL'
|
50
|
+
address.zip = '32250'
|
51
|
+
address.phone_number = '877 885 8846'
|
52
|
+
contact.addresses = [address]
|
53
|
+
|
54
|
+
collection.save(contact)
|
55
|
+
|
56
|
+
p. We can query using the powerful mongoDB query syntax, and have it return Ruby objects:
|
57
|
+
|
58
|
+
bc.. in_fl = collection.where('addresses.state' => 'FL')
|
59
|
+
in_fl_hashrocket = in_fl.where('name' => /rocket/)
|
60
|
+
puts in_fl_hashrocket.first.addresses.first.phone_number
|
61
|
+
|
62
|
+
p. Take a look in the examples directory for more code.
|
63
|
+
|
64
|
+
h2. Mapping Documents
|
65
|
+
|
66
|
+
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.
|
67
|
+
|
68
|
+
@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).
|
69
|
+
|
70
|
+
Lets define a @Contact@ document with an @Address@ embedded document:
|
71
|
+
|
72
|
+
bc.. class Address
|
73
|
+
include MongoDoc::Document
|
74
|
+
|
75
|
+
attr_accessor :street
|
76
|
+
attr_accessor :city
|
77
|
+
attr_accessor :state
|
78
|
+
attr_accessor :zip_code
|
79
|
+
attr_accessor :phone_number
|
80
|
+
end
|
81
|
+
|
82
|
+
class Contact
|
83
|
+
include MongoDoc::Document
|
84
|
+
|
85
|
+
attr_accessor :name
|
86
|
+
attr_accessor :interests
|
87
|
+
embed_many :addresses
|
88
|
+
|
89
|
+
scope :in_state, lambda {|state| where('addresses.state' => state)}
|
90
|
+
end
|
91
|
+
|
92
|
+
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@.
|
93
|
+
|
94
|
+
Building and saving a document is easy:
|
95
|
+
|
96
|
+
bc.. contact = Contact.new(:name => 'Hashrocket', :interests => ['ruby', 'rails', 'agile'])
|
97
|
+
contact.addresses << Address.new(:street => '320 1st Street North, #712',
|
98
|
+
:city => 'Jacksonville Beach',
|
99
|
+
:state => 'FL',
|
100
|
+
:zip_code => '32250',
|
101
|
+
:phone_number => '877 885 8846')
|
102
|
+
contact.save
|
103
|
+
|
104
|
+
p. Now that we have some data, we can query using our named scope:
|
105
|
+
|
106
|
+
bc.. hashrocket_in_fl = Contact.in_state('FL').where(:name => /rocket/)
|
107
|
+
hashrocket_address = hashrocket_in_fl.first.addresses.first
|
108
|
+
|
109
|
+
p. And we can even perform partial updates:
|
110
|
+
|
111
|
+
bc. hashrocket_address.update_attributes(:street => '320 First Street North, #712')
|
112
|
+
|
113
|
+
h2. Installation
|
114
|
+
|
115
|
+
MongoDoc *requires* mongoDB v1.4.0 or later.
|
116
|
+
|
117
|
+
bc. gem install mongo_doc_rails2
|
118
|
+
|
119
|
+
h2. Connecting
|
120
|
+
|
121
|
+
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"@.
|
122
|
+
|
123
|
+
h3. With Rails
|
124
|
+
|
125
|
+
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}@.
|
126
|
+
|
127
|
+
h3. Database configuration file
|
128
|
+
|
129
|
+
The file is similar to the Rails database.yml file, with environment definitions containing the database configuration attributes. For example:
|
130
|
+
|
131
|
+
bc. development:
|
132
|
+
name: development
|
133
|
+
host: localhost
|
134
|
+
port: 27017
|
135
|
+
options:
|
136
|
+
auto_reconnect: true
|
137
|
+
test:
|
138
|
+
name: test
|
139
|
+
host: localhost
|
140
|
+
port: 27017
|
141
|
+
options:
|
142
|
+
auto_reconnect: true
|
143
|
+
|
144
|
+
If you are not using Rails, the default environment is @development@ and you can set the current environment in your code:
|
145
|
+
|
146
|
+
bc. MongoDoc::Connection.env = 'test'
|
147
|
+
|
148
|
+
You can also change the location of the configuration file:
|
149
|
+
|
150
|
+
bc. MongoDoc::Connection.config_path = './config/mongodb.yml'
|
151
|
+
|
152
|
+
h3. Programmatically setting the database connection information
|
153
|
+
|
154
|
+
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@:
|
155
|
+
|
156
|
+
bc. MongoDoc::Connection.name = 'stats'
|
157
|
+
|
158
|
+
h2. Credits
|
159
|
+
|
160
|
+
Les Hill, leshill on github
|
161
|
+
|
162
|
+
h3. Thanks
|
163
|
+
|
164
|
+
Thanks to Sandro and Durran for some great conversations and some lovely code.
|
165
|
+
|
166
|
+
Thanks to Elliot for pushing me to get Rails 3 support
|
167
|
+
|
168
|
+
h2. Note on Patches/Pull Requests
|
169
|
+
|
170
|
+
* Fork the project.
|
171
|
+
* Make your feature addition or bug fix.
|
172
|
+
* Add tests for it. This is important so I don't break it in a
|
173
|
+
future version unintentionally.
|
174
|
+
* Commit, do not mess with rakefile, version, or history.
|
175
|
+
(if you want to have your own version, that is fine but
|
176
|
+
bump version in a commit by itself I can ignore when I pull)
|
177
|
+
* Send me a pull request. Bonus points for topic branches.
|
178
|
+
|
179
|
+
h2. Copyright
|
180
|
+
|
181
|
+
Copyright (c) 2009 - 2010 Les Hill. See LICENSE for details.
|
182
|
+
|
183
|
+
fn1. If you are using Rails 3.x you need to be using the @mongo_doc@ gem from the master branch on "github":http://github.com/leshill/mongodoc .
|
184
|
+
|
185
|
+
fn2. The Ruby driver exposes an API that understands JSON.
|
data/Rakefile
ADDED
@@ -0,0 +1,188 @@
|
|
1
|
+
require 'rake'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'jeweler'
|
5
|
+
Jeweler::Tasks.new do |gem|
|
6
|
+
gem.name = "mongo_doc_rails2"
|
7
|
+
gem.summary = %Q{ODM for MongoDB - for Rails 2.3.x}
|
8
|
+
gem.description = %Q{ODM for MongoDB - for Rails 2.3.x}
|
9
|
+
gem.email = "leshill@gmail.com"
|
10
|
+
gem.homepage = "http://github.com/leshill/mongodoc"
|
11
|
+
gem.authors = ["Les Hill"]
|
12
|
+
gem.add_dependency "activesupport", ">= 2.3.4"
|
13
|
+
gem.add_dependency "mongo", "= 1.0.1"
|
14
|
+
gem.add_dependency "bson", "= 1.0.1"
|
15
|
+
gem.add_dependency "bson_ext", "= 1.0.1"
|
16
|
+
gem.add_dependency "durran-validatable", "= 2.0.1"
|
17
|
+
gem.add_dependency "leshill-will_paginate", "= 2.3.11"
|
18
|
+
gem.add_development_dependency "rspec", "= 1.3.0"
|
19
|
+
gem.add_development_dependency "cucumber", ">= 0.6.2"
|
20
|
+
end
|
21
|
+
Jeweler::GemcutterTasks.new
|
22
|
+
rescue LoadError
|
23
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
24
|
+
end
|
25
|
+
|
26
|
+
require 'cucumber/rake/task'
|
27
|
+
Cucumber::Rake::Task.new(:features) do |t|
|
28
|
+
t.cucumber_opts = "--format pretty --tag ~@wip"
|
29
|
+
end
|
30
|
+
|
31
|
+
namespace :cucumber do
|
32
|
+
Cucumber::Rake::Task.new(:wip) do |t|
|
33
|
+
t.cucumber_opts = "--format pretty --tag @wip"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
require 'spec/rake/spectask'
|
38
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
39
|
+
spec.spec_opts = ['--options', "#{File.expand_path(File.dirname(__FILE__))}/spec/spec.opts"]
|
40
|
+
spec.libs << 'lib' << 'spec'
|
41
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
42
|
+
end
|
43
|
+
|
44
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
45
|
+
spec.spec_opts = ['--options', "#{File.expand_path(File.dirname(__FILE__))}/spec/spec.opts"]
|
46
|
+
spec.libs << 'lib' << 'spec'
|
47
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
48
|
+
spec.rcov = true
|
49
|
+
end
|
50
|
+
|
51
|
+
task :spec => :check_dependencies
|
52
|
+
|
53
|
+
task :default => :spec
|
54
|
+
|
55
|
+
require 'rake/rdoctask'
|
56
|
+
Rake::RDocTask.new do |rdoc|
|
57
|
+
if File.exist?('VERSION')
|
58
|
+
version = File.read('VERSION')
|
59
|
+
else
|
60
|
+
version = ""
|
61
|
+
end
|
62
|
+
|
63
|
+
rdoc.rdoc_dir = 'rdoc'
|
64
|
+
rdoc.title = "MongoDoc #{version}"
|
65
|
+
rdoc.rdoc_files.include('README*')
|
66
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
67
|
+
end
|
68
|
+
|
69
|
+
namespace :mongo do
|
70
|
+
desc 'Start mongod'
|
71
|
+
task :start do
|
72
|
+
default_config = { "dbpath" => "/data/db" }
|
73
|
+
config = begin
|
74
|
+
YAML.load_file(File.join(File.dirname(__FILE__), 'mongod.yml'))
|
75
|
+
rescue Exception => e
|
76
|
+
{}
|
77
|
+
end
|
78
|
+
config = default_config.merge(config)
|
79
|
+
sh("nohup #{config['mongod'] || 'mongod'} --dbpath #{config['dbpath']} &")
|
80
|
+
puts "\n"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
namespace :mongoid do
|
85
|
+
desc 'Sync criteria from Mongoid'
|
86
|
+
task :sync do
|
87
|
+
require 'pathname'
|
88
|
+
|
89
|
+
src_dir = Pathname.new('../durran-mongoid/lib/mongoid')
|
90
|
+
dest_dir = Pathname.new('lib/mongoid')
|
91
|
+
dest_dir.mkpath
|
92
|
+
%w(criteria.rb contexts/paging.rb contexts/enumerable.rb criterion extensions/symbol/inflections.rb extensions/hash/criteria_helpers.rb matchers).each do |f|
|
93
|
+
src = src_dir + f
|
94
|
+
if src.directory?
|
95
|
+
FileUtils.cp_r(src, dest_dir)
|
96
|
+
else
|
97
|
+
dest = dest_dir + f
|
98
|
+
dest.dirname.mkpath
|
99
|
+
FileUtils.cp(src, dest)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
namespace :bench do
|
106
|
+
desc 'Run benchmark for MongoDoc'
|
107
|
+
task 'mongo_doc' do
|
108
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
|
109
|
+
require 'perf/mongo_document'
|
110
|
+
benchmark("MongoDoc saving documents", MongoDocument.new)
|
111
|
+
end
|
112
|
+
|
113
|
+
desc 'Run benchmark for MongoDoc Object'
|
114
|
+
task 'mongo_object' do
|
115
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
|
116
|
+
require 'perf/mongo_doc_object'
|
117
|
+
benchmark("MongoDoc saving objects", MongoDocObject.new)
|
118
|
+
end
|
119
|
+
|
120
|
+
desc 'Run profiler for driver'
|
121
|
+
task 'driver' do
|
122
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
|
123
|
+
require 'perf/ruby_driver'
|
124
|
+
benchmark("Ruby driver saving hashes", RubyDriver.new)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
namespace :prof do
|
129
|
+
desc 'Run profiler for MongoDoc'
|
130
|
+
task 'mongo_doc' do
|
131
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
|
132
|
+
require 'perf/mongo_document'
|
133
|
+
profile("MongoDoc saving documents", MongoDocument.new)
|
134
|
+
end
|
135
|
+
|
136
|
+
desc 'Run profiler for MongoDoc Object'
|
137
|
+
task 'mongo_object' do
|
138
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
|
139
|
+
require 'perf/mongo_doc_object'
|
140
|
+
profile("MongoDoc saving objects", MongoDocObject.new)
|
141
|
+
end
|
142
|
+
|
143
|
+
desc 'Run profiler for driver'
|
144
|
+
task 'driver' do
|
145
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
|
146
|
+
require 'perf/ruby_driver'
|
147
|
+
profile("Ruby driver saving hashes", RubyDriver.new)
|
148
|
+
end
|
149
|
+
|
150
|
+
def benchmark(what, runner)
|
151
|
+
puts "Benchmark: " + what
|
152
|
+
|
153
|
+
runner.generate(10000)
|
154
|
+
|
155
|
+
Benchmark.bm do |bm|
|
156
|
+
bm.report(what + " writes") do
|
157
|
+
runner.writes(10000)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
Benchmark.bm do |bm|
|
162
|
+
bm.report(what + " reads") do
|
163
|
+
runner.reads(10000)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
Benchmark.bm do |bm|
|
168
|
+
bm.report(what + " query") do
|
169
|
+
runner.query_all
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
def profile(what, runner)
|
175
|
+
puts "Profiling: " + what
|
176
|
+
RubyProf.start
|
177
|
+
|
178
|
+
runner.generate(1000)
|
179
|
+
runner.writes(1000)
|
180
|
+
runner.reads(1000)
|
181
|
+
runner.query_all
|
182
|
+
|
183
|
+
result = RubyProf.stop
|
184
|
+
printer = RubyProf::FlatPrinter.new(result)
|
185
|
+
printer.print(STDOUT, 0)
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
data/TODO
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
As of 2010-03-06
|
2
|
+
|
3
|
+
Associations
|
4
|
+
------------
|
5
|
+
|
6
|
+
Pull associations out of attributes and refactor
|
7
|
+
|
8
|
+
Criteria
|
9
|
+
--------
|
10
|
+
|
11
|
+
Make critieria module; include into CriteriaProxy, Association?
|
12
|
+
page, per_page, paginate shuould be wrapped or not?
|
13
|
+
|
14
|
+
Contexts
|
15
|
+
--------
|
16
|
+
|
17
|
+
Extract Mongo context as module, Document#collection => klass.collection Collection#collection => klass
|
18
|
+
|
19
|
+
Documentation
|
20
|
+
-------------
|
21
|
+
|
22
|
+
How to get started with Rails
|
23
|
+
|
24
|
+
Dynamic Attributes
|
25
|
+
------------------
|
26
|
+
|
27
|
+
Document#[] => reads attribute
|
28
|
+
Document#[]= => writes attribute
|
29
|
+
Document#dynamic_attributes => key, value for each dynamic attribute
|
30
|
+
Document#dynamic_attribute_names => list of dynamic attribute names
|
31
|
+
|
32
|
+
Validations
|
33
|
+
-----------
|
34
|
+
|
35
|
+
validates_hash_keys :has_hash_name, :in => [array of names]
|
36
|
+
|
37
|
+
|
38
|
+
Q
|
39
|
+
-
|
40
|
+
What is Criteria#ids
|