pwnash-mongo_mapper 0.7.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. data/.gitignore +10 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +31 -0
  4. data/Rakefile +37 -0
  5. data/bin/mmconsole +60 -0
  6. data/lib/mongo_mapper.rb +116 -0
  7. data/lib/mongo_mapper/document.rb +314 -0
  8. data/lib/mongo_mapper/embedded_document.rb +71 -0
  9. data/lib/mongo_mapper/plugins.rb +36 -0
  10. data/lib/mongo_mapper/plugins/associations.rb +114 -0
  11. data/lib/mongo_mapper/plugins/associations/base.rb +123 -0
  12. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +30 -0
  13. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +25 -0
  14. data/lib/mongo_mapper/plugins/associations/collection.rb +21 -0
  15. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +39 -0
  16. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +144 -0
  17. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
  18. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +129 -0
  19. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +31 -0
  20. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +23 -0
  21. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +13 -0
  22. data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +41 -0
  23. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +69 -0
  24. data/lib/mongo_mapper/plugins/associations/proxy.rb +124 -0
  25. data/lib/mongo_mapper/plugins/callbacks.rb +240 -0
  26. data/lib/mongo_mapper/plugins/clone.rb +13 -0
  27. data/lib/mongo_mapper/plugins/descendants.rb +16 -0
  28. data/lib/mongo_mapper/plugins/dirty.rb +119 -0
  29. data/lib/mongo_mapper/plugins/equality.rb +23 -0
  30. data/lib/mongo_mapper/plugins/identity_map.rb +122 -0
  31. data/lib/mongo_mapper/plugins/inspect.rb +14 -0
  32. data/lib/mongo_mapper/plugins/keys.rb +317 -0
  33. data/lib/mongo_mapper/plugins/keys/key.rb +44 -0
  34. data/lib/mongo_mapper/plugins/logger.rb +17 -0
  35. data/lib/mongo_mapper/plugins/modifiers.rb +111 -0
  36. data/lib/mongo_mapper/plugins/pagination.rb +24 -0
  37. data/lib/mongo_mapper/plugins/pagination/proxy.rb +72 -0
  38. data/lib/mongo_mapper/plugins/persistence.rb +68 -0
  39. data/lib/mongo_mapper/plugins/protected.rb +45 -0
  40. data/lib/mongo_mapper/plugins/query_logger.rb +68 -0
  41. data/lib/mongo_mapper/plugins/rails.rb +57 -0
  42. data/lib/mongo_mapper/plugins/serialization.rb +75 -0
  43. data/lib/mongo_mapper/plugins/timestamps.rb +21 -0
  44. data/lib/mongo_mapper/plugins/userstamps.rb +14 -0
  45. data/lib/mongo_mapper/plugins/validations.rb +46 -0
  46. data/lib/mongo_mapper/query.rb +143 -0
  47. data/lib/mongo_mapper/support.rb +218 -0
  48. data/lib/mongo_mapper/support/descendant_appends.rb +46 -0
  49. data/lib/mongo_mapper/support/find.rb +77 -0
  50. data/lib/mongo_mapper/version.rb +3 -0
  51. data/mongo_mapper.gemspec +216 -0
  52. data/performance/read_write.rb +52 -0
  53. data/specs.watchr +51 -0
  54. data/test/NOTE_ON_TESTING +1 -0
  55. data/test/active_model_lint_test.rb +13 -0
  56. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +63 -0
  57. data/test/functional/associations/test_belongs_to_proxy.rb +101 -0
  58. data/test/functional/associations/test_in_array_proxy.rb +325 -0
  59. data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
  60. data/test/functional/associations/test_many_documents_proxy.rb +536 -0
  61. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +176 -0
  62. data/test/functional/associations/test_many_embedded_proxy.rb +256 -0
  63. data/test/functional/associations/test_many_polymorphic_proxy.rb +302 -0
  64. data/test/functional/associations/test_one_embedded_proxy.rb +68 -0
  65. data/test/functional/associations/test_one_proxy.rb +196 -0
  66. data/test/functional/test_associations.rb +44 -0
  67. data/test/functional/test_binary.rb +27 -0
  68. data/test/functional/test_callbacks.rb +151 -0
  69. data/test/functional/test_dirty.rb +163 -0
  70. data/test/functional/test_document.rb +1219 -0
  71. data/test/functional/test_embedded_document.rb +210 -0
  72. data/test/functional/test_identity_map.rb +507 -0
  73. data/test/functional/test_indexing.rb +44 -0
  74. data/test/functional/test_logger.rb +20 -0
  75. data/test/functional/test_modifiers.rb +416 -0
  76. data/test/functional/test_pagination.rb +93 -0
  77. data/test/functional/test_protected.rb +163 -0
  78. data/test/functional/test_string_id_compatibility.rb +67 -0
  79. data/test/functional/test_timestamps.rb +64 -0
  80. data/test/functional/test_userstamps.rb +28 -0
  81. data/test/functional/test_validations.rb +342 -0
  82. data/test/models.rb +227 -0
  83. data/test/support/custom_matchers.rb +37 -0
  84. data/test/support/timing.rb +16 -0
  85. data/test/test_helper.rb +64 -0
  86. data/test/unit/associations/test_base.rb +212 -0
  87. data/test/unit/associations/test_proxy.rb +105 -0
  88. data/test/unit/serializers/test_json_serializer.rb +202 -0
  89. data/test/unit/test_descendant_appends.rb +71 -0
  90. data/test/unit/test_document.rb +225 -0
  91. data/test/unit/test_dynamic_finder.rb +123 -0
  92. data/test/unit/test_embedded_document.rb +657 -0
  93. data/test/unit/test_keys.rb +216 -0
  94. data/test/unit/test_mongo_mapper.rb +118 -0
  95. data/test/unit/test_pagination.rb +160 -0
  96. data/test/unit/test_plugins.rb +50 -0
  97. data/test/unit/test_query.rb +374 -0
  98. data/test/unit/test_rails.rb +181 -0
  99. data/test/unit/test_rails_compatibility.rb +52 -0
  100. data/test/unit/test_serialization.rb +51 -0
  101. data/test/unit/test_support.rb +390 -0
  102. data/test/unit/test_time_zones.rb +39 -0
  103. data/test/unit/test_validations.rb +544 -0
  104. metadata +285 -0
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
6
+ *~
7
+ *.gem
8
+ tmp
9
+ .yardoc
10
+ doc/*
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 John Nunemaker
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.rdoc ADDED
@@ -0,0 +1,31 @@
1
+ = MongoMapper
2
+
3
+ A Ruby Object Mapper for Mongo.
4
+
5
+ Releases are tagged on github and released on gemcutter. Master is pushed to whenever I add a patch or a new feature, but I do not release a new gem version each time I push.
6
+
7
+ == Note on Patches/Pull Requests
8
+
9
+ * Fork the project.
10
+ * Make your feature addition or bug fix.
11
+ * Add tests for it. This is important so I don't break it in a future version unintentionally.
12
+ * Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself in another branch so I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Install
16
+
17
+ $ gem install mongo_mapper
18
+
19
+ == Problems or Questions?
20
+
21
+ Hit up the google group.
22
+ http://groups.google.com/group/mongomapper
23
+
24
+ To see if the problem you are having is a verified issue, you can see the MM pivotal tracker project:
25
+ http://www.pivotaltracker.com/projects/33576
26
+
27
+ There is no need to request to join the Pivotal Tracker project as I am only granting access to a select few (easier to keep things organized). If you have a problem, please use the mailing list. If I confirm it to be a bug, I am happy to add it to PT. Thanks!
28
+
29
+ == Copyright
30
+
31
+ Copyright (c) 2009 John Nunemaker. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'jeweler'
4
+
5
+ require File.dirname(__FILE__) + '/lib/mongo_mapper/version'
6
+
7
+ Jeweler::Tasks.new do |gem|
8
+ gem.name = "mongo_mapper"
9
+ gem.summary = %Q{A Ruby Object Mapper for Mongo}
10
+ gem.email = "nunemaker@gmail.com"
11
+ gem.homepage = "http://github.com/jnunemaker/mongomapper"
12
+ gem.authors = ["John Nunemaker"]
13
+ gem.version = MongoMapper::Version
14
+
15
+ gem.add_dependency('activesupport', '>= 2.3.4')
16
+ gem.add_dependency('mongo', '1.0')
17
+ gem.add_dependency('jnunemaker-validatable', '1.8.4')
18
+
19
+ gem.add_development_dependency('json', '>= 1.2.3')
20
+ gem.add_development_dependency('jnunemaker-matchy', '0.4.0')
21
+ gem.add_development_dependency('shoulda', '2.10.2')
22
+ gem.add_development_dependency('timecop', '0.3.1')
23
+ gem.add_development_dependency('mocha', '0.9.8')
24
+ end
25
+
26
+ Jeweler::GemcutterTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'test'
31
+ test.ruby_opts << '-rubygems'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ task :default => :test
37
+ task :test => :check_dependencies
data/bin/mmconsole ADDED
@@ -0,0 +1,60 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
3
+
4
+ begin
5
+ require 'mongo_mapper'
6
+ require 'irb'
7
+ rescue LoadError
8
+ require 'rubygems'
9
+ retry
10
+ end
11
+
12
+ IRB.setup(nil)
13
+ irb = IRB::Irb.new
14
+
15
+ IRB.conf[:MAIN_CONTEXT] = irb.context
16
+
17
+ irb.context.evaluate("require 'irb/completion'", 0)
18
+ irb.context.evaluate(%@
19
+ include MongoMapper
20
+
21
+ MongoMapper.database = "mmtest"
22
+ $db = MongoMapper.database
23
+
24
+ @, 0)
25
+
26
+ puts %@
27
+ Welcome to the MongoMapper Console!
28
+
29
+ Example 1:
30
+ things = $db.collection("things")
31
+ things.insert("name" => "Raw Thing")
32
+ things.insert("name" => "Another Thing", "date" => Time.now)
33
+
34
+ cursor = things.find("name" => "Raw Thing")
35
+ puts cursor.next_object.inspect
36
+
37
+ Example 2:
38
+ class Thing
39
+ include MongoMapper::Document
40
+ key :name, String, :required => true
41
+ key :date, Time
42
+ end
43
+
44
+ thing = Thing.new
45
+ thing.name = "My thing"
46
+ thing.date = Time.now
47
+ thing.save
48
+
49
+ all_things = Thing.all
50
+ puts all_things.map { |object| object.name }.inspect
51
+ @
52
+
53
+ trap("SIGINT") do
54
+ irb.signal_handle
55
+ end
56
+
57
+ catch(:IRB_EXIT) do
58
+ irb.eval_input
59
+ end
60
+
@@ -0,0 +1,116 @@
1
+ # Make sure you have the following libs in your load path or you could have issues:
2
+ # gem 'activesupport', '>= 2.3.4'
3
+ # gem 'mongo', '1.0'
4
+ # gem 'jnunemaker-validatable', '1.8.4'
5
+ require 'set'
6
+ require 'uri'
7
+ require 'mongo'
8
+ require 'validatable'
9
+ require 'active_support/all'
10
+
11
+ module MongoMapper
12
+ # generic MM error
13
+ class MongoMapperError < StandardError; end
14
+
15
+ # raised when key expected to exist but not found
16
+ class KeyNotFound < MongoMapperError; end
17
+
18
+ # raised when document expected but not found
19
+ class DocumentNotFound < MongoMapperError; end
20
+
21
+ # raised when trying to connect using uri with incorrect scheme
22
+ class InvalidScheme < MongoMapperError; end
23
+
24
+ # raised when document not valid and using !
25
+ class DocumentNotValid < MongoMapperError
26
+ def initialize(document)
27
+ super("Validation failed: #{document.errors.full_messages.join(", ")}")
28
+ end
29
+ end
30
+
31
+ # @api public
32
+ def self.connection
33
+ @@connection ||= Mongo::Connection.new
34
+ end
35
+
36
+ # @api public
37
+ def self.connection=(new_connection)
38
+ @@connection = new_connection
39
+ end
40
+
41
+ # @api public
42
+ def self.logger
43
+ connection.logger
44
+ end
45
+
46
+ # @api public
47
+ def self.database=(name)
48
+ @@database = nil
49
+ @@database_name = name
50
+ end
51
+
52
+ # @api public
53
+ def self.database
54
+ if @@database_name.blank?
55
+ raise 'You forgot to set the default database name: MongoMapper.database = "foobar"'
56
+ end
57
+
58
+ @@database ||= MongoMapper.connection.db(@@database_name)
59
+ end
60
+
61
+ def self.config=(hash)
62
+ @@config = hash
63
+ end
64
+
65
+ def self.config
66
+ raise 'Set config before connecting. MongoMapper.config = {...}' unless defined?(@@config)
67
+ @@config
68
+ end
69
+
70
+ # @api private
71
+ def self.config_for_environment(environment)
72
+ env = config[environment]
73
+ return env if env['uri'].blank?
74
+
75
+ uri = URI.parse(env['uri'])
76
+ raise InvalidScheme.new('must be mongodb') unless uri.scheme == 'mongodb'
77
+ {
78
+ 'host' => uri.host,
79
+ 'port' => uri.port,
80
+ 'database' => uri.path.gsub(/^\//, ''),
81
+ 'username' => uri.user,
82
+ 'password' => uri.password,
83
+ }
84
+ end
85
+
86
+ def self.connect(environment, options={})
87
+ raise 'Set config before connecting. MongoMapper.config = {...}' if config.blank?
88
+ env = config_for_environment(environment)
89
+ MongoMapper.connection = Mongo::Connection.new(env['host'], env['port'], options)
90
+ MongoMapper.database = env['database']
91
+ MongoMapper.database.authenticate(env['username'], env['password']) if env['username'] && env['password']
92
+ end
93
+
94
+ def self.setup(config, environment, options={})
95
+ using_passenger = options.delete(:passenger)
96
+ handle_passenger_forking if using_passenger
97
+ self.config = config
98
+ connect(environment, options)
99
+ end
100
+
101
+ def self.handle_passenger_forking
102
+ if defined?(PhusionPassenger)
103
+ PhusionPassenger.on_event(:starting_worker_process) do |forked|
104
+ connection.connect_to_master if forked
105
+ end
106
+ end
107
+ end
108
+
109
+ autoload :Query, 'mongo_mapper/query'
110
+ autoload :Document, 'mongo_mapper/document'
111
+ autoload :EmbeddedDocument, 'mongo_mapper/embedded_document'
112
+ autoload :Version, 'mongo_mapper/version'
113
+ end
114
+
115
+ require 'mongo_mapper/support'
116
+ require 'mongo_mapper/plugins'
@@ -0,0 +1,314 @@
1
+ module MongoMapper
2
+ module Document
3
+ extend Support::DescendantAppends
4
+
5
+ def self.included(model)
6
+ model.class_eval do
7
+ include InstanceMethods
8
+ extend Support::Find
9
+ extend ClassMethods
10
+ extend Plugins
11
+
12
+ plugin Plugins::Associations
13
+ plugin Plugins::Clone
14
+ plugin Plugins::Descendants
15
+ plugin Plugins::Equality
16
+ plugin Plugins::Inspect
17
+ plugin Plugins::Keys
18
+ plugin Plugins::Dirty # for now dirty needs to be after keys
19
+ plugin Plugins::Logger
20
+ plugin Plugins::Modifiers
21
+ plugin Plugins::Pagination
22
+ plugin Plugins::Persistence
23
+ plugin Plugins::Protected
24
+ plugin Plugins::Rails
25
+ plugin Plugins::Serialization
26
+ plugin Plugins::Timestamps
27
+ plugin Plugins::Userstamps
28
+ plugin Plugins::Validations
29
+ plugin Plugins::Callbacks # for now callbacks needs to be after validations
30
+ plugin Plugins::QueryLogger
31
+
32
+ extend Plugins::Validations::DocumentMacros
33
+ end
34
+
35
+ super
36
+ end
37
+
38
+ module ClassMethods
39
+ def inherited(subclass)
40
+ subclass.set_collection_name(collection_name)
41
+ super
42
+ end
43
+
44
+ def ensure_index(spec, options={})
45
+ collection.create_index(spec, options)
46
+ end
47
+
48
+ def find(*args)
49
+ assert_no_first_last_or_all(args)
50
+ options = args.extract_options!
51
+ return nil if args.size == 0
52
+
53
+ if args.first.is_a?(Array) || args.size > 1
54
+ find_some(args, options)
55
+ else
56
+ find_one(options.merge({:_id => args[0]}))
57
+ end
58
+ end
59
+
60
+ def find!(*args)
61
+ assert_no_first_last_or_all(args)
62
+ options = args.extract_options!
63
+ raise DocumentNotFound, "Couldn't find without an ID" if args.size == 0
64
+
65
+ if args.first.is_a?(Array) || args.size > 1
66
+ find_some!(args, options)
67
+ else
68
+ find_one(options.merge({:_id => args[0]})) || raise(DocumentNotFound, "Document match #{options.inspect} does not exist in #{collection.name} collection")
69
+ end
70
+ end
71
+
72
+ def find_each(options={})
73
+ criteria, options = to_query(options)
74
+ collection.find(criteria, options).each do |doc|
75
+ yield load(doc)
76
+ end
77
+ end
78
+
79
+ def find_by_id(id)
80
+ find(id)
81
+ end
82
+
83
+ def first_or_create(args)
84
+ first(args) || create(args.reject { |key, value| !key?(key) })
85
+ end
86
+
87
+ def first_or_new(args)
88
+ first(args) || new(args.reject { |key, value| !key?(key) })
89
+ end
90
+
91
+ def first(options={})
92
+ find_one(options)
93
+ end
94
+
95
+ def last(options={})
96
+ raise ':order option must be provided when using last' if options[:order].blank?
97
+ find_one(options.merge(:order => invert_order_clause(options[:order])))
98
+ end
99
+
100
+ def all(options={})
101
+ find_many(options)
102
+ end
103
+
104
+ def count(options={})
105
+ collection.find(to_criteria(options)).count
106
+ end
107
+
108
+ def exists?(options={})
109
+ !count(options).zero?
110
+ end
111
+
112
+ def create(*docs)
113
+ initialize_each(*docs) { |doc| doc.save }
114
+ end
115
+
116
+ def create!(*docs)
117
+ initialize_each(*docs) { |doc| doc.save! }
118
+ end
119
+
120
+ def update(*args)
121
+ if args.length == 1
122
+ update_multiple(args[0])
123
+ else
124
+ id, attributes = args
125
+ update_single(id, attributes)
126
+ end
127
+ end
128
+
129
+ def delete(*ids)
130
+ collection.remove(to_criteria(:_id => ids.flatten))
131
+ end
132
+
133
+ def delete_all(options={})
134
+ collection.remove(to_criteria(options))
135
+ end
136
+
137
+ def destroy(*ids)
138
+ find_some!(ids.flatten).each(&:destroy)
139
+ end
140
+
141
+ def destroy_all(options={})
142
+ find_each(options) { |document| document.destroy }
143
+ end
144
+
145
+ def embeddable?
146
+ false
147
+ end
148
+
149
+ def single_collection_inherited?
150
+ keys.key?(:_type) && single_collection_inherited_superclass?
151
+ end
152
+
153
+ def single_collection_inherited_superclass?
154
+ superclass.respond_to?(:keys) && superclass.keys.key?(:_type)
155
+ end
156
+
157
+ private
158
+ def initialize_each(*docs)
159
+ instances = []
160
+ docs = [{}] if docs.blank?
161
+ docs.flatten.each do |attrs|
162
+ doc = new(attrs)
163
+ yield(doc)
164
+ instances << doc
165
+ end
166
+ instances.size == 1 ? instances[0] : instances
167
+ end
168
+
169
+ def assert_no_first_last_or_all(args)
170
+ if args[0] == :first || args[0] == :last || args[0] == :all
171
+ raise ArgumentError, "#{self}.find(:#{args}) is no longer supported, use #{self}.#{args} instead."
172
+ end
173
+ end
174
+
175
+ def find_some(ids, options={})
176
+ ids = ids.flatten.compact.uniq
177
+ find_many(options.merge(:_id => ids)).compact
178
+ end
179
+
180
+ def find_some!(ids, options={})
181
+ ids = ids.flatten.compact.uniq
182
+ documents = find_some(ids, options)
183
+
184
+ if ids.size == documents.size
185
+ documents
186
+ else
187
+ raise DocumentNotFound, "Couldn't find all of the ids (#{ids.to_sentence}). Found #{documents.size}, but was expecting #{ids.size}"
188
+ end
189
+ end
190
+
191
+ # All query methods that load documents pass through find_one or find_many
192
+ def find_one(options={})
193
+ criteria, options = to_query(options)
194
+ if doc = collection.find_one(criteria, options)
195
+ load(doc)
196
+ end
197
+ end
198
+
199
+ # All query methods that load documents pass through find_one or find_many
200
+ def find_many(options)
201
+ criteria, options = to_query(options)
202
+ collection.find(criteria, options).to_a.map do |doc|
203
+ load(doc)
204
+ end
205
+ end
206
+
207
+ def invert_order_clause(order)
208
+ order.split(',').map do |order_segment|
209
+ if order_segment =~ /\sasc/i
210
+ order_segment.sub /\sasc/i, ' desc'
211
+ elsif order_segment =~ /\sdesc/i
212
+ order_segment.sub /\sdesc/i, ' asc'
213
+ else
214
+ "#{order_segment.strip} desc"
215
+ end
216
+ end.join(',')
217
+ end
218
+
219
+ def update_single(id, attrs)
220
+ if id.blank? || attrs.blank? || !attrs.is_a?(Hash)
221
+ raise ArgumentError, "Updating a single document requires an id and a hash of attributes"
222
+ end
223
+
224
+ doc = find(id)
225
+ doc.update_attributes(attrs)
226
+ doc
227
+ end
228
+
229
+ def update_multiple(docs)
230
+ unless docs.is_a?(Hash)
231
+ raise ArgumentError, "Updating multiple documents takes 1 argument and it must be hash"
232
+ end
233
+
234
+ instances = []
235
+ docs.each_pair { |id, attrs| instances << update(id, attrs) }
236
+ instances
237
+ end
238
+
239
+ def to_criteria(options={})
240
+ Query.new(self, options).criteria
241
+ end
242
+
243
+ def to_query(options={})
244
+ Query.new(self, options).to_a
245
+ end
246
+ end
247
+
248
+ module InstanceMethods
249
+ def save(options={})
250
+ options.assert_valid_keys(:validate, :safe)
251
+ options.reverse_merge!(:validate => true)
252
+ !options[:validate] || valid? ? create_or_update(options) : false
253
+ end
254
+
255
+ def save!(options={})
256
+ options.assert_valid_keys(:safe)
257
+ save(options) || raise(DocumentNotValid.new(self))
258
+ end
259
+
260
+ def destroy
261
+ delete
262
+ end
263
+
264
+ def delete
265
+ @_destroyed = true
266
+ self.class.delete(id) unless new?
267
+ end
268
+
269
+ def new?
270
+ @new
271
+ end
272
+
273
+ def destroyed?
274
+ @_destroyed == true
275
+ end
276
+
277
+ def reload
278
+ if attrs = collection.find_one({:_id => _id})
279
+ self.class.associations.each { |name, assoc| send(name).reset if respond_to?(name) }
280
+ self.attributes = attrs
281
+ self
282
+ else
283
+ raise DocumentNotFound, "Document match #{_id.inspect} does not exist in #{collection.name} collection"
284
+ end
285
+ end
286
+
287
+ # Used by embedded docs to find root easily without if/respond_to? stuff.
288
+ # Documents are always root documents.
289
+ def _root_document
290
+ self
291
+ end
292
+
293
+ private
294
+ def create_or_update(options={})
295
+ result = new? ? create(options) : update(options)
296
+ result != false
297
+ end
298
+
299
+ def create(options={})
300
+ save_to_collection(options)
301
+ end
302
+
303
+ def update(options={})
304
+ save_to_collection(options)
305
+ end
306
+
307
+ def save_to_collection(options={})
308
+ safe = options[:safe] || false
309
+ @new = false
310
+ collection.save(to_mongo, :safe => safe)
311
+ end
312
+ end
313
+ end # Document
314
+ end # MongoMapper