mongo_doc 0.6.26 → 0.6.27

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. data/README.textile +14 -203
  2. data/Rakefile +0 -134
  3. data/lib/mongo_doc/collection.rb +4 -0
  4. data/lib/mongo_doc/finders.rb +1 -0
  5. data/lib/mongo_doc/timestamps.rb +4 -3
  6. data/lib/mongo_doc/version.rb +3 -0
  7. data/lib/mongo_doc.rb +0 -5
  8. metadata +25 -212
  9. data/.document +0 -5
  10. data/.gitignore +0 -8
  11. data/.rspec +0 -2
  12. data/.rvmrc +0 -1
  13. data/.watchr +0 -32
  14. data/Gemfile +0 -3
  15. data/Gemfile.lock +0 -72
  16. data/HISTORY.md +0 -11
  17. data/VERSION +0 -1
  18. data/examples/simple_document.rb +0 -46
  19. data/examples/simple_object.rb +0 -34
  20. data/features/collections.feature +0 -9
  21. data/features/connection.feature +0 -13
  22. data/features/db_references.feature +0 -21
  23. data/features/embed_hash.feature +0 -16
  24. data/features/finders.feature +0 -76
  25. data/features/indexes.feature +0 -28
  26. data/features/mongodb.yml +0 -7
  27. data/features/mongodoc_base.feature +0 -128
  28. data/features/new_record.feature +0 -36
  29. data/features/partial_updates.feature +0 -95
  30. data/features/polymorphic_collections.feature +0 -16
  31. data/features/references.feature +0 -28
  32. data/features/removing_documents.feature +0 -68
  33. data/features/saving_an_object.feature +0 -15
  34. data/features/scopes.feature +0 -66
  35. data/features/step_definitions/collection_steps.rb +0 -17
  36. data/features/step_definitions/connection_steps.rb +0 -13
  37. data/features/step_definitions/document_steps.rb +0 -155
  38. data/features/step_definitions/documents.rb +0 -61
  39. data/features/step_definitions/embed_hash_steps.rb +0 -6
  40. data/features/step_definitions/field_steps.rb +0 -28
  41. data/features/step_definitions/finder_steps.rb +0 -15
  42. data/features/step_definitions/index_steps.rb +0 -10
  43. data/features/step_definitions/json_steps.rb +0 -9
  44. data/features/step_definitions/object_steps.rb +0 -50
  45. data/features/step_definitions/objects.rb +0 -24
  46. data/features/step_definitions/partial_update_steps.rb +0 -31
  47. data/features/step_definitions/query_steps.rb +0 -69
  48. data/features/step_definitions/removing_documents_steps.rb +0 -14
  49. data/features/step_definitions/scope_steps.rb +0 -18
  50. data/features/step_definitions/string_casting_steps.rb +0 -29
  51. data/features/step_definitions/util_steps.rb +0 -7
  52. data/features/string_casting.feature +0 -10
  53. data/features/support/support.rb +0 -12
  54. data/features/timestamps.feature +0 -14
  55. data/features/using_criteria.feature +0 -148
  56. data/mongo_doc.gemspec +0 -272
  57. data/mongodb.example.yml +0 -14
  58. data/perf/mongo_doc_object.rb +0 -83
  59. data/perf/mongo_document.rb +0 -84
  60. data/perf/ruby_driver.rb +0 -49
  61. data/script/console +0 -8
  62. data/spec/active_model_behavior.rb +0 -64
  63. data/spec/array_including_argument_matcher.rb +0 -62
  64. data/spec/associations/collection_proxy_spec.rb +0 -233
  65. data/spec/associations/document_proxy_spec.rb +0 -45
  66. data/spec/associations/hash_proxy_spec.rb +0 -181
  67. data/spec/associations/proxy_base_spec.rb +0 -92
  68. data/spec/associations_spec.rb +0 -218
  69. data/spec/attributes_accessor_spec.rb +0 -39
  70. data/spec/attributes_spec.rb +0 -184
  71. data/spec/bson_matchers.rb +0 -54
  72. data/spec/bson_spec.rb +0 -196
  73. data/spec/collection_spec.rb +0 -169
  74. data/spec/connection_spec.rb +0 -169
  75. data/spec/contexts/ids_spec.rb +0 -49
  76. data/spec/contexts/mongo_spec.rb +0 -241
  77. data/spec/contexts_spec.rb +0 -56
  78. data/spec/criteria/optional_spec.rb +0 -26
  79. data/spec/criteria_spec.rb +0 -73
  80. data/spec/cursor_spec.rb +0 -91
  81. data/spec/database_cleaner_spec.rb +0 -24
  82. data/spec/document_ext.rb +0 -9
  83. data/spec/document_spec.rb +0 -569
  84. data/spec/embedded_save_spec.rb +0 -73
  85. data/spec/ext_spec.rb +0 -100
  86. data/spec/finders_spec.rb +0 -61
  87. data/spec/hash_matchers.rb +0 -29
  88. data/spec/index_spec.rb +0 -79
  89. data/spec/matchers_spec.rb +0 -342
  90. data/spec/mongodb.yml +0 -6
  91. data/spec/mongodb_pairs.yml +0 -8
  92. data/spec/new_record_spec.rb +0 -128
  93. data/spec/polymorphic_collection_spec.rb +0 -47
  94. data/spec/references_many_spec.rb +0 -214
  95. data/spec/references_spec.rb +0 -121
  96. data/spec/root_spec.rb +0 -41
  97. data/spec/scope_spec.rb +0 -88
  98. data/spec/spec_helper.rb +0 -16
  99. data/spec/timestamps_spec.rb +0 -154
  100. data/spec/update_spec.rb +0 -167
  101. 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.26 2010/12/06
3
+ Version: 0.6.27 2011/03/05
4
4
 
5
- h2. Notes
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
- This version of MongoDoc works with Rails 3[1]
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
- h2. Quick Start
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 - 2010 Les Hill. See LICENSE for details.
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
-
@@ -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
@@ -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
@@ -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
- module_eval(<<-RUBY, __FILE__, __LINE__)
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
- module_eval do
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
- end
52
+ RUBY
52
53
  end
53
54
  end
54
55
  end
@@ -0,0 +1,3 @@
1
+ module MongoDoc
2
+ VERSION = "0.6.27"
3
+ end
data/lib/mongo_doc.rb CHANGED
@@ -1,11 +1,6 @@
1
1
  require 'mongo'
2
2
  require 'active_support'
3
3
  require 'active_support/core_ext'
4
-
5
- module MongoDoc
6
- VERSION = '0.6.26'
7
- end
8
-
9
4
  require 'mongo_doc/connection'
10
5
  require 'mongo_doc/collection'
11
6
  require 'mongo_doc/document'