mongoid-locomotive 2.0.0.beta9
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT_LICENSE +20 -0
- data/README.rdoc +47 -0
- data/lib/mongoid.rb +141 -0
- data/lib/mongoid/associations.rb +306 -0
- data/lib/mongoid/associations/embedded_in.rb +74 -0
- data/lib/mongoid/associations/embeds_many.rb +280 -0
- data/lib/mongoid/associations/embeds_one.rb +97 -0
- data/lib/mongoid/associations/foreign_key.rb +35 -0
- data/lib/mongoid/associations/meta_data.rb +38 -0
- data/lib/mongoid/associations/options.rb +62 -0
- data/lib/mongoid/associations/proxy.rb +33 -0
- data/lib/mongoid/associations/referenced_in.rb +59 -0
- data/lib/mongoid/associations/references_many.rb +245 -0
- data/lib/mongoid/associations/references_many_as_array.rb +78 -0
- data/lib/mongoid/associations/references_one.rb +99 -0
- data/lib/mongoid/atomicity.rb +55 -0
- data/lib/mongoid/attributes.rb +242 -0
- data/lib/mongoid/callbacks.rb +21 -0
- data/lib/mongoid/collection.rb +120 -0
- data/lib/mongoid/collections.rb +71 -0
- data/lib/mongoid/collections/cyclic_iterator.rb +34 -0
- data/lib/mongoid/collections/master.rb +29 -0
- data/lib/mongoid/collections/operations.rb +41 -0
- data/lib/mongoid/collections/slaves.rb +45 -0
- data/lib/mongoid/components.rb +34 -0
- data/lib/mongoid/config.rb +263 -0
- data/lib/mongoid/contexts.rb +24 -0
- data/lib/mongoid/contexts/enumerable.rb +156 -0
- data/lib/mongoid/contexts/ids.rb +25 -0
- data/lib/mongoid/contexts/mongo.rb +285 -0
- data/lib/mongoid/contexts/paging.rb +50 -0
- data/lib/mongoid/criteria.rb +248 -0
- data/lib/mongoid/criterion/complex.rb +21 -0
- data/lib/mongoid/criterion/exclusion.rb +65 -0
- data/lib/mongoid/criterion/inclusion.rb +110 -0
- data/lib/mongoid/criterion/optional.rb +189 -0
- data/lib/mongoid/cursor.rb +81 -0
- data/lib/mongoid/deprecation.rb +21 -0
- data/lib/mongoid/dirty.rb +252 -0
- data/lib/mongoid/document.rb +210 -0
- data/lib/mongoid/errors.rb +131 -0
- data/lib/mongoid/extensions.rb +115 -0
- data/lib/mongoid/extensions/array/accessors.rb +17 -0
- data/lib/mongoid/extensions/array/assimilation.rb +26 -0
- data/lib/mongoid/extensions/array/conversions.rb +23 -0
- data/lib/mongoid/extensions/array/parentization.rb +13 -0
- data/lib/mongoid/extensions/big_decimal/conversions.rb +19 -0
- data/lib/mongoid/extensions/binary/conversions.rb +17 -0
- data/lib/mongoid/extensions/boolean/conversions.rb +27 -0
- data/lib/mongoid/extensions/date/conversions.rb +24 -0
- data/lib/mongoid/extensions/datetime/conversions.rb +12 -0
- data/lib/mongoid/extensions/false_class/equality.rb +13 -0
- data/lib/mongoid/extensions/float/conversions.rb +20 -0
- data/lib/mongoid/extensions/hash/accessors.rb +42 -0
- data/lib/mongoid/extensions/hash/assimilation.rb +40 -0
- data/lib/mongoid/extensions/hash/conversions.rb +42 -0
- data/lib/mongoid/extensions/hash/criteria_helpers.rb +20 -0
- data/lib/mongoid/extensions/hash/scoping.rb +12 -0
- data/lib/mongoid/extensions/integer/conversions.rb +20 -0
- data/lib/mongoid/extensions/nil/assimilation.rb +17 -0
- data/lib/mongoid/extensions/object/conversions.rb +21 -0
- data/lib/mongoid/extensions/objectid/conversions.rb +15 -0
- data/lib/mongoid/extensions/proc/scoping.rb +12 -0
- data/lib/mongoid/extensions/set/conversions.rb +20 -0
- data/lib/mongoid/extensions/string/conversions.rb +15 -0
- data/lib/mongoid/extensions/string/inflections.rb +97 -0
- data/lib/mongoid/extensions/symbol/inflections.rb +40 -0
- data/lib/mongoid/extensions/time_conversions.rb +35 -0
- data/lib/mongoid/extensions/true_class/equality.rb +13 -0
- data/lib/mongoid/extras.rb +61 -0
- data/lib/mongoid/factory.rb +20 -0
- data/lib/mongoid/field.rb +83 -0
- data/lib/mongoid/fields.rb +62 -0
- data/lib/mongoid/finders.rb +145 -0
- data/lib/mongoid/hierarchy.rb +74 -0
- data/lib/mongoid/identity.rb +47 -0
- data/lib/mongoid/indexes.rb +27 -0
- data/lib/mongoid/javascript.rb +21 -0
- data/lib/mongoid/javascript/functions.yml +37 -0
- data/lib/mongoid/logger.rb +19 -0
- data/lib/mongoid/matchers.rb +35 -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/lib/mongoid/memoization.rb +33 -0
- data/lib/mongoid/named_scope.rb +37 -0
- data/lib/mongoid/paranoia.rb +106 -0
- data/lib/mongoid/paths.rb +61 -0
- data/lib/mongoid/persistence.rb +216 -0
- data/lib/mongoid/persistence/command.rb +39 -0
- data/lib/mongoid/persistence/insert.rb +48 -0
- data/lib/mongoid/persistence/insert_embedded.rb +44 -0
- data/lib/mongoid/persistence/remove.rb +39 -0
- data/lib/mongoid/persistence/remove_all.rb +38 -0
- data/lib/mongoid/persistence/remove_embedded.rb +50 -0
- data/lib/mongoid/persistence/update.rb +71 -0
- data/lib/mongoid/railtie.rb +67 -0
- data/lib/mongoid/railties/database.rake +60 -0
- data/lib/mongoid/scope.rb +75 -0
- data/lib/mongoid/state.rb +32 -0
- data/lib/mongoid/timestamps.rb +27 -0
- data/lib/mongoid/validations.rb +51 -0
- data/lib/mongoid/validations/associated.rb +32 -0
- data/lib/mongoid/validations/locale/en.yml +5 -0
- data/lib/mongoid/validations/uniqueness.rb +56 -0
- data/lib/mongoid/version.rb +4 -0
- data/lib/mongoid/versioning.rb +26 -0
- data/lib/rails/generators/mongoid/config/config_generator.rb +25 -0
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +24 -0
- data/lib/rails/generators/mongoid/model/model_generator.rb +24 -0
- data/lib/rails/generators/mongoid/model/templates/model.rb +15 -0
- data/lib/rails/generators/mongoid_generator.rb +61 -0
- data/spec/integration/mongoid/association_attributes_spec.rb +71 -0
- data/spec/integration/mongoid/associations_spec.rb +768 -0
- data/spec/integration/mongoid/attributes_spec.rb +59 -0
- data/spec/integration/mongoid/callback_spec.rb +33 -0
- data/spec/integration/mongoid/contexts/enumerable_spec.rb +33 -0
- data/spec/integration/mongoid/criteria_spec.rb +281 -0
- data/spec/integration/mongoid/dirty_spec.rb +85 -0
- data/spec/integration/mongoid/document_spec.rb +741 -0
- data/spec/integration/mongoid/extensions_spec.rb +22 -0
- data/spec/integration/mongoid/finders_spec.rb +119 -0
- data/spec/integration/mongoid/inheritance_spec.rb +171 -0
- data/spec/integration/mongoid/named_scope_spec.rb +58 -0
- data/spec/integration/mongoid/paranoia_spec.rb +44 -0
- data/spec/integration/mongoid/persistence/update_spec.rb +46 -0
- data/spec/integration/mongoid/persistence_spec.rb +311 -0
- data/spec/integration/mongoid/validations/uniqueness_spec.rb +206 -0
- data/spec/models/account.rb +5 -0
- data/spec/models/address.rb +40 -0
- data/spec/models/agent.rb +7 -0
- data/spec/models/animal.rb +15 -0
- data/spec/models/answer.rb +4 -0
- data/spec/models/callbacks.rb +47 -0
- data/spec/models/category.rb +13 -0
- data/spec/models/comment.rb +10 -0
- data/spec/models/country_code.rb +6 -0
- data/spec/models/employer.rb +5 -0
- data/spec/models/favorite.rb +8 -0
- data/spec/models/game.rb +9 -0
- data/spec/models/inheritance.rb +72 -0
- data/spec/models/location.rb +5 -0
- data/spec/models/login.rb +6 -0
- data/spec/models/mixed_drink.rb +4 -0
- data/spec/models/name.rb +13 -0
- data/spec/models/namespacing.rb +11 -0
- data/spec/models/paranoid_post.rb +18 -0
- data/spec/models/parents.rb +32 -0
- data/spec/models/patient.rb +15 -0
- data/spec/models/person.rb +106 -0
- data/spec/models/pet.rb +7 -0
- data/spec/models/pet_owner.rb +6 -0
- data/spec/models/phone.rb +7 -0
- data/spec/models/post.rb +25 -0
- data/spec/models/preference.rb +7 -0
- data/spec/models/question.rb +8 -0
- data/spec/models/survey.rb +6 -0
- data/spec/models/translation.rb +5 -0
- data/spec/models/user.rb +6 -0
- data/spec/models/user_accout.rb +5 -0
- data/spec/models/vet_visit.rb +5 -0
- data/spec/models/video.rb +5 -0
- data/spec/spec_helper.rb +33 -0
- data/spec/unit/mongoid/associations/embedded_in_spec.rb +193 -0
- data/spec/unit/mongoid/associations/embeds_many_spec.rb +626 -0
- data/spec/unit/mongoid/associations/embeds_one_spec.rb +287 -0
- data/spec/unit/mongoid/associations/foreign_key_spec.rb +90 -0
- data/spec/unit/mongoid/associations/meta_data_spec.rb +110 -0
- data/spec/unit/mongoid/associations/options_spec.rb +215 -0
- data/spec/unit/mongoid/associations/referenced_in_spec.rb +145 -0
- data/spec/unit/mongoid/associations/references_many_as_array_spec.rb +424 -0
- data/spec/unit/mongoid/associations/references_many_spec.rb +502 -0
- data/spec/unit/mongoid/associations/references_one_spec.rb +204 -0
- data/spec/unit/mongoid/associations_spec.rb +688 -0
- data/spec/unit/mongoid/atomicity_spec.rb +164 -0
- data/spec/unit/mongoid/attributes_spec.rb +646 -0
- data/spec/unit/mongoid/callbacks_spec.rb +85 -0
- data/spec/unit/mongoid/collection_spec.rb +187 -0
- data/spec/unit/mongoid/collections/cyclic_iterator_spec.rb +75 -0
- data/spec/unit/mongoid/collections/master_spec.rb +41 -0
- data/spec/unit/mongoid/collections/slaves_spec.rb +81 -0
- data/spec/unit/mongoid/collections_spec.rb +98 -0
- data/spec/unit/mongoid/config_spec.rb +298 -0
- data/spec/unit/mongoid/contexts/enumerable_spec.rb +447 -0
- data/spec/unit/mongoid/contexts/mongo_spec.rb +703 -0
- data/spec/unit/mongoid/contexts_spec.rb +25 -0
- data/spec/unit/mongoid/criteria_spec.rb +873 -0
- data/spec/unit/mongoid/criterion/complex_spec.rb +17 -0
- data/spec/unit/mongoid/criterion/exclusion_spec.rb +121 -0
- data/spec/unit/mongoid/criterion/inclusion_spec.rb +274 -0
- data/spec/unit/mongoid/criterion/optional_spec.rb +483 -0
- data/spec/unit/mongoid/cursor_spec.rb +80 -0
- data/spec/unit/mongoid/deprecation_spec.rb +24 -0
- data/spec/unit/mongoid/dirty_spec.rb +430 -0
- data/spec/unit/mongoid/document_spec.rb +623 -0
- data/spec/unit/mongoid/errors_spec.rb +154 -0
- data/spec/unit/mongoid/extensions/array/accessors_spec.rb +50 -0
- data/spec/unit/mongoid/extensions/array/assimilation_spec.rb +24 -0
- data/spec/unit/mongoid/extensions/array/conversions_spec.rb +52 -0
- data/spec/unit/mongoid/extensions/array/parentization_spec.rb +20 -0
- data/spec/unit/mongoid/extensions/big_decimal/conversions_spec.rb +36 -0
- data/spec/unit/mongoid/extensions/binary/conversions_spec.rb +22 -0
- data/spec/unit/mongoid/extensions/boolean/conversions_spec.rb +49 -0
- data/spec/unit/mongoid/extensions/date/conversions_spec.rb +145 -0
- data/spec/unit/mongoid/extensions/datetime/conversions_spec.rb +14 -0
- data/spec/unit/mongoid/extensions/false_class/equality_spec.rb +35 -0
- data/spec/unit/mongoid/extensions/float/conversions_spec.rb +61 -0
- data/spec/unit/mongoid/extensions/hash/accessors_spec.rb +184 -0
- data/spec/unit/mongoid/extensions/hash/assimilation_spec.rb +59 -0
- data/spec/unit/mongoid/extensions/hash/conversions_spec.rb +35 -0
- data/spec/unit/mongoid/extensions/hash/criteria_helpers_spec.rb +17 -0
- data/spec/unit/mongoid/extensions/hash/scoping_spec.rb +14 -0
- data/spec/unit/mongoid/extensions/integer/conversions_spec.rb +61 -0
- data/spec/unit/mongoid/extensions/nil/assimilation_spec.rb +29 -0
- data/spec/unit/mongoid/extensions/object/conversions_spec.rb +44 -0
- data/spec/unit/mongoid/extensions/objectid/conversions_spec.rb +22 -0
- data/spec/unit/mongoid/extensions/proc/scoping_spec.rb +34 -0
- data/spec/unit/mongoid/extensions/set/conversions_spec.rb +21 -0
- data/spec/unit/mongoid/extensions/string/conversions_spec.rb +28 -0
- data/spec/unit/mongoid/extensions/string/inflections_spec.rb +208 -0
- data/spec/unit/mongoid/extensions/symbol/inflections_spec.rb +107 -0
- data/spec/unit/mongoid/extensions/time_conversions_spec.rb +186 -0
- data/spec/unit/mongoid/extensions/true_class/equality_spec.rb +35 -0
- data/spec/unit/mongoid/extras_spec.rb +102 -0
- data/spec/unit/mongoid/factory_spec.rb +31 -0
- data/spec/unit/mongoid/field_spec.rb +169 -0
- data/spec/unit/mongoid/fields_spec.rb +181 -0
- data/spec/unit/mongoid/finders_spec.rb +439 -0
- data/spec/unit/mongoid/hierarchy_spec.rb +68 -0
- data/spec/unit/mongoid/identity_spec.rb +109 -0
- data/spec/unit/mongoid/indexes_spec.rb +99 -0
- data/spec/unit/mongoid/javascript_spec.rb +48 -0
- data/spec/unit/mongoid/logger_spec.rb +38 -0
- data/spec/unit/mongoid/matchers/all_spec.rb +27 -0
- data/spec/unit/mongoid/matchers/default_spec.rb +27 -0
- data/spec/unit/mongoid/matchers/exists_spec.rb +56 -0
- data/spec/unit/mongoid/matchers/gt_spec.rb +39 -0
- data/spec/unit/mongoid/matchers/gte_spec.rb +49 -0
- data/spec/unit/mongoid/matchers/in_spec.rb +27 -0
- data/spec/unit/mongoid/matchers/lt_spec.rb +39 -0
- data/spec/unit/mongoid/matchers/lte_spec.rb +49 -0
- data/spec/unit/mongoid/matchers/ne_spec.rb +27 -0
- data/spec/unit/mongoid/matchers/nin_spec.rb +27 -0
- data/spec/unit/mongoid/matchers/size_spec.rb +27 -0
- data/spec/unit/mongoid/matchers_spec.rb +329 -0
- data/spec/unit/mongoid/memoization_spec.rb +75 -0
- data/spec/unit/mongoid/named_scope_spec.rb +123 -0
- data/spec/unit/mongoid/paranoia_spec.rb +108 -0
- data/spec/unit/mongoid/paths_spec.rb +272 -0
- data/spec/unit/mongoid/persistence/insert_embedded_spec.rb +154 -0
- data/spec/unit/mongoid/persistence/insert_spec.rb +144 -0
- data/spec/unit/mongoid/persistence/remove_all_spec.rb +82 -0
- data/spec/unit/mongoid/persistence/remove_embedded_spec.rb +152 -0
- data/spec/unit/mongoid/persistence/remove_spec.rb +89 -0
- data/spec/unit/mongoid/persistence/update_spec.rb +177 -0
- data/spec/unit/mongoid/persistence_spec.rb +452 -0
- data/spec/unit/mongoid/scope_spec.rb +240 -0
- data/spec/unit/mongoid/serialization_spec.rb +43 -0
- data/spec/unit/mongoid/state_spec.rb +94 -0
- data/spec/unit/mongoid/timestamps_spec.rb +30 -0
- data/spec/unit/mongoid/validations/associated_spec.rb +103 -0
- data/spec/unit/mongoid/validations/uniqueness_spec.rb +201 -0
- data/spec/unit/mongoid/validations_spec.rb +43 -0
- data/spec/unit/mongoid/versioning_spec.rb +41 -0
- data/spec/unit/mongoid_spec.rb +46 -0
- metadata +433 -0
data/MIT_LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Durran Jordan
|
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,47 @@
|
|
1
|
+
= Overview
|
2
|
+
|
3
|
+
== About Mongoid
|
4
|
+
|
5
|
+
Mongoid is an ODM (Object-Document-Mapper) framework for MongoDB in Ruby.
|
6
|
+
|
7
|
+
== Project Tracking
|
8
|
+
|
9
|
+
* {Mongoid Google Group}[http://groups.google.com/group/mongoid]
|
10
|
+
* {Mongoid Website and Documentation}[http://mongoid.org]
|
11
|
+
|
12
|
+
== Compatibility
|
13
|
+
|
14
|
+
Mongoid is developed against Ruby 1.8.7, 1.9.1, 1.9.2
|
15
|
+
|
16
|
+
= Documentation
|
17
|
+
|
18
|
+
Please see the new Mongoid website for up-to-date documentation:
|
19
|
+
{mongoid.org}[http://mongoid.org]
|
20
|
+
|
21
|
+
= License
|
22
|
+
|
23
|
+
Copyright (c) 2009, 2010 Durran Jordan
|
24
|
+
|
25
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
26
|
+
a copy of this software and associated documentation files (the
|
27
|
+
"Software"), to deal in the Software without restriction, including
|
28
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
29
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
30
|
+
permit persons to whom the Software is furnished to do so, subject to
|
31
|
+
the following conditions:
|
32
|
+
|
33
|
+
The above copyright notice and this permission notice shall be
|
34
|
+
included in all copies or substantial portions of the Software.
|
35
|
+
|
36
|
+
|
37
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
38
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
39
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
40
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
41
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
42
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
43
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
44
|
+
|
45
|
+
= Credits
|
46
|
+
|
47
|
+
Durran Jordan: durran at gmail dot com
|
data/lib/mongoid.rb
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# Copyright (c) 2009 Durran Jordan
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
# a copy of this software and associated documentation files (the
|
6
|
+
# "Software"), to deal in the Software without restriction, including
|
7
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
# the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be
|
13
|
+
# included in all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
|
23
|
+
require "delegate"
|
24
|
+
require "singleton"
|
25
|
+
require "time"
|
26
|
+
require "ostruct"
|
27
|
+
require "active_support/core_ext"
|
28
|
+
require 'active_support/json'
|
29
|
+
require "active_support/inflector"
|
30
|
+
require "active_support/time_with_zone"
|
31
|
+
require "active_model"
|
32
|
+
require "active_model/callbacks"
|
33
|
+
require "active_model/conversion"
|
34
|
+
require "active_model/deprecated_error_methods"
|
35
|
+
require "active_model/errors"
|
36
|
+
require "active_model/naming"
|
37
|
+
require "active_model/serialization"
|
38
|
+
require "active_model/translation"
|
39
|
+
require "active_model/validator"
|
40
|
+
require "active_model/validations"
|
41
|
+
require "will_paginate/collection"
|
42
|
+
require "mongo"
|
43
|
+
require "mongoid/extensions"
|
44
|
+
require "mongoid/associations"
|
45
|
+
require "mongoid/atomicity"
|
46
|
+
require "mongoid/attributes"
|
47
|
+
require "mongoid/callbacks"
|
48
|
+
require "mongoid/collection"
|
49
|
+
require "mongoid/collections"
|
50
|
+
require "mongoid/config"
|
51
|
+
require "mongoid/contexts"
|
52
|
+
require "mongoid/criteria"
|
53
|
+
require "mongoid/cursor"
|
54
|
+
require "mongoid/deprecation"
|
55
|
+
require "mongoid/dirty"
|
56
|
+
require "mongoid/extras"
|
57
|
+
require "mongoid/errors"
|
58
|
+
require "mongoid/factory"
|
59
|
+
require "mongoid/field"
|
60
|
+
require "mongoid/fields"
|
61
|
+
require "mongoid/finders"
|
62
|
+
require "mongoid/hierarchy"
|
63
|
+
require "mongoid/identity"
|
64
|
+
require "mongoid/indexes"
|
65
|
+
require "mongoid/javascript"
|
66
|
+
require "mongoid/logger"
|
67
|
+
require "mongoid/matchers"
|
68
|
+
require "mongoid/memoization"
|
69
|
+
require "mongoid/named_scope"
|
70
|
+
require "mongoid/paths"
|
71
|
+
require "mongoid/persistence"
|
72
|
+
require "mongoid/scope"
|
73
|
+
require "mongoid/state"
|
74
|
+
require "mongoid/timestamps"
|
75
|
+
require "mongoid/validations"
|
76
|
+
require "mongoid/versioning"
|
77
|
+
require "mongoid/components"
|
78
|
+
require "mongoid/paranoia"
|
79
|
+
require "mongoid/document"
|
80
|
+
|
81
|
+
# add railtie
|
82
|
+
if defined?(Rails)
|
83
|
+
require "mongoid/railtie"
|
84
|
+
end
|
85
|
+
|
86
|
+
module Mongoid #:nodoc
|
87
|
+
|
88
|
+
MONGODB_VERSION = "1.4.0"
|
89
|
+
|
90
|
+
class << self
|
91
|
+
|
92
|
+
# Sets the Mongoid configuration options. Best used by passing a block.
|
93
|
+
#
|
94
|
+
# Example:
|
95
|
+
#
|
96
|
+
# Mongoid.configure do |config|
|
97
|
+
# name = "mongoid_test"
|
98
|
+
# host = "localhost"
|
99
|
+
# config.allow_dynamic_fields = false
|
100
|
+
# config.master = Mongo::Connection.new.db(name)
|
101
|
+
# config.slaves = [
|
102
|
+
# Mongo::Connection.new(host, 27018, :slave_ok => true).db(name),
|
103
|
+
# Mongo::Connection.new(host, 27019, :slave_ok => true).db(name)
|
104
|
+
# ]
|
105
|
+
# end
|
106
|
+
#
|
107
|
+
# Returns:
|
108
|
+
#
|
109
|
+
# The Mongoid +Config+ singleton instance.
|
110
|
+
def configure
|
111
|
+
config = Mongoid::Config.instance
|
112
|
+
block_given? ? yield(config) : config
|
113
|
+
end
|
114
|
+
|
115
|
+
# Easy convenience method for generating an alert from the
|
116
|
+
# deprecation module.
|
117
|
+
#
|
118
|
+
# Example:
|
119
|
+
#
|
120
|
+
# <tt>Mongoid.deprecate("Method no longer used")</tt>
|
121
|
+
def deprecate(message)
|
122
|
+
Mongoid::Deprecation.instance.alert(message)
|
123
|
+
end
|
124
|
+
|
125
|
+
alias :config :configure
|
126
|
+
end
|
127
|
+
|
128
|
+
# Take all the public instance methods from the Config singleton and allow
|
129
|
+
# them to be accessed through the Mongoid module directly.
|
130
|
+
#
|
131
|
+
# Example:
|
132
|
+
#
|
133
|
+
# <tt>Mongoid.database = Mongo::Connection.new.db("test")</tt>
|
134
|
+
Mongoid::Config.public_instance_methods(false).each do |name|
|
135
|
+
(class << self; self; end).class_eval <<-EOT
|
136
|
+
def #{name}(*args)
|
137
|
+
configure.send("#{name}", *args)
|
138
|
+
end
|
139
|
+
EOT
|
140
|
+
end
|
141
|
+
end
|
@@ -0,0 +1,306 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "mongoid/associations/proxy"
|
3
|
+
require "mongoid/associations/embedded_in"
|
4
|
+
require "mongoid/associations/embeds_many"
|
5
|
+
require "mongoid/associations/embeds_one"
|
6
|
+
require "mongoid/associations/foreign_key"
|
7
|
+
require "mongoid/associations/references_many"
|
8
|
+
require "mongoid/associations/references_many_as_array"
|
9
|
+
require "mongoid/associations/references_one"
|
10
|
+
require "mongoid/associations/referenced_in"
|
11
|
+
require "mongoid/associations/options"
|
12
|
+
require "mongoid/associations/meta_data"
|
13
|
+
|
14
|
+
module Mongoid # :nodoc:
|
15
|
+
module Associations #:nodoc:
|
16
|
+
extend ActiveSupport::Concern
|
17
|
+
included do
|
18
|
+
include ForeignKey
|
19
|
+
|
20
|
+
cattr_accessor :embedded
|
21
|
+
self.embedded = false
|
22
|
+
|
23
|
+
class_inheritable_accessor :associations
|
24
|
+
self.associations = {}
|
25
|
+
|
26
|
+
delegate :embedded, :embedded?, :to => "self.class"
|
27
|
+
end
|
28
|
+
|
29
|
+
# Returns the associations for the +Document+.
|
30
|
+
def associations
|
31
|
+
self.class.associations
|
32
|
+
end
|
33
|
+
|
34
|
+
# are we in an embeds_many?
|
35
|
+
def embedded_many?
|
36
|
+
embedded? && _parent.associations[association_name].association == EmbedsMany
|
37
|
+
end
|
38
|
+
|
39
|
+
# are we in an embeds_one?
|
40
|
+
def embedded_one?
|
41
|
+
embedded? && !embedded_many?
|
42
|
+
end
|
43
|
+
|
44
|
+
# Update the one-to-one relational association for the name.
|
45
|
+
def update_association(name)
|
46
|
+
association = send(name)
|
47
|
+
association.save if new_record? && !association.nil?
|
48
|
+
end
|
49
|
+
|
50
|
+
# Updates all the one-to-many relational associations for the name.
|
51
|
+
def update_associations(name)
|
52
|
+
send(name).each { |doc| doc.save } if new_record?
|
53
|
+
end
|
54
|
+
|
55
|
+
def update_foreign_keys
|
56
|
+
associations.each do |name, association|
|
57
|
+
next unless association.macro == :referenced_in
|
58
|
+
foreign_key = association.options.foreign_key
|
59
|
+
if send(foreign_key).nil?
|
60
|
+
target = send(name)
|
61
|
+
send("#{foreign_key}=", target ? target.id : nil)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
module ClassMethods
|
67
|
+
# Gets whether or not the document is embedded.
|
68
|
+
#
|
69
|
+
# Example:
|
70
|
+
#
|
71
|
+
# <tt>Person.embedded?</tt>
|
72
|
+
#
|
73
|
+
# Returns:
|
74
|
+
#
|
75
|
+
# <tt>true</tt> if embedded, <tt>false</tt> if not.
|
76
|
+
def embedded?
|
77
|
+
!!self.embedded
|
78
|
+
end
|
79
|
+
|
80
|
+
# Adds the association back to the parent document. This macro is
|
81
|
+
# necessary to set the references from the child back to the parent
|
82
|
+
# document. If a child does not define this association calling
|
83
|
+
# persistence methods on the child object will cause a save to fail.
|
84
|
+
#
|
85
|
+
# Options:
|
86
|
+
#
|
87
|
+
# name: A +Symbol+ that matches the name of the parent class.
|
88
|
+
#
|
89
|
+
# Example:
|
90
|
+
#
|
91
|
+
# class Person
|
92
|
+
# include Mongoid::Document
|
93
|
+
# embeds_many :addresses
|
94
|
+
# end
|
95
|
+
#
|
96
|
+
# class Address
|
97
|
+
# include Mongoid::Document
|
98
|
+
# embedded_in :person, :inverse_of => :addresses
|
99
|
+
# end
|
100
|
+
def embedded_in(name, options = {}, &block)
|
101
|
+
unless options.has_key?(:inverse_of)
|
102
|
+
raise Errors::InvalidOptions.new("Options for embedded_in association must include :inverse_of")
|
103
|
+
end
|
104
|
+
self.embedded = true
|
105
|
+
associate(Associations::EmbeddedIn, optionize(name, options, nil, &block))
|
106
|
+
end
|
107
|
+
|
108
|
+
# Adds the association from a parent document to its children. The name
|
109
|
+
# of the association needs to be a pluralized form of the child class
|
110
|
+
# name.
|
111
|
+
#
|
112
|
+
# Options:
|
113
|
+
#
|
114
|
+
# name: A +Symbol+ that is the plural child class name.
|
115
|
+
#
|
116
|
+
# Example:
|
117
|
+
#
|
118
|
+
# class Person
|
119
|
+
# include Mongoid::Document
|
120
|
+
# embeds_many :addresses
|
121
|
+
# end
|
122
|
+
#
|
123
|
+
# class Address
|
124
|
+
# include Mongoid::Document
|
125
|
+
# embedded_in :person, :inverse_of => :addresses
|
126
|
+
# end
|
127
|
+
def embeds_many(name, options = {}, &block)
|
128
|
+
associate(Associations::EmbedsMany, optionize(name, options, nil, &block))
|
129
|
+
end
|
130
|
+
|
131
|
+
alias :embed_many :embeds_many
|
132
|
+
|
133
|
+
# Adds the association from a parent document to its child. The name
|
134
|
+
# of the association needs to be a singular form of the child class
|
135
|
+
# name.
|
136
|
+
#
|
137
|
+
# Options:
|
138
|
+
#
|
139
|
+
# name: A +Symbol+ that is the plural child class name.
|
140
|
+
#
|
141
|
+
# Example:
|
142
|
+
#
|
143
|
+
# class Person
|
144
|
+
# include Mongoid::Document
|
145
|
+
# embeds_one :name
|
146
|
+
# end
|
147
|
+
#
|
148
|
+
# class Name
|
149
|
+
# include Mongoid::Document
|
150
|
+
# embedded_in :person
|
151
|
+
# end
|
152
|
+
def embeds_one(name, options = {}, &block)
|
153
|
+
opts = optionize(name, options, nil, &block)
|
154
|
+
type = Associations::EmbedsOne
|
155
|
+
associate(type, opts)
|
156
|
+
add_builder(type, opts)
|
157
|
+
add_creator(type, opts)
|
158
|
+
end
|
159
|
+
|
160
|
+
alias :embed_one :embeds_one
|
161
|
+
|
162
|
+
# Adds a relational association from the child Document to a Document in
|
163
|
+
# another database or collection.
|
164
|
+
#
|
165
|
+
# Options:
|
166
|
+
#
|
167
|
+
# name: A +Symbol+ that is the related class name.
|
168
|
+
#
|
169
|
+
# Example:
|
170
|
+
#
|
171
|
+
# class Game
|
172
|
+
# include Mongoid::Document
|
173
|
+
# referenced_in :person
|
174
|
+
# end
|
175
|
+
#
|
176
|
+
def referenced_in(name, options = {}, &block)
|
177
|
+
opts = optionize(name, options, constraint(name, options, :in), &block)
|
178
|
+
associate(Associations::ReferencedIn, opts)
|
179
|
+
field(opts.foreign_key, :type => Mongoid.use_object_ids ? BSON::ObjectID : String)
|
180
|
+
index(opts.foreign_key) unless embedded?
|
181
|
+
set_callback(:save, :before) { |document| document.update_foreign_keys }
|
182
|
+
end
|
183
|
+
|
184
|
+
alias :belongs_to_related :referenced_in
|
185
|
+
|
186
|
+
# Adds a relational association from the Document to many Documents in
|
187
|
+
# another database or collection.
|
188
|
+
#
|
189
|
+
# Options:
|
190
|
+
#
|
191
|
+
# name: A +Symbol+ that is the related class name pluralized.
|
192
|
+
#
|
193
|
+
# Example:
|
194
|
+
#
|
195
|
+
# class Person
|
196
|
+
# include Mongoid::Document
|
197
|
+
# references_many :posts
|
198
|
+
# end
|
199
|
+
#
|
200
|
+
def references_many(name, options = {}, &block)
|
201
|
+
reference_many(name, options, &block)
|
202
|
+
set_callback :save, :before do |document|
|
203
|
+
document.update_associations(name)
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
alias :has_many_related :references_many
|
208
|
+
|
209
|
+
# Adds a relational association from the Document to one Document in
|
210
|
+
# another database or collection.
|
211
|
+
#
|
212
|
+
# Options:
|
213
|
+
#
|
214
|
+
# name: A +Symbol+ that is the related class name pluralized.
|
215
|
+
#
|
216
|
+
# Example:
|
217
|
+
#
|
218
|
+
# class Person
|
219
|
+
# include Mongoid::Document
|
220
|
+
# references_one :game
|
221
|
+
# end
|
222
|
+
def references_one(name, options = {}, &block)
|
223
|
+
opts = optionize(name, options, constraint(name, options, :one), &block)
|
224
|
+
associate(Associations::ReferencesOne, opts)
|
225
|
+
set_callback :save, :before do |document|
|
226
|
+
document.update_association(name)
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
alias :has_one_related :references_one
|
231
|
+
|
232
|
+
# Returns the macro associated with the supplied association name. This
|
233
|
+
# will return embeds_on, embeds_many, embedded_in or nil.
|
234
|
+
#
|
235
|
+
# Options:
|
236
|
+
#
|
237
|
+
# name: The association name.
|
238
|
+
#
|
239
|
+
# Example:
|
240
|
+
#
|
241
|
+
# <tt>Person.reflect_on_association(:addresses)</tt>
|
242
|
+
def reflect_on_association(name)
|
243
|
+
association = associations[name.to_s]
|
244
|
+
association ? association.macro : nil
|
245
|
+
end
|
246
|
+
|
247
|
+
protected
|
248
|
+
# Adds the association to the associations hash with the type as the key,
|
249
|
+
# then adds the accessors for the association. The defined setters and
|
250
|
+
# getters for the associations will perform the necessary memoization.
|
251
|
+
#
|
252
|
+
# Example:
|
253
|
+
#
|
254
|
+
# <tt>Person.associate(EmbedsMany, { :name => :addresses })</tt>
|
255
|
+
def associate(type, options)
|
256
|
+
name = options.name.to_s
|
257
|
+
associations[name] = MetaData.new(type, options)
|
258
|
+
define_method(name) { memoized(name) { type.instantiate(self, options) } }
|
259
|
+
define_method("#{name}=") do |object|
|
260
|
+
unmemoize(name)
|
261
|
+
memoized(name) { type.update(object, self, options) }
|
262
|
+
end
|
263
|
+
end
|
264
|
+
|
265
|
+
# Adds a builder for a has_one association. This comes in the form of
|
266
|
+
# build_name(attributes)
|
267
|
+
def add_builder(type, options)
|
268
|
+
name = options.name.to_s
|
269
|
+
define_method("build_#{name}") do |*params|
|
270
|
+
attrs = params[0]
|
271
|
+
attr_options = params[1] || {}
|
272
|
+
reset(name) { type.new(self, (attrs || {}).stringify_keys, options) } unless type == Associations::EmbedsOne && attr_options[:update_only]
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
276
|
+
# Adds a creator for a has_one association. This comes in the form of
|
277
|
+
# create_name(attributes)
|
278
|
+
def add_creator(type, options)
|
279
|
+
name = options.name.to_s
|
280
|
+
define_method("create_#{name}") do |*params|
|
281
|
+
attrs = params[0]
|
282
|
+
attr_options = params[1] || {}
|
283
|
+
send("build_#{name}", attrs, attr_options).tap(&:save) unless type == Associations::EmbedsOne && attr_options[:update_only]
|
284
|
+
end
|
285
|
+
end
|
286
|
+
|
287
|
+
# build the options given the params.
|
288
|
+
def optionize(name, options, foreign_key, &block)
|
289
|
+
Associations::Options.new(
|
290
|
+
options.merge(:name => name, :foreign_key => foreign_key, :extend => block)
|
291
|
+
)
|
292
|
+
end
|
293
|
+
|
294
|
+
def reference_many(name, options, &block)
|
295
|
+
if (options[:stored_as] == :array)
|
296
|
+
opts = optionize(name, options, constraint(name, options, :many_as_array), &block)
|
297
|
+
field "#{name.to_s.singularize}_ids", :type => Array, :default => []
|
298
|
+
associate(Associations::ReferencesManyAsArray, opts)
|
299
|
+
else
|
300
|
+
opts = optionize(name, options, constraint(name, options, :many), &block)
|
301
|
+
associate(Associations::ReferencesMany, opts)
|
302
|
+
end
|
303
|
+
end
|
304
|
+
end
|
305
|
+
end
|
306
|
+
end
|