mongoid 2.0.2 → 2.1.0
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/README.rdoc +3 -1
- data/Rakefile +3 -2
- data/lib/config/locales/bg.yml +6 -0
- data/lib/config/locales/de.yml +6 -0
- data/lib/config/locales/en-GB.yml +48 -0
- data/lib/config/locales/en.yml +6 -3
- data/lib/config/locales/es.yml +6 -0
- data/lib/config/locales/fr.yml +6 -0
- data/lib/config/locales/hi.yml +39 -0
- data/lib/config/locales/hu.yml +13 -7
- data/lib/config/locales/id.yml +3 -0
- data/lib/config/locales/it.yml +7 -1
- data/lib/config/locales/ja.yml +4 -1
- data/lib/config/locales/kr.yml +9 -34
- data/lib/config/locales/nl.yml +6 -0
- data/lib/config/locales/pl.yml +6 -0
- data/lib/config/locales/pt-BR.yml +6 -0
- data/lib/config/locales/pt.yml +6 -0
- data/lib/config/locales/ro.yml +6 -0
- data/lib/config/locales/ru.yml +6 -0
- data/lib/config/locales/sv.yml +6 -0
- data/lib/config/locales/vi.yml +3 -0
- data/lib/config/locales/zh-CN.yml +6 -0
- data/lib/mongoid.rb +51 -45
- data/lib/mongoid/atomic.rb +145 -0
- data/lib/mongoid/atomic/modifiers.rb +109 -0
- data/lib/mongoid/atomic/paths.rb +3 -0
- data/lib/mongoid/atomic/paths/embedded.rb +43 -0
- data/lib/mongoid/atomic/paths/embedded/many.rb +44 -0
- data/lib/mongoid/atomic/paths/embedded/one.rb +43 -0
- data/lib/mongoid/atomic/paths/root.rb +40 -0
- data/lib/mongoid/attributes.rb +12 -23
- data/lib/mongoid/attributes/processing.rb +5 -5
- data/lib/mongoid/callbacks.rb +2 -0
- data/lib/mongoid/collection.rb +12 -59
- data/lib/mongoid/collections.rb +23 -20
- data/lib/mongoid/collections/master.rb +6 -4
- data/lib/mongoid/collections/operations.rb +1 -0
- data/lib/mongoid/collections/retry.rb +7 -0
- data/lib/mongoid/components.rb +2 -2
- data/lib/mongoid/config.rb +42 -55
- data/lib/mongoid/config/database.rb +6 -2
- data/lib/mongoid/config/replset_database.rb +7 -3
- data/lib/mongoid/contexts.rb +9 -3
- data/lib/mongoid/contexts/enumerable.rb +7 -3
- data/lib/mongoid/contexts/mongo.rb +139 -101
- data/lib/mongoid/criteria.rb +86 -69
- data/lib/mongoid/criterion/complex.rb +32 -5
- data/lib/mongoid/criterion/inclusion.rb +4 -2
- data/lib/mongoid/criterion/optional.rb +111 -86
- data/lib/mongoid/criterion/selector.rb +8 -4
- data/lib/mongoid/cursor.rb +27 -27
- data/lib/mongoid/dirty.rb +54 -214
- data/lib/mongoid/document.rb +37 -39
- data/lib/mongoid/errors/document_not_found.rb +3 -4
- data/lib/mongoid/errors/invalid_collection.rb +2 -3
- data/lib/mongoid/errors/invalid_database.rb +2 -3
- data/lib/mongoid/errors/invalid_field.rb +2 -3
- data/lib/mongoid/errors/invalid_options.rb +19 -7
- data/lib/mongoid/errors/invalid_type.rb +2 -3
- data/lib/mongoid/errors/mongoid_error.rb +5 -6
- data/lib/mongoid/errors/too_many_nested_attribute_records.rb +2 -3
- data/lib/mongoid/errors/unsupported_version.rb +2 -3
- data/lib/mongoid/errors/validations.rb +2 -3
- data/lib/mongoid/extensions.rb +8 -62
- data/lib/mongoid/extensions/array/deletion.rb +29 -0
- data/lib/mongoid/extensions/false_class/equality.rb +14 -1
- data/lib/mongoid/extensions/hash/criteria_helpers.rb +21 -10
- data/lib/mongoid/extensions/hash/scoping.rb +14 -1
- data/lib/mongoid/extensions/nil/collectionization.rb +12 -1
- data/lib/mongoid/extensions/object/reflections.rb +33 -2
- data/lib/mongoid/extensions/object_id/conversions.rb +2 -36
- data/lib/mongoid/extensions/proc/scoping.rb +14 -1
- data/lib/mongoid/extensions/string/conversions.rb +4 -16
- data/lib/mongoid/extensions/string/inflections.rb +35 -14
- data/lib/mongoid/extensions/symbol/inflections.rb +38 -12
- data/lib/mongoid/extensions/true_class/equality.rb +14 -1
- data/lib/mongoid/extras.rb +11 -30
- data/lib/mongoid/factory.rb +1 -1
- data/lib/mongoid/fields.rb +121 -29
- data/lib/mongoid/fields/mappings.rb +36 -0
- data/lib/mongoid/fields/serializable.rb +131 -0
- data/lib/mongoid/fields/serializable/array.rb +64 -0
- data/lib/mongoid/fields/serializable/big_decimal.rb +42 -0
- data/lib/mongoid/fields/serializable/bignum.rb +10 -0
- data/lib/mongoid/fields/serializable/binary.rb +11 -0
- data/lib/mongoid/fields/serializable/boolean.rb +44 -0
- data/lib/mongoid/fields/serializable/date.rb +51 -0
- data/lib/mongoid/fields/serializable/date_time.rb +28 -0
- data/lib/mongoid/fields/serializable/fixnum.rb +10 -0
- data/lib/mongoid/fields/serializable/float.rb +33 -0
- data/lib/mongoid/fields/serializable/foreign_keys/array.rb +56 -0
- data/lib/mongoid/fields/serializable/foreign_keys/object.rb +43 -0
- data/lib/mongoid/fields/serializable/hash.rb +25 -0
- data/lib/mongoid/fields/serializable/integer.rb +33 -0
- data/lib/mongoid/fields/serializable/object.rb +11 -0
- data/lib/mongoid/fields/serializable/object_id.rb +32 -0
- data/lib/mongoid/fields/serializable/range.rb +42 -0
- data/lib/mongoid/fields/serializable/set.rb +42 -0
- data/lib/mongoid/fields/serializable/string.rb +28 -0
- data/lib/mongoid/fields/serializable/symbol.rb +28 -0
- data/lib/mongoid/fields/serializable/time.rb +12 -0
- data/lib/mongoid/fields/serializable/time_with_zone.rb +12 -0
- data/lib/mongoid/fields/serializable/timekeeping.rb +102 -0
- data/lib/mongoid/finders.rb +61 -37
- data/lib/mongoid/hierarchy.rb +43 -8
- data/lib/mongoid/identity_map.rb +106 -0
- data/lib/mongoid/indexes.rb +17 -1
- data/lib/mongoid/javascript.rb +2 -3
- data/lib/mongoid/keys.rb +10 -21
- data/lib/mongoid/logger.rb +22 -1
- data/lib/mongoid/matchers/all.rb +10 -0
- data/lib/mongoid/matchers/default.rb +1 -1
- data/lib/mongoid/matchers/exists.rb +10 -0
- data/lib/mongoid/matchers/gt.rb +10 -0
- data/lib/mongoid/matchers/gte.rb +10 -0
- data/lib/mongoid/matchers/in.rb +10 -0
- data/lib/mongoid/matchers/lt.rb +10 -0
- data/lib/mongoid/matchers/lte.rb +10 -0
- data/lib/mongoid/matchers/ne.rb +10 -0
- data/lib/mongoid/matchers/nin.rb +10 -0
- data/lib/mongoid/matchers/or.rb +7 -4
- data/lib/mongoid/matchers/size.rb +10 -0
- data/lib/mongoid/multi_database.rb +26 -6
- data/lib/mongoid/multi_parameter_attributes.rb +40 -17
- data/lib/mongoid/named_scope.rb +1 -2
- data/lib/mongoid/nested_attributes.rb +4 -1
- data/lib/mongoid/observer.rb +108 -5
- data/lib/mongoid/paranoia.rb +26 -26
- data/lib/mongoid/persistence.rb +15 -21
- data/lib/mongoid/persistence/atomic.rb +135 -0
- data/lib/mongoid/persistence/atomic/add_to_set.rb +11 -8
- data/lib/mongoid/persistence/atomic/bit.rb +37 -0
- data/lib/mongoid/persistence/atomic/inc.rb +9 -6
- data/lib/mongoid/persistence/atomic/operation.rb +48 -7
- data/lib/mongoid/persistence/atomic/pop.rb +34 -0
- data/lib/mongoid/persistence/atomic/pull.rb +34 -0
- data/lib/mongoid/persistence/atomic/pull_all.rb +10 -9
- data/lib/mongoid/persistence/atomic/push.rb +8 -5
- data/lib/mongoid/persistence/atomic/push_all.rb +31 -0
- data/lib/mongoid/persistence/atomic/rename.rb +31 -0
- data/lib/mongoid/persistence/atomic/set.rb +30 -0
- data/lib/mongoid/persistence/atomic/unset.rb +28 -0
- data/lib/mongoid/persistence/deletion.rb +32 -0
- data/lib/mongoid/persistence/insertion.rb +41 -0
- data/lib/mongoid/persistence/modification.rb +37 -0
- data/lib/mongoid/persistence/operations.rb +214 -0
- data/lib/mongoid/persistence/operations/embedded/insert.rb +42 -0
- data/lib/mongoid/persistence/operations/embedded/remove.rb +40 -0
- data/lib/mongoid/persistence/operations/insert.rb +34 -0
- data/lib/mongoid/persistence/operations/remove.rb +33 -0
- data/lib/mongoid/persistence/operations/update.rb +53 -0
- data/lib/mongoid/railtie.rb +21 -33
- data/lib/mongoid/railties/database.rake +12 -12
- data/lib/mongoid/relations.rb +9 -5
- data/lib/mongoid/relations/accessors.rb +15 -36
- data/lib/mongoid/relations/auto_save.rb +2 -2
- data/lib/mongoid/relations/binding.rb +28 -1
- data/lib/mongoid/relations/bindings/embedded/in.rb +17 -30
- data/lib/mongoid/relations/bindings/embedded/many.rb +16 -21
- data/lib/mongoid/relations/bindings/embedded/one.rb +11 -16
- data/lib/mongoid/relations/bindings/referenced/in.rb +31 -32
- data/lib/mongoid/relations/bindings/referenced/many.rb +19 -61
- data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +15 -63
- data/lib/mongoid/relations/bindings/referenced/one.rb +18 -26
- data/lib/mongoid/relations/builder.rb +4 -2
- data/lib/mongoid/relations/builders.rb +21 -2
- data/lib/mongoid/relations/builders/embedded/in.rb +5 -1
- data/lib/mongoid/relations/builders/embedded/many.rb +12 -4
- data/lib/mongoid/relations/builders/embedded/one.rb +5 -1
- data/lib/mongoid/relations/builders/nested_attributes/many.rb +2 -2
- data/lib/mongoid/relations/builders/nested_attributes/one.rb +1 -1
- data/lib/mongoid/relations/builders/referenced/in.rb +2 -5
- data/lib/mongoid/relations/builders/referenced/many.rb +2 -3
- data/lib/mongoid/relations/builders/referenced/many_to_many.rb +14 -5
- data/lib/mongoid/relations/builders/referenced/one.rb +2 -3
- data/lib/mongoid/relations/embedded/atomic.rb +2 -2
- data/lib/mongoid/relations/embedded/in.rb +72 -41
- data/lib/mongoid/relations/embedded/many.rb +116 -120
- data/lib/mongoid/relations/embedded/one.rb +59 -41
- data/lib/mongoid/relations/embedded/sort.rb +31 -0
- data/lib/mongoid/relations/macros.rb +28 -24
- data/lib/mongoid/relations/many.rb +10 -103
- data/lib/mongoid/relations/metadata.rb +335 -38
- data/lib/mongoid/relations/one.rb +7 -32
- data/lib/mongoid/relations/options.rb +47 -0
- data/lib/mongoid/relations/proxy.rb +29 -28
- data/lib/mongoid/relations/referenced/batch.rb +2 -3
- data/lib/mongoid/relations/referenced/in.rb +66 -53
- data/lib/mongoid/relations/referenced/many.rb +216 -143
- data/lib/mongoid/relations/referenced/many_to_many.rb +132 -163
- data/lib/mongoid/relations/referenced/one.rb +76 -58
- data/lib/mongoid/relations/synchronization.rb +113 -0
- data/lib/mongoid/relations/targets.rb +2 -0
- data/lib/mongoid/relations/targets/enumerable.rb +329 -0
- data/lib/mongoid/safety.rb +24 -156
- data/lib/mongoid/serialization.rb +21 -0
- data/lib/mongoid/state.rb +34 -0
- data/lib/mongoid/threaded.rb +175 -0
- data/lib/mongoid/timestamps/updated.rb +1 -1
- data/lib/mongoid/validations.rb +3 -7
- data/lib/mongoid/version.rb +1 -1
- data/lib/mongoid/versioning.rb +61 -7
- data/lib/rack/mongoid.rb +2 -0
- data/lib/rack/mongoid/middleware/identity_map.rb +38 -0
- data/lib/rails/generators/mongoid/model/model_generator.rb +1 -1
- data/lib/rails/generators/mongoid/model/templates/{model.rb → model.rb.tt} +0 -0
- data/lib/rails/generators/mongoid/observer/observer_generator.rb +1 -1
- data/lib/rails/generators/mongoid/observer/templates/{observer.rb → observer.rb.tt} +0 -0
- data/lib/rails/mongoid.rb +17 -17
- metadata +136 -102
- data/lib/mongoid/atomicity.rb +0 -111
- data/lib/mongoid/collections/cyclic_iterator.rb +0 -34
- data/lib/mongoid/collections/slaves.rb +0 -61
- data/lib/mongoid/extensions/array/conversions.rb +0 -23
- data/lib/mongoid/extensions/array/parentization.rb +0 -13
- data/lib/mongoid/extensions/big_decimal/conversions.rb +0 -19
- data/lib/mongoid/extensions/binary/conversions.rb +0 -17
- data/lib/mongoid/extensions/boolean/conversions.rb +0 -27
- data/lib/mongoid/extensions/date/conversions.rb +0 -25
- data/lib/mongoid/extensions/datetime/conversions.rb +0 -12
- data/lib/mongoid/extensions/float/conversions.rb +0 -20
- data/lib/mongoid/extensions/hash/conversions.rb +0 -19
- data/lib/mongoid/extensions/integer/conversions.rb +0 -20
- data/lib/mongoid/extensions/object/conversions.rb +0 -25
- data/lib/mongoid/extensions/range/conversions.rb +0 -25
- data/lib/mongoid/extensions/set/conversions.rb +0 -20
- data/lib/mongoid/extensions/symbol/conversions.rb +0 -21
- data/lib/mongoid/extensions/time_conversions.rb +0 -38
- data/lib/mongoid/field.rb +0 -162
- data/lib/mongoid/paths.rb +0 -61
- data/lib/mongoid/persistence/command.rb +0 -71
- data/lib/mongoid/persistence/insert.rb +0 -53
- data/lib/mongoid/persistence/insert_embedded.rb +0 -43
- data/lib/mongoid/persistence/remove.rb +0 -44
- data/lib/mongoid/persistence/remove_all.rb +0 -40
- data/lib/mongoid/persistence/remove_embedded.rb +0 -48
- data/lib/mongoid/persistence/update.rb +0 -77
- data/lib/mongoid/safe.rb +0 -23
- data/lib/mongoid/validations/referenced.rb +0 -58
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
require "mongoid/atomic/paths/embedded/one"
|
|
3
|
+
require "mongoid/atomic/paths/embedded/many"
|
|
4
|
+
|
|
5
|
+
module Mongoid #:nodoc:
|
|
6
|
+
module Atomic #:nodoc:
|
|
7
|
+
module Paths #:nodoc:
|
|
8
|
+
|
|
9
|
+
# Common functionality between the two different embedded paths.
|
|
10
|
+
module Embedded
|
|
11
|
+
|
|
12
|
+
attr_reader :delete_modifier, :document, :insert_modifier, :parent
|
|
13
|
+
|
|
14
|
+
# Get the path to the document in the hierarchy.
|
|
15
|
+
#
|
|
16
|
+
# @example Get the path.
|
|
17
|
+
# many.path
|
|
18
|
+
#
|
|
19
|
+
# @return [ String ] The path to the document.
|
|
20
|
+
#
|
|
21
|
+
# @since 2.1.0
|
|
22
|
+
def path
|
|
23
|
+
position.sub(/\.\d+$/, "")
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Get the selector to use for the root document when performing atomic
|
|
27
|
+
# updates. When sharding this will include the shard key.
|
|
28
|
+
#
|
|
29
|
+
# @example Get the selector.
|
|
30
|
+
# many.selector
|
|
31
|
+
#
|
|
32
|
+
# @return [ Hash ] The selector to identify the document with.
|
|
33
|
+
#
|
|
34
|
+
# @since 2.1.0
|
|
35
|
+
def selector
|
|
36
|
+
parent.atomic_selector.
|
|
37
|
+
merge!({ "#{path}._id" => document.identifier || document._id }).
|
|
38
|
+
merge!(document.shard_key_selector)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Atomic #:nodoc:
|
|
4
|
+
module Paths #:nodoc:
|
|
5
|
+
module Embedded #:nodoc:
|
|
6
|
+
|
|
7
|
+
# This class encapsulates behaviour for locating and updating
|
|
8
|
+
# documents that are defined as an embedded 1-n.
|
|
9
|
+
class Many
|
|
10
|
+
include Embedded
|
|
11
|
+
|
|
12
|
+
# Create the new path utility.
|
|
13
|
+
#
|
|
14
|
+
# @example Create the path util.
|
|
15
|
+
# Many.new(document)
|
|
16
|
+
#
|
|
17
|
+
# @param [ Document ] document The document to generate the paths for.
|
|
18
|
+
#
|
|
19
|
+
# @since 2.1.0
|
|
20
|
+
def initialize(document)
|
|
21
|
+
@document, @parent = document, document._parent
|
|
22
|
+
@insert_modifier, @delete_modifier ="$push", "$pull"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Get the position of the document in the hierarchy. This will
|
|
26
|
+
# include indexes of 1-n embedded relations that may sit above the
|
|
27
|
+
# embedded many.
|
|
28
|
+
#
|
|
29
|
+
# @example Get the position.
|
|
30
|
+
# many.position
|
|
31
|
+
#
|
|
32
|
+
# @return [ String ] The position of the document.
|
|
33
|
+
#
|
|
34
|
+
# @since 2.1.0
|
|
35
|
+
def position
|
|
36
|
+
pos = parent.atomic_position
|
|
37
|
+
locator = document.new? ? "" : ".#{document._index}"
|
|
38
|
+
"#{pos}#{"." unless pos.blank?}#{document.metadata.name}#{locator}"
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Atomic #:nodoc:
|
|
4
|
+
module Paths #:nodoc:
|
|
5
|
+
module Embedded #:nodoc:
|
|
6
|
+
|
|
7
|
+
# This class encapsulates behaviour for locating and updating
|
|
8
|
+
# documents that are defined as an embedded 1-1.
|
|
9
|
+
class One
|
|
10
|
+
include Embedded
|
|
11
|
+
|
|
12
|
+
# Create the new path utility.
|
|
13
|
+
#
|
|
14
|
+
# @example Create the path util.
|
|
15
|
+
# One.new(document)
|
|
16
|
+
#
|
|
17
|
+
# @param [ Document ] document The document to generate the paths for.
|
|
18
|
+
#
|
|
19
|
+
# @since 2.1.0
|
|
20
|
+
def initialize(document)
|
|
21
|
+
@document, @parent = document, document._parent
|
|
22
|
+
@insert_modifier, @delete_modifier ="$set", "$unset"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Get the position of the document in the hierarchy. This will
|
|
26
|
+
# include indexes of 1-n embedded relations that may sit above the
|
|
27
|
+
# embedded one.
|
|
28
|
+
#
|
|
29
|
+
# @example Get the position.
|
|
30
|
+
# one.position
|
|
31
|
+
#
|
|
32
|
+
# @return [ String ] The position of the document.
|
|
33
|
+
#
|
|
34
|
+
# @since 2.1.0
|
|
35
|
+
def position
|
|
36
|
+
pos = parent.atomic_position
|
|
37
|
+
"#{pos}#{"." unless pos.blank?}#{document.metadata.name}"
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Atomic #:nodoc:
|
|
4
|
+
module Paths #:nodoc:
|
|
5
|
+
|
|
6
|
+
# This class encapsulates behaviour for locating and updating root
|
|
7
|
+
# documents atomically.
|
|
8
|
+
class Root
|
|
9
|
+
|
|
10
|
+
attr_reader :document, :path, :position
|
|
11
|
+
|
|
12
|
+
# Create the new root path utility.
|
|
13
|
+
#
|
|
14
|
+
# @example Create the root path util.
|
|
15
|
+
# Root.new(document)
|
|
16
|
+
#
|
|
17
|
+
# @param [ Document ] document The document to generate the paths for.
|
|
18
|
+
#
|
|
19
|
+
# @since 2.1.0
|
|
20
|
+
def initialize(document)
|
|
21
|
+
@document, @path, @position = document, "", ""
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Get the selector to use for the root document when performing atomic
|
|
25
|
+
# updates. When sharding this will include the shard key.
|
|
26
|
+
#
|
|
27
|
+
# @example Get the selector.
|
|
28
|
+
# root.selector
|
|
29
|
+
#
|
|
30
|
+
# @return [ Hash ] The selector to identify the document with.
|
|
31
|
+
#
|
|
32
|
+
# @since 2.1.0
|
|
33
|
+
def selector
|
|
34
|
+
{ "_id" => document.identifier || document._id }.
|
|
35
|
+
merge!(document.shard_key_selector)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
data/lib/mongoid/attributes.rb
CHANGED
|
@@ -22,7 +22,8 @@ module Mongoid #:nodoc:
|
|
|
22
22
|
#
|
|
23
23
|
# @since 1.0.0
|
|
24
24
|
def attribute_present?(name)
|
|
25
|
-
|
|
25
|
+
attribute = read_attribute(name)
|
|
26
|
+
! attribute.blank? || attribute == false
|
|
26
27
|
end
|
|
27
28
|
|
|
28
29
|
# Read a value from the document attributes. If the value does not exist
|
|
@@ -40,9 +41,7 @@ module Mongoid #:nodoc:
|
|
|
40
41
|
#
|
|
41
42
|
# @since 1.0.0
|
|
42
43
|
def read_attribute(name)
|
|
43
|
-
|
|
44
|
-
value = attributes[access]
|
|
45
|
-
accessed(access, value)
|
|
44
|
+
attributes[name.to_s]
|
|
46
45
|
end
|
|
47
46
|
alias :[] :read_attribute
|
|
48
47
|
|
|
@@ -57,7 +56,8 @@ module Mongoid #:nodoc:
|
|
|
57
56
|
# @since 1.0.0
|
|
58
57
|
def remove_attribute(name)
|
|
59
58
|
access = name.to_s
|
|
60
|
-
|
|
59
|
+
attribute_will_change!(access)
|
|
60
|
+
attributes.delete(access)
|
|
61
61
|
end
|
|
62
62
|
|
|
63
63
|
# Override respond_to? so it responds properly for dynamic attributes.
|
|
@@ -93,7 +93,10 @@ module Mongoid #:nodoc:
|
|
|
93
93
|
# @since 1.0.0
|
|
94
94
|
def write_attribute(name, value)
|
|
95
95
|
access = name.to_s
|
|
96
|
-
|
|
96
|
+
typed_value_for(access, value).tap do |value|
|
|
97
|
+
attribute_will_change!(access) unless attributes[access] == value || attribute_changed?(access)
|
|
98
|
+
attributes[access] = value
|
|
99
|
+
end
|
|
97
100
|
end
|
|
98
101
|
alias :[]= :write_attribute
|
|
99
102
|
|
|
@@ -120,21 +123,6 @@ module Mongoid #:nodoc:
|
|
|
120
123
|
|
|
121
124
|
protected
|
|
122
125
|
|
|
123
|
-
# Get the default values for the attributes.
|
|
124
|
-
#
|
|
125
|
-
# @example Get the defaults.
|
|
126
|
-
# person.default_attributes
|
|
127
|
-
#
|
|
128
|
-
# @return [ Hash ] The default values for each field.
|
|
129
|
-
#
|
|
130
|
-
# @since 1.0.0
|
|
131
|
-
#
|
|
132
|
-
# @raise [ RuntimeError ] Always
|
|
133
|
-
# @since 2.0.0.rc.8
|
|
134
|
-
def default_attributes
|
|
135
|
-
raise "default_attributes is no longer valid. Plase use: apply_default_attributes."
|
|
136
|
-
end
|
|
137
|
-
|
|
138
126
|
# Set any missing default values in the attributes.
|
|
139
127
|
#
|
|
140
128
|
# @example Get the raw attributes after defaults have been applied.
|
|
@@ -147,7 +135,8 @@ module Mongoid #:nodoc:
|
|
|
147
135
|
(@attributes ||= {}).tap do |h|
|
|
148
136
|
defaults.each_pair do |key, val|
|
|
149
137
|
unless h.has_key?(key)
|
|
150
|
-
h[key] = val.respond_to?(:call) ? typed_value_for(key, val.call) :
|
|
138
|
+
h[key] = val.respond_to?(:call) ? typed_value_for(key, val.call) :
|
|
139
|
+
val.duplicable? ? val.dup : val
|
|
151
140
|
end
|
|
152
141
|
end
|
|
153
142
|
end
|
|
@@ -179,7 +168,7 @@ module Mongoid #:nodoc:
|
|
|
179
168
|
#
|
|
180
169
|
# @since 1.0.0
|
|
181
170
|
def typed_value_for(key, value)
|
|
182
|
-
fields.has_key?(key) ? fields[key].
|
|
171
|
+
fields.has_key?(key) ? fields[key].serialize(value) : value
|
|
183
172
|
end
|
|
184
173
|
end
|
|
185
174
|
end
|
|
@@ -25,14 +25,14 @@ module Mongoid #:nodoc:
|
|
|
25
25
|
process_attribute(key, value)
|
|
26
26
|
end
|
|
27
27
|
yield self if block_given?
|
|
28
|
-
process_pending
|
|
28
|
+
process_pending
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
protected
|
|
32
32
|
|
|
33
|
-
# If the key provided a relation or a nested attribute,
|
|
34
|
-
#
|
|
35
|
-
#
|
|
33
|
+
# If the key provided is the name of a relation or a nested attribute, we
|
|
34
|
+
# need to wait until all other attributes are set before processing
|
|
35
|
+
# these.
|
|
36
36
|
#
|
|
37
37
|
# @example Is the attribute pending?
|
|
38
38
|
# document.pending_attribute?(:name, "Durran")
|
|
@@ -135,7 +135,7 @@ module Mongoid #:nodoc:
|
|
|
135
135
|
if value.is_a?(Hash)
|
|
136
136
|
metadata.nested_builder(value, {}).build(self)
|
|
137
137
|
else
|
|
138
|
-
send("#{name}=", value
|
|
138
|
+
send("#{name}=", value)
|
|
139
139
|
end
|
|
140
140
|
end
|
|
141
141
|
end
|
data/lib/mongoid/callbacks.rb
CHANGED
data/lib/mongoid/collection.rb
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
# encoding: utf-8
|
|
2
2
|
require "mongoid/collections/retry"
|
|
3
3
|
require "mongoid/collections/operations"
|
|
4
|
-
require "mongoid/collections/cyclic_iterator"
|
|
5
4
|
require "mongoid/collections/master"
|
|
6
|
-
require "mongoid/collections/slaves"
|
|
7
5
|
|
|
8
6
|
module Mongoid #:nodoc
|
|
9
7
|
|
|
@@ -17,28 +15,7 @@ module Mongoid #:nodoc
|
|
|
17
15
|
#
|
|
18
16
|
# @example Delegate the operation.
|
|
19
17
|
# collection.save({ :name => "Al" })
|
|
20
|
-
Collections::Operations::PROXIED.
|
|
21
|
-
define_method(name) { |*args| master.send(name, *args) }
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
# Determines where to send the next read query. If the slaves are not
|
|
25
|
-
# defined then send to master. If the read counter is under the configured
|
|
26
|
-
# maximum then return the master. In any other case return the slaves.
|
|
27
|
-
#
|
|
28
|
-
# @example Send the operation to the master or slaves.
|
|
29
|
-
# collection.directed
|
|
30
|
-
#
|
|
31
|
-
# @param [ Hash ] options The operation options.
|
|
32
|
-
#
|
|
33
|
-
# @option options [ true, false ] :cache Should the query cache in memory?
|
|
34
|
-
# @option options [ true, false ] :enslave Send the write to the slave?
|
|
35
|
-
#
|
|
36
|
-
# @return [ Master, Slaves ] The connection to use.
|
|
37
|
-
def directed(options = {})
|
|
38
|
-
options.delete(:cache)
|
|
39
|
-
enslave = options.delete(:enslave) || @klass.enslaved?
|
|
40
|
-
enslave ? master_or_slaves : master
|
|
41
|
-
end
|
|
18
|
+
delegate *(Collections::Operations::PROXIED.dup << {:to => :master})
|
|
42
19
|
|
|
43
20
|
# Find documents from the database given a selector and options.
|
|
44
21
|
#
|
|
@@ -50,7 +27,7 @@ module Mongoid #:nodoc
|
|
|
50
27
|
#
|
|
51
28
|
# @return [ Cursor ] The results.
|
|
52
29
|
def find(selector = {}, options = {})
|
|
53
|
-
cursor = Mongoid::Cursor.new(@klass, self,
|
|
30
|
+
cursor = Mongoid::Cursor.new(@klass, self, master(options).find(selector, options))
|
|
54
31
|
if block_given?
|
|
55
32
|
yield cursor; cursor.close
|
|
56
33
|
else
|
|
@@ -68,7 +45,7 @@ module Mongoid #:nodoc
|
|
|
68
45
|
#
|
|
69
46
|
# @return [ Document, nil ] A matching document or nil if none found.
|
|
70
47
|
def find_one(selector = {}, options = {})
|
|
71
|
-
|
|
48
|
+
master(options).find_one(selector, options)
|
|
72
49
|
end
|
|
73
50
|
|
|
74
51
|
# Initialize a new Mongoid::Collection, setting up the master, slave, and
|
|
@@ -96,11 +73,11 @@ module Mongoid #:nodoc
|
|
|
96
73
|
#
|
|
97
74
|
# @since 2.0.2, batch-relational-insert
|
|
98
75
|
def insert(documents, options = {})
|
|
99
|
-
|
|
100
|
-
if
|
|
101
|
-
|
|
76
|
+
consumer = Threaded.insert
|
|
77
|
+
if consumer
|
|
78
|
+
consumer.consume(documents, options)
|
|
102
79
|
else
|
|
103
|
-
master.insert(documents, options)
|
|
80
|
+
master(options).insert(documents, options)
|
|
104
81
|
end
|
|
105
82
|
end
|
|
106
83
|
|
|
@@ -115,7 +92,7 @@ module Mongoid #:nodoc
|
|
|
115
92
|
#
|
|
116
93
|
# @return [ Cursor ] The results.
|
|
117
94
|
def map_reduce(map, reduce, options = {})
|
|
118
|
-
|
|
95
|
+
master(options).map_reduce(map, reduce, options)
|
|
119
96
|
end
|
|
120
97
|
alias :mapreduce :map_reduce
|
|
121
98
|
|
|
@@ -126,24 +103,12 @@ module Mongoid #:nodoc
|
|
|
126
103
|
# collection.master
|
|
127
104
|
#
|
|
128
105
|
# @return [ Master ] The master connection.
|
|
129
|
-
def master
|
|
106
|
+
def master(options = {})
|
|
107
|
+
options.delete(:cache)
|
|
130
108
|
db = Mongoid.databases[@klass.database] || Mongoid.master
|
|
131
109
|
@master ||= Collections::Master.new(db, @name)
|
|
132
110
|
end
|
|
133
111
|
|
|
134
|
-
# Return the object responsible for reading documents from the database.
|
|
135
|
-
# This is usually the slave databases, but in their absence the master will
|
|
136
|
-
# handle the task.
|
|
137
|
-
#
|
|
138
|
-
# @example Get the slaves array.
|
|
139
|
-
# collection.slaves
|
|
140
|
-
#
|
|
141
|
-
# @return [ Slaves ] The pool of slave connections.
|
|
142
|
-
def slaves
|
|
143
|
-
slaves = Mongoid.databases["#{@klass.database}_slaves"] || Mongoid.slaves
|
|
144
|
-
@slaves ||= Collections::Slaves.new(slaves, @name)
|
|
145
|
-
end
|
|
146
|
-
|
|
147
112
|
# Updates one or more documents in the collection.
|
|
148
113
|
#
|
|
149
114
|
# @example Update documents.
|
|
@@ -159,24 +124,12 @@ module Mongoid #:nodoc
|
|
|
159
124
|
#
|
|
160
125
|
# @since 2.0.0
|
|
161
126
|
def update(selector, document, options = {})
|
|
162
|
-
updater =
|
|
127
|
+
updater = Threaded.update
|
|
163
128
|
if updater
|
|
164
129
|
updater.consume(selector, document, options)
|
|
165
130
|
else
|
|
166
|
-
master.update(selector, document, options)
|
|
131
|
+
master(options).update(selector, document, options)
|
|
167
132
|
end
|
|
168
133
|
end
|
|
169
|
-
|
|
170
|
-
protected
|
|
171
|
-
|
|
172
|
-
# Determine if the read is going to the master or the slaves.
|
|
173
|
-
#
|
|
174
|
-
# @example Use the master or slaves?
|
|
175
|
-
# collection.master_or_slaves
|
|
176
|
-
#
|
|
177
|
-
# @return [ Master, Slaves ] Master if not slaves exist, or slaves.
|
|
178
|
-
def master_or_slaves
|
|
179
|
-
slaves.empty? ? master : slaves
|
|
180
|
-
end
|
|
181
134
|
end
|
|
182
135
|
end
|
data/lib/mongoid/collections.rb
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
# encoding: utf-8
|
|
2
2
|
module Mongoid #:nodoc
|
|
3
|
+
|
|
3
4
|
# The collections module is used for providing functionality around setting
|
|
4
5
|
# up and updating collections.
|
|
5
6
|
module Collections
|
|
6
7
|
extend ActiveSupport::Concern
|
|
8
|
+
|
|
7
9
|
included do
|
|
8
10
|
cattr_accessor :_collection, :collection_name
|
|
9
11
|
self.collection_name = self.name.collectionize
|
|
@@ -12,11 +14,15 @@ module Mongoid #:nodoc
|
|
|
12
14
|
end
|
|
13
15
|
|
|
14
16
|
module ClassMethods #:nodoc:
|
|
17
|
+
|
|
15
18
|
# Returns the collection associated with this +Document+. If the
|
|
16
19
|
# document is embedded, there will be no collection associated
|
|
17
|
-
# with it.
|
|
20
|
+
# with it unless it's in a cyclic relation.
|
|
18
21
|
#
|
|
19
|
-
#
|
|
22
|
+
# @example Get the collection.
|
|
23
|
+
# Model.collection
|
|
24
|
+
#
|
|
25
|
+
# @return [ Collection ] The Mongoid collection wrapper.
|
|
20
26
|
def collection
|
|
21
27
|
raise Errors::InvalidCollection.new(self) if embedded? && !cyclic
|
|
22
28
|
self._collection || set_collection
|
|
@@ -25,44 +31,41 @@ module Mongoid #:nodoc
|
|
|
25
31
|
|
|
26
32
|
# Return the database associated with this collection.
|
|
27
33
|
#
|
|
28
|
-
#
|
|
34
|
+
# @example Get the database object.
|
|
35
|
+
# Model.db
|
|
29
36
|
#
|
|
30
|
-
#
|
|
37
|
+
# @return [ Mongo::DB ] The Mongo daatabase object.
|
|
31
38
|
def db
|
|
32
39
|
collection.db
|
|
33
40
|
end
|
|
34
41
|
|
|
35
42
|
# Convenience method for getting index information from the collection.
|
|
36
43
|
#
|
|
37
|
-
#
|
|
44
|
+
# @example Get the index information from the collection.
|
|
45
|
+
# Model.index_information
|
|
38
46
|
#
|
|
39
|
-
#
|
|
47
|
+
# @return [ Array ] The collection index information.
|
|
40
48
|
def index_information
|
|
41
49
|
collection.index_information
|
|
42
50
|
end
|
|
43
51
|
|
|
44
|
-
# The MongoDB logger is not exposed through the driver to be changed
|
|
45
|
-
# after initialization of the connection, this is a hacky way around that
|
|
46
|
-
# if logging needs to be changed at runtime.
|
|
47
|
-
#
|
|
48
|
-
# Example:
|
|
49
|
-
#
|
|
50
|
-
# <tt>Person.logger = Logger.new($stdout)</tt>
|
|
51
|
-
def logger=(logger)
|
|
52
|
-
db.connection.instance_variable_set(:@logger, logger)
|
|
53
|
-
end
|
|
54
|
-
|
|
55
52
|
# Macro for setting the collection name to store in.
|
|
56
53
|
#
|
|
57
|
-
#
|
|
58
|
-
#
|
|
59
|
-
# <tt>Person.store_in :population</tt>
|
|
54
|
+
# @example Store in a separate collection than the default.
|
|
55
|
+
# Model.store_in :population
|
|
60
56
|
def store_in(name)
|
|
61
57
|
self.collection_name = name.to_s
|
|
62
58
|
set_collection
|
|
63
59
|
end
|
|
64
60
|
|
|
65
61
|
protected
|
|
62
|
+
|
|
63
|
+
# Set the collection on the class.
|
|
64
|
+
#
|
|
65
|
+
# @example Set the collection.
|
|
66
|
+
# Model.set_collection
|
|
67
|
+
#
|
|
68
|
+
# @return [ Collection ] The Mongoid collection wrapper.
|
|
66
69
|
def set_collection
|
|
67
70
|
self._collection = Mongoid::Collection.new(self, self.collection_name)
|
|
68
71
|
end
|