mongo_mapper_ign 0.7.4
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/.gitignore +10 -0
- data/LICENSE +20 -0
- data/README.rdoc +35 -0
- data/Rakefile +37 -0
- data/bin/mmconsole +60 -0
- data/lib/mongo_mapper.rb +116 -0
- data/lib/mongo_mapper/document.rb +313 -0
- data/lib/mongo_mapper/embedded_document.rb +70 -0
- data/lib/mongo_mapper/plugins.rb +35 -0
- data/lib/mongo_mapper/plugins/associations.rb +114 -0
- data/lib/mongo_mapper/plugins/associations/base.rb +123 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +30 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +25 -0
- data/lib/mongo_mapper/plugins/associations/collection.rb +21 -0
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +39 -0
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +144 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +129 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +31 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +23 -0
- data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +13 -0
- data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +41 -0
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +69 -0
- data/lib/mongo_mapper/plugins/associations/proxy.rb +124 -0
- data/lib/mongo_mapper/plugins/callbacks.rb +240 -0
- data/lib/mongo_mapper/plugins/clone.rb +13 -0
- data/lib/mongo_mapper/plugins/descendants.rb +16 -0
- data/lib/mongo_mapper/plugins/dirty.rb +119 -0
- data/lib/mongo_mapper/plugins/equality.rb +23 -0
- data/lib/mongo_mapper/plugins/identity_map.rb +122 -0
- data/lib/mongo_mapper/plugins/inspect.rb +14 -0
- data/lib/mongo_mapper/plugins/keys.rb +345 -0
- data/lib/mongo_mapper/plugins/logger.rb +17 -0
- data/lib/mongo_mapper/plugins/modifiers.rb +107 -0
- data/lib/mongo_mapper/plugins/pagination.rb +24 -0
- data/lib/mongo_mapper/plugins/pagination/proxy.rb +72 -0
- data/lib/mongo_mapper/plugins/persistence.rb +68 -0
- data/lib/mongo_mapper/plugins/protected.rb +45 -0
- data/lib/mongo_mapper/plugins/rails.rb +57 -0
- data/lib/mongo_mapper/plugins/serialization.rb +91 -0
- data/lib/mongo_mapper/plugins/serialization/array.rb +56 -0
- data/lib/mongo_mapper/plugins/serialization/xml_serializer.rb +240 -0
- data/lib/mongo_mapper/plugins/timestamps.rb +21 -0
- data/lib/mongo_mapper/plugins/userstamps.rb +14 -0
- data/lib/mongo_mapper/plugins/validations.rb +46 -0
- data/lib/mongo_mapper/query.rb +143 -0
- data/lib/mongo_mapper/support.rb +218 -0
- data/lib/mongo_mapper/support/descendant_appends.rb +46 -0
- data/lib/mongo_mapper/support/find.rb +77 -0
- data/lib/mongo_mapper/version.rb +3 -0
- data/mongo_mapper.gemspec +214 -0
- data/mongo_mapper_ign.gemspec +217 -0
- data/performance/read_write.rb +52 -0
- data/specs.watchr +51 -0
- data/test/NOTE_ON_TESTING +1 -0
- data/test/active_model_lint_test.rb +13 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +63 -0
- data/test/functional/associations/test_belongs_to_proxy.rb +101 -0
- data/test/functional/associations/test_in_array_proxy.rb +325 -0
- data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
- data/test/functional/associations/test_many_documents_proxy.rb +536 -0
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +176 -0
- data/test/functional/associations/test_many_embedded_proxy.rb +256 -0
- data/test/functional/associations/test_many_polymorphic_proxy.rb +302 -0
- data/test/functional/associations/test_one_embedded_proxy.rb +68 -0
- data/test/functional/associations/test_one_proxy.rb +196 -0
- data/test/functional/test_associations.rb +44 -0
- data/test/functional/test_binary.rb +27 -0
- data/test/functional/test_callbacks.rb +151 -0
- data/test/functional/test_dirty.rb +163 -0
- data/test/functional/test_document.rb +1219 -0
- data/test/functional/test_embedded_document.rb +210 -0
- data/test/functional/test_identity_map.rb +507 -0
- data/test/functional/test_indexing.rb +44 -0
- data/test/functional/test_logger.rb +20 -0
- data/test/functional/test_modifiers.rb +394 -0
- data/test/functional/test_pagination.rb +93 -0
- data/test/functional/test_protected.rb +163 -0
- data/test/functional/test_string_id_compatibility.rb +67 -0
- data/test/functional/test_timestamps.rb +64 -0
- data/test/functional/test_userstamps.rb +28 -0
- data/test/functional/test_validations.rb +342 -0
- data/test/models.rb +227 -0
- data/test/support/custom_matchers.rb +37 -0
- data/test/support/timing.rb +16 -0
- data/test/test_helper.rb +64 -0
- data/test/unit/associations/test_base.rb +212 -0
- data/test/unit/associations/test_proxy.rb +105 -0
- data/test/unit/serializers/test_json_serializer.rb +202 -0
- data/test/unit/test_descendant_appends.rb +71 -0
- data/test/unit/test_document.rb +225 -0
- data/test/unit/test_dynamic_finder.rb +123 -0
- data/test/unit/test_embedded_document.rb +657 -0
- data/test/unit/test_keys.rb +185 -0
- data/test/unit/test_mongo_mapper.rb +118 -0
- data/test/unit/test_pagination.rb +160 -0
- data/test/unit/test_plugins.rb +50 -0
- data/test/unit/test_query.rb +374 -0
- data/test/unit/test_rails.rb +181 -0
- data/test/unit/test_rails_compatibility.rb +52 -0
- data/test/unit/test_serialization.rb +51 -0
- data/test/unit/test_support.rb +382 -0
- data/test/unit/test_time_zones.rb +39 -0
- data/test/unit/test_validations.rb +544 -0
- metadata +327 -0
data/.gitignore
ADDED
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,35 @@
|
|
|
1
|
+
= MongoMapper
|
|
2
|
+
|
|
3
|
+
A Ruby Object Mapper for Mongo. mongo_mapper_ign gem includes support for xml serialization
|
|
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
|
+
== Config Rails environment.rb
|
|
20
|
+
|
|
21
|
+
config.gem "mongo_mapper_ign", :lib => "mongo_mapper"
|
|
22
|
+
|
|
23
|
+
== Problems or Questions?
|
|
24
|
+
|
|
25
|
+
Hit up the google group.
|
|
26
|
+
http://groups.google.com/group/mongomapper
|
|
27
|
+
|
|
28
|
+
To see if the problem you are having is a verified issue, you can see the MM pivotal tracker project:
|
|
29
|
+
http://www.pivotaltracker.com/projects/33576
|
|
30
|
+
|
|
31
|
+
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!
|
|
32
|
+
|
|
33
|
+
== Copyright
|
|
34
|
+
|
|
35
|
+
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_ign"
|
|
9
|
+
gem.summary = %Q{A Ruby Object Mapper for Mongo including support for XML serialization}
|
|
10
|
+
gem.email = "nunemaker@gmail.com,cpatni@ign.com,thnguyen@ign.com"
|
|
11
|
+
gem.homepage = "http://github.com/rubyorchard/mongomapper"
|
|
12
|
+
gem.authors = ["John Nunemaker", "Chandra Patni", "Thomas Nguyen"]
|
|
13
|
+
gem.version = MongoMapper::Version
|
|
14
|
+
|
|
15
|
+
gem.add_dependency('activesupport', '>= 2.3.4')
|
|
16
|
+
gem.add_dependency('mongo', '0.20.1')
|
|
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
|
+
|
data/lib/mongo_mapper.rb
ADDED
|
@@ -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', '0.20.1'
|
|
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,313 @@
|
|
|
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
|
+
|
|
31
|
+
extend Plugins::Validations::DocumentMacros
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
super
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
module ClassMethods
|
|
38
|
+
def inherited(subclass)
|
|
39
|
+
subclass.set_collection_name(collection_name)
|
|
40
|
+
super
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def ensure_index(spec, options={})
|
|
44
|
+
collection.create_index(spec, options)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def find(*args)
|
|
48
|
+
assert_no_first_last_or_all(args)
|
|
49
|
+
options = args.extract_options!
|
|
50
|
+
return nil if args.size == 0
|
|
51
|
+
|
|
52
|
+
if args.first.is_a?(Array) || args.size > 1
|
|
53
|
+
find_some(args, options)
|
|
54
|
+
else
|
|
55
|
+
find_one(options.merge({:_id => args[0]}))
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def find!(*args)
|
|
60
|
+
assert_no_first_last_or_all(args)
|
|
61
|
+
options = args.extract_options!
|
|
62
|
+
raise DocumentNotFound, "Couldn't find without an ID" if args.size == 0
|
|
63
|
+
|
|
64
|
+
if args.first.is_a?(Array) || args.size > 1
|
|
65
|
+
find_some!(args, options)
|
|
66
|
+
else
|
|
67
|
+
find_one(options.merge({:_id => args[0]})) || raise(DocumentNotFound, "Document match #{options.inspect} does not exist in #{collection.name} collection")
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def find_each(options={})
|
|
72
|
+
criteria, options = to_query(options)
|
|
73
|
+
collection.find(criteria, options).each do |doc|
|
|
74
|
+
yield load(doc)
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def find_by_id(id)
|
|
79
|
+
find(id)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def first_or_create(args)
|
|
83
|
+
first(args) || create(args.reject { |key, value| !key?(key) })
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def first_or_new(args)
|
|
87
|
+
first(args) || new(args.reject { |key, value| !key?(key) })
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def first(options={})
|
|
91
|
+
find_one(options)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def last(options={})
|
|
95
|
+
raise ':order option must be provided when using last' if options[:order].blank?
|
|
96
|
+
find_one(options.merge(:order => invert_order_clause(options[:order])))
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def all(options={})
|
|
100
|
+
find_many(options)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def count(options={})
|
|
104
|
+
collection.find(to_criteria(options)).count
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def exists?(options={})
|
|
108
|
+
!count(options).zero?
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def create(*docs)
|
|
112
|
+
initialize_each(*docs) { |doc| doc.save }
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def create!(*docs)
|
|
116
|
+
initialize_each(*docs) { |doc| doc.save! }
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def update(*args)
|
|
120
|
+
if args.length == 1
|
|
121
|
+
update_multiple(args[0])
|
|
122
|
+
else
|
|
123
|
+
id, attributes = args
|
|
124
|
+
update_single(id, attributes)
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def delete(*ids)
|
|
129
|
+
collection.remove(to_criteria(:_id => ids.flatten))
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def delete_all(options={})
|
|
133
|
+
collection.remove(to_criteria(options))
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def destroy(*ids)
|
|
137
|
+
find_some!(ids.flatten).each(&:destroy)
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def destroy_all(options={})
|
|
141
|
+
find_each(options) { |document| document.destroy }
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def embeddable?
|
|
145
|
+
false
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def single_collection_inherited?
|
|
149
|
+
keys.key?(:_type) && single_collection_inherited_superclass?
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def single_collection_inherited_superclass?
|
|
153
|
+
superclass.respond_to?(:keys) && superclass.keys.key?(:_type)
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
private
|
|
157
|
+
def initialize_each(*docs)
|
|
158
|
+
instances = []
|
|
159
|
+
docs = [{}] if docs.blank?
|
|
160
|
+
docs.flatten.each do |attrs|
|
|
161
|
+
doc = new(attrs)
|
|
162
|
+
yield(doc)
|
|
163
|
+
instances << doc
|
|
164
|
+
end
|
|
165
|
+
instances.size == 1 ? instances[0] : instances
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def assert_no_first_last_or_all(args)
|
|
169
|
+
if args[0] == :first || args[0] == :last || args[0] == :all
|
|
170
|
+
raise ArgumentError, "#{self}.find(:#{args}) is no longer supported, use #{self}.#{args} instead."
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def find_some(ids, options={})
|
|
175
|
+
ids = ids.flatten.compact.uniq
|
|
176
|
+
find_many(options.merge(:_id => ids)).compact
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
def find_some!(ids, options={})
|
|
180
|
+
ids = ids.flatten.compact.uniq
|
|
181
|
+
documents = find_some(ids, options)
|
|
182
|
+
|
|
183
|
+
if ids.size == documents.size
|
|
184
|
+
documents
|
|
185
|
+
else
|
|
186
|
+
raise DocumentNotFound, "Couldn't find all of the ids (#{ids.to_sentence}). Found #{documents.size}, but was expecting #{ids.size}"
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
# All query methods that load documents pass through find_one or find_many
|
|
191
|
+
def find_one(options={})
|
|
192
|
+
criteria, options = to_query(options)
|
|
193
|
+
if doc = collection.find_one(criteria, options)
|
|
194
|
+
load(doc)
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
# All query methods that load documents pass through find_one or find_many
|
|
199
|
+
def find_many(options)
|
|
200
|
+
criteria, options = to_query(options)
|
|
201
|
+
collection.find(criteria, options).to_a.map do |doc|
|
|
202
|
+
load(doc)
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
def invert_order_clause(order)
|
|
207
|
+
order.split(',').map do |order_segment|
|
|
208
|
+
if order_segment =~ /\sasc/i
|
|
209
|
+
order_segment.sub /\sasc/i, ' desc'
|
|
210
|
+
elsif order_segment =~ /\sdesc/i
|
|
211
|
+
order_segment.sub /\sdesc/i, ' asc'
|
|
212
|
+
else
|
|
213
|
+
"#{order_segment.strip} desc"
|
|
214
|
+
end
|
|
215
|
+
end.join(',')
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
def update_single(id, attrs)
|
|
219
|
+
if id.blank? || attrs.blank? || !attrs.is_a?(Hash)
|
|
220
|
+
raise ArgumentError, "Updating a single document requires an id and a hash of attributes"
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
doc = find(id)
|
|
224
|
+
doc.update_attributes(attrs)
|
|
225
|
+
doc
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
def update_multiple(docs)
|
|
229
|
+
unless docs.is_a?(Hash)
|
|
230
|
+
raise ArgumentError, "Updating multiple documents takes 1 argument and it must be hash"
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
instances = []
|
|
234
|
+
docs.each_pair { |id, attrs| instances << update(id, attrs) }
|
|
235
|
+
instances
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
def to_criteria(options={})
|
|
239
|
+
Query.new(self, options).criteria
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
def to_query(options={})
|
|
243
|
+
Query.new(self, options).to_a
|
|
244
|
+
end
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
module InstanceMethods
|
|
248
|
+
def save(options={})
|
|
249
|
+
options.assert_valid_keys(:validate, :safe)
|
|
250
|
+
options.reverse_merge!(:validate => true)
|
|
251
|
+
!options[:validate] || valid? ? create_or_update(options) : false
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
def save!(options={})
|
|
255
|
+
options.assert_valid_keys(:safe)
|
|
256
|
+
save(options) || raise(DocumentNotValid.new(self))
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
def destroy
|
|
260
|
+
delete
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
def delete
|
|
264
|
+
@_destroyed = true
|
|
265
|
+
self.class.delete(id) unless new?
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
def new?
|
|
269
|
+
@new
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
def destroyed?
|
|
273
|
+
@_destroyed == true
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
def reload
|
|
277
|
+
if attrs = collection.find_one({:_id => _id})
|
|
278
|
+
self.class.associations.each { |name, assoc| send(name).reset if respond_to?(name) }
|
|
279
|
+
self.attributes = attrs
|
|
280
|
+
self
|
|
281
|
+
else
|
|
282
|
+
raise DocumentNotFound, "Document match #{_id.inspect} does not exist in #{collection.name} collection"
|
|
283
|
+
end
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
# Used by embedded docs to find root easily without if/respond_to? stuff.
|
|
287
|
+
# Documents are always root documents.
|
|
288
|
+
def _root_document
|
|
289
|
+
self
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
private
|
|
293
|
+
def create_or_update(options={})
|
|
294
|
+
result = new? ? create(options) : update(options)
|
|
295
|
+
result != false
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
def create(options={})
|
|
299
|
+
save_to_collection(options)
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
def update(options={})
|
|
303
|
+
save_to_collection(options)
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
def save_to_collection(options={})
|
|
307
|
+
safe = options[:safe] || false
|
|
308
|
+
@new = false
|
|
309
|
+
collection.save(to_mongo, :safe => safe)
|
|
310
|
+
end
|
|
311
|
+
end
|
|
312
|
+
end # Document
|
|
313
|
+
end # MongoMapper
|