mongoid 2.0.0.rc.7 → 2.0.0.rc.8
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/config/locales/en.yml +3 -0
- data/lib/config/locales/id.yml +46 -0
- data/lib/config/locales/ja.yml +40 -0
- data/lib/config/locales/vi.yml +45 -0
- data/lib/mongoid.rb +5 -3
- data/lib/mongoid/attributes.rb +24 -63
- data/lib/mongoid/attributes/processing.rb +5 -2
- data/lib/mongoid/callbacks.rb +10 -0
- data/lib/mongoid/collection.rb +24 -0
- data/lib/mongoid/collections/master.rb +14 -6
- data/lib/mongoid/collections/operations.rb +1 -1
- data/lib/mongoid/collections/retry.rb +39 -0
- data/lib/mongoid/collections/slaves.rb +26 -10
- data/lib/mongoid/components.rb +4 -4
- data/lib/mongoid/config.rb +6 -3
- data/lib/mongoid/contexts.rb +0 -1
- data/lib/mongoid/contexts/enumerable.rb +19 -7
- data/lib/mongoid/contexts/mongo.rb +9 -5
- data/lib/mongoid/copyable.rb +10 -8
- data/lib/mongoid/criteria.rb +83 -61
- data/lib/mongoid/criterion/builder.rb +34 -0
- data/lib/mongoid/criterion/creational.rb +2 -2
- data/lib/mongoid/criterion/exclusion.rb +58 -32
- data/lib/mongoid/criterion/inclusion.rb +49 -10
- data/lib/mongoid/criterion/optional.rb +1 -1
- data/lib/mongoid/criterion/selector.rb +80 -11
- data/lib/mongoid/cursor.rb +6 -1
- data/lib/mongoid/default_scope.rb +27 -19
- data/lib/mongoid/document.rb +26 -1
- data/lib/mongoid/errors.rb +1 -0
- data/lib/mongoid/errors/mixed_relations.rb +37 -0
- data/lib/mongoid/extensions/object_id/conversions.rb +7 -4
- data/lib/mongoid/factory.rb +1 -1
- data/lib/mongoid/field.rb +47 -30
- data/lib/mongoid/fields.rb +9 -2
- data/lib/mongoid/finders.rb +15 -49
- data/lib/mongoid/identity.rb +6 -4
- data/lib/mongoid/keys.rb +85 -31
- data/lib/mongoid/multi_parameter_attributes.rb +2 -2
- data/lib/mongoid/named_scope.rb +129 -28
- data/lib/mongoid/observer.rb +36 -0
- data/lib/mongoid/paranoia.rb +3 -3
- data/lib/mongoid/paths.rb +1 -1
- data/lib/mongoid/persistence.rb +2 -0
- data/lib/mongoid/persistence/atomic.rb +88 -0
- data/lib/mongoid/persistence/atomic/add_to_set.rb +30 -0
- data/lib/mongoid/persistence/atomic/inc.rb +28 -0
- data/lib/mongoid/persistence/atomic/operation.rb +44 -0
- data/lib/mongoid/persistence/atomic/pull_all.rb +33 -0
- data/lib/mongoid/persistence/atomic/push.rb +28 -0
- data/lib/mongoid/railtie.rb +13 -1
- data/lib/mongoid/relations.rb +1 -0
- data/lib/mongoid/relations/accessors.rb +20 -2
- data/lib/mongoid/relations/builders/embedded/one.rb +1 -0
- data/lib/mongoid/relations/builders/nested_attributes/many.rb +17 -6
- data/lib/mongoid/relations/builders/referenced/many.rb +2 -1
- data/lib/mongoid/relations/builders/referenced/one.rb +1 -0
- data/lib/mongoid/relations/embedded/atomic.rb +86 -0
- data/lib/mongoid/relations/embedded/atomic/operation.rb +63 -0
- data/lib/mongoid/relations/embedded/atomic/pull.rb +65 -0
- data/lib/mongoid/relations/embedded/atomic/push_all.rb +59 -0
- data/lib/mongoid/relations/embedded/atomic/set.rb +61 -0
- data/lib/mongoid/relations/embedded/atomic/unset.rb +41 -0
- data/lib/mongoid/relations/embedded/many.rb +57 -25
- data/lib/mongoid/relations/macros.rb +6 -4
- data/lib/mongoid/relations/many.rb +51 -10
- data/lib/mongoid/relations/metadata.rb +4 -2
- data/lib/mongoid/relations/proxy.rb +39 -24
- data/lib/mongoid/relations/referenced/many.rb +47 -26
- data/lib/mongoid/relations/referenced/many_to_many.rb +47 -14
- data/lib/mongoid/relations/referenced/one.rb +14 -0
- data/lib/mongoid/sharding.rb +51 -0
- data/lib/mongoid/state.rb +3 -2
- data/lib/mongoid/timestamps.rb +5 -29
- data/lib/mongoid/timestamps/created.rb +31 -0
- data/lib/mongoid/timestamps/updated.rb +33 -0
- data/lib/mongoid/validations.rb +10 -3
- data/lib/mongoid/validations/referenced.rb +58 -0
- data/lib/mongoid/version.rb +1 -1
- data/lib/mongoid/versioning.rb +67 -5
- data/lib/rails/generators/mongoid/model/templates/model.rb +2 -0
- data/lib/rails/generators/mongoid/observer/observer_generator.rb +17 -0
- data/lib/rails/generators/mongoid/observer/templates/observer.rb +4 -0
- data/lib/rails/generators/mongoid_generator.rb +10 -1
- data/lib/rails/mongoid.rb +1 -0
- metadata +29 -8
- data/lib/mongoid/contexts/ids.rb +0 -25
- data/lib/mongoid/modifiers.rb +0 -24
- data/lib/mongoid/modifiers/command.rb +0 -18
- data/lib/mongoid/modifiers/inc.rb +0 -24
data/lib/config/locales/en.yml
CHANGED
@@ -40,3 +40,6 @@ en:
|
|
40
40
|
unsaved_document:
|
41
41
|
You cannot call create or create! through a relational association
|
42
42
|
relation (%{document}) who's parent (%{base}) is not already saved.
|
43
|
+
mixed_relations:
|
44
|
+
Referencing a(n) %{embedded} document from the %{root} document via a
|
45
|
+
relational association is not allowed since the %{embedded} is embedded.
|
@@ -0,0 +1,46 @@
|
|
1
|
+
id:
|
2
|
+
mongoid:
|
3
|
+
errors:
|
4
|
+
messages:
|
5
|
+
taken:
|
6
|
+
telah digunakan
|
7
|
+
document_not_found:
|
8
|
+
Dokumen tidak ditemukan untuk kelas %{klass} dengan id %{identifiers}.
|
9
|
+
invalid_database:
|
10
|
+
Database harus Mongo::DB, bukan %{name}.
|
11
|
+
invalid_type:
|
12
|
+
Field didefinisikan sebagai %{klass}, tetapi menerima %{other} dengan
|
13
|
+
value %{value}.
|
14
|
+
unsupported_version:
|
15
|
+
MongoDB %{version} tidak di support, silakan upgrade
|
16
|
+
ke %{mongo_version}.
|
17
|
+
validations:
|
18
|
+
Validasi gagal - %{errors}.
|
19
|
+
invalid_collection:
|
20
|
+
Akses terhadap koleksi %{klass} tidak diperbolehkan karena merupakan
|
21
|
+
embedded document, silakan akses dokumen melalui root document.
|
22
|
+
invalid_field:
|
23
|
+
Mendefinisikan field dengan nama %{name} tidak diperbolehkan. Jangan
|
24
|
+
mendefinisikan field yang konflik dengan nama atribut atau method
|
25
|
+
internal Mongoid. Gunakan Document#instance_methods untuk melihat nama
|
26
|
+
yang termasuk.
|
27
|
+
too_many_nested_attribute_records:
|
28
|
+
Penerimaan nested attribut untuk %{association} dibatasi %{limit}
|
29
|
+
record.
|
30
|
+
embedded_in_must_have_inverse_of:
|
31
|
+
Opsi untuk asosiasi embedded_in harus memiliki inverse_of.
|
32
|
+
dependent_only_references_one_or_many:
|
33
|
+
Opsi dependent => destroy|delete yang diberikan hanya valid untuk
|
34
|
+
asosiasi references_one atau references_many.
|
35
|
+
association_cant_have_inverse_of:
|
36
|
+
Mendefinisikan inverse_of pada asosiasi ini tidak diperbolehkan. Hanya
|
37
|
+
gunakan opsi ini pada embedded_in atau references_many sebagai array.
|
38
|
+
calling_document_find_with_nil_is_invalid:
|
39
|
+
Memanggil Document#find dengan nil adalah tidak valid
|
40
|
+
unsaved_document:
|
41
|
+
Anda tidak dapat memanggil create atau create! melalui asosiasi
|
42
|
+
relational (%{document}) yang parent-nya (%{base}) belum disimpan.
|
43
|
+
mixed_relations:
|
44
|
+
Mereferensi sebuah %{embedded} document dari %{root} dokumen melalui
|
45
|
+
asosiasi relasional tidak diperbolehkan karena %{embedded}
|
46
|
+
adalah dokumen embedded.
|
@@ -0,0 +1,40 @@
|
|
1
|
+
ja:
|
2
|
+
mongoid:
|
3
|
+
errors:
|
4
|
+
messages:
|
5
|
+
taken:
|
6
|
+
はすでに使用されています
|
7
|
+
document_not_found:
|
8
|
+
次のDocumentは見つかりませんでした。class %{klass} with id(s) %{identifiers}
|
9
|
+
invalid_database:
|
10
|
+
次のDatabaseは利用できませんでした。%{name}
|
11
|
+
invalid_type:
|
12
|
+
このフィールドは %{klass} で定義されていますが、%{other}に対し %{value} という値が送信されました。
|
13
|
+
unsupported_version:
|
14
|
+
MongoDB %{version} はサポートしていません。 %{mongo_version}にアップグレードをしてください。
|
15
|
+
validations:
|
16
|
+
バリデーションエラー - %{errors}
|
17
|
+
invalid_collection:
|
18
|
+
次のコレクションにアクセスしようしました。 %{klass}
|
19
|
+
しかし、このコレクションアクセスするためにはルートドキュメントから
|
20
|
+
アクセスする必要があります。
|
21
|
+
invalid_field:
|
22
|
+
次のフィールド名は利用できません。 %{name}
|
23
|
+
Mongoidが内部利用している属性やメソッドとコンフリクトしている名前は利用しないでください。
|
24
|
+
これらの名前を取得するためには、Document#instance_methods を参照してください。
|
25
|
+
too_many_nested_attribute_records:
|
26
|
+
次のネストされた属性は、 %{limit} 以下に制限されています。{%association}
|
27
|
+
embedded_in_must_have_inverse_of:
|
28
|
+
embedded_in を利用するときには inverse_of を定義する必要があります。
|
29
|
+
dependent_only_references_one_or_many:
|
30
|
+
dependent => destroy|delete を利用するには、references_one もしくは references_many で定義されている必要があります。
|
31
|
+
association_cant_have_inverse_of:
|
32
|
+
現在の定義では、inverse_of が利用できません。
|
33
|
+
このオプションは embedded_in もしくは 配列として定義された references_many のみで利用できます。
|
34
|
+
calling_document_find_with_nil_is_invalid:
|
35
|
+
Document#find を nil で呼び出すことは出来ません
|
36
|
+
unsaved_document:
|
37
|
+
リレーションを通して create もしくは create! を呼び出すことが出来ませんでした。
|
38
|
+
%{document} の 親ドキュメント (%{base}) は保存されていません。
|
39
|
+
mixed_relations:
|
40
|
+
%{embedded} ドキュメント に対して %{root} ドキュメントからリレーション関係を用いて、参照することはできません。
|
@@ -0,0 +1,45 @@
|
|
1
|
+
vi:
|
2
|
+
mongoid:
|
3
|
+
errors:
|
4
|
+
messages:
|
5
|
+
taken:
|
6
|
+
đã bị lấy
|
7
|
+
document_not_found:
|
8
|
+
Không tìm thấy văn bản cho lớp %{klass} với định danh %{identifiers}.
|
9
|
+
invalid_database:
|
10
|
+
Cơ sở dữ liệu nên là Mongo::DB, không phải %{name}.
|
11
|
+
invalid_type:
|
12
|
+
Trường được định nghĩa là một %{klass}, nhưng nhận được %{other} với
|
13
|
+
giá trị %{value}.
|
14
|
+
unsupported_version:
|
15
|
+
MongoDB %{version} không được hỗ trợ, xin vui lòng nâng cấp
|
16
|
+
lên %{mongo_version}.
|
17
|
+
validations:
|
18
|
+
Kiểm định không thành công - %{errors}.
|
19
|
+
invalid_collection:
|
20
|
+
Không được phép truy cập tập văn bản cuả %{klass} vì đây
|
21
|
+
là một văn bản nhúng, xin vui lòng truy cập tập văn bản từ
|
22
|
+
văn bản gốc.
|
23
|
+
invalid_field:
|
24
|
+
Không được phép định nghĩa tên trường là %{name}. Không định nghĩa
|
25
|
+
tên trường trùng với các thuộc tính có sẵn hoặc các tên hàm cuả Mongoid.
|
26
|
+
Sử dụng Use Document#instance_methods để xem danh sách tên đã được dùng.
|
27
|
+
too_many_nested_attribute_records:
|
28
|
+
Số lượng các thuộc tính lồng cho %{association} bị giới hạn
|
29
|
+
ở %{limit} bản ghi.
|
30
|
+
embedded_in_must_have_inverse_of:
|
31
|
+
Phải cho thêm inverse_of vào lực chọn cuả kết hợp embedded_in.
|
32
|
+
dependent_only_references_one_or_many:
|
33
|
+
Lựa chọn dependent => destroy|delete
|
34
|
+
chỉ có hiệu lực với kết hợp references_one hoặc references_many.
|
35
|
+
association_cant_have_inverse_of:
|
36
|
+
Không được phép định nghĩa inverse_of cho kết hợp này. Chỉ
|
37
|
+
sử dụng lựa chọn này cho embedded_in or references_many as array.
|
38
|
+
calling_document_find_with_nil_is_invalid:
|
39
|
+
Không được phép gọi Document#find với giá trị nil
|
40
|
+
unsaved_document:
|
41
|
+
Bạn không được gọi create hoặc create! thông qua một quan hệ kết hợp
|
42
|
+
mà văn bản gốc (%{base}) chưa được lưu.
|
43
|
+
mixed_relations:
|
44
|
+
Không được phép tham chiếu một văn bản %{embedded} từ văn bản %{root} thông qua một
|
45
|
+
quan hệ kết hợp vì %{embedded} là kiểu nhúng.
|
data/lib/mongoid.rb
CHANGED
@@ -42,6 +42,7 @@ require "mongo"
|
|
42
42
|
require "mongoid/errors"
|
43
43
|
require "mongoid/extensions"
|
44
44
|
require "mongoid/safe"
|
45
|
+
require "mongoid/relations"
|
45
46
|
require "mongoid/atomicity"
|
46
47
|
require "mongoid/attributes"
|
47
48
|
require "mongoid/callbacks"
|
@@ -68,17 +69,17 @@ require "mongoid/json"
|
|
68
69
|
require "mongoid/keys"
|
69
70
|
require "mongoid/logger"
|
70
71
|
require "mongoid/matchers"
|
71
|
-
require "mongoid/modifiers"
|
72
72
|
require "mongoid/multi_parameter_attributes"
|
73
73
|
require "mongoid/multi_database"
|
74
74
|
require "mongoid/named_scope"
|
75
75
|
require "mongoid/nested_attributes"
|
76
|
+
require "mongoid/observer"
|
76
77
|
require "mongoid/paths"
|
77
78
|
require "mongoid/persistence"
|
78
|
-
require "mongoid/relations"
|
79
79
|
require "mongoid/safety"
|
80
80
|
require "mongoid/scope"
|
81
81
|
require "mongoid/serialization"
|
82
|
+
require "mongoid/sharding"
|
82
83
|
require "mongoid/state"
|
83
84
|
require "mongoid/timestamps"
|
84
85
|
require "mongoid/validations"
|
@@ -129,7 +130,8 @@ module Mongoid #:nodoc
|
|
129
130
|
#
|
130
131
|
# @example Delegate the configuration methods.
|
131
132
|
# Mongoid.database = Mongo::Connection.new.db("test")
|
132
|
-
Mongoid::Config.public_instance_methods(false)
|
133
|
+
(Mongoid::Config.public_instance_methods(false) +
|
134
|
+
ActiveModel::Observing::ClassMethods.public_instance_methods(false)).each do |name|
|
133
135
|
(class << self; self; end).class_eval <<-EOT
|
134
136
|
def #{name}(*args)
|
135
137
|
configure.send("#{name}", *args)
|
data/lib/mongoid/attributes.rb
CHANGED
@@ -8,34 +8,6 @@ module Mongoid #:nodoc:
|
|
8
8
|
module Attributes
|
9
9
|
include Processing
|
10
10
|
|
11
|
-
# Returns the object type. This corresponds to the name of the class that
|
12
|
-
# this document is, which is used in determining the class to
|
13
|
-
# instantiate in various cases.
|
14
|
-
#
|
15
|
-
# @example Get the type.
|
16
|
-
# person._type
|
17
|
-
#
|
18
|
-
# @return [ String ] The name of the class the document is.
|
19
|
-
#
|
20
|
-
# @since 1.0.0
|
21
|
-
def _type
|
22
|
-
@attributes["_type"]
|
23
|
-
end
|
24
|
-
|
25
|
-
# Set the type of the document. This should be the name of the class.
|
26
|
-
#
|
27
|
-
# @example Set the type
|
28
|
-
# person._type = "Person"
|
29
|
-
#
|
30
|
-
# @param [ String ] new_type The name of the class.
|
31
|
-
#
|
32
|
-
# @return [ String ] the new type.
|
33
|
-
#
|
34
|
-
# @since 1.0.0
|
35
|
-
def _type=(new_type)
|
36
|
-
@attributes["_type"] = new_type
|
37
|
-
end
|
38
|
-
|
39
11
|
# Determine if an attribute is present.
|
40
12
|
#
|
41
13
|
# @example Is the attribute present?
|
@@ -50,35 +22,6 @@ module Mongoid #:nodoc:
|
|
50
22
|
!read_attribute(name).blank?
|
51
23
|
end
|
52
24
|
|
53
|
-
# Get the id associated with this object. This will pull the _id value out
|
54
|
-
# of the attributes.
|
55
|
-
#
|
56
|
-
# @example Get the id.
|
57
|
-
# person.id
|
58
|
-
#
|
59
|
-
# @return [ BSON::ObjectId, String ] The id of the document.
|
60
|
-
#
|
61
|
-
# @since 1.0.0
|
62
|
-
def id
|
63
|
-
@attributes["_id"]
|
64
|
-
end
|
65
|
-
alias :_id :id
|
66
|
-
|
67
|
-
# Set the id of the document to a new one.
|
68
|
-
#
|
69
|
-
# @example Set the id.
|
70
|
-
# person.id = BSON::ObjectId.new
|
71
|
-
#
|
72
|
-
# @param [ BSON::ObjectId, String ] new_id The new id.
|
73
|
-
#
|
74
|
-
# @return [ BSON::ObjectId, String ] The new id.
|
75
|
-
#
|
76
|
-
# @since 1.0.0
|
77
|
-
def id=(new_id)
|
78
|
-
@attributes["_id"] = _id_type.set(new_id)
|
79
|
-
end
|
80
|
-
alias :_id= :id=
|
81
|
-
|
82
25
|
# Read a value from the document attributes. If the value does not exist
|
83
26
|
# it will return nil.
|
84
27
|
#
|
@@ -163,10 +106,11 @@ module Mongoid #:nodoc:
|
|
163
106
|
# person.attributes = { :title => "Mr." }
|
164
107
|
#
|
165
108
|
# @param [ Hash ] attrs The new attributes to set.
|
109
|
+
# @param [ Boolean ] guard_protected_attributes False to skip mass assignment protection.
|
166
110
|
#
|
167
111
|
# @since 1.0.0
|
168
|
-
def write_attributes(attrs = nil)
|
169
|
-
process(attrs) do |document|
|
112
|
+
def write_attributes(attrs = nil, guard_protected_attributes = true)
|
113
|
+
process(attrs, guard_protected_attributes) do |document|
|
170
114
|
document.identify if new? && id.blank?
|
171
115
|
end
|
172
116
|
end
|
@@ -182,12 +126,29 @@ module Mongoid #:nodoc:
|
|
182
126
|
# @return [ Hash ] The default values for each field.
|
183
127
|
#
|
184
128
|
# @since 1.0.0
|
129
|
+
#
|
130
|
+
# @raise [ RuntimeError ] Always
|
131
|
+
# @since 2.0.0.rc.8
|
185
132
|
def default_attributes
|
186
|
-
|
187
|
-
|
188
|
-
|
133
|
+
raise "default_attributes is no longer valid. Plase use: apply_default_attributes."
|
134
|
+
end
|
135
|
+
|
136
|
+
# Set any missing default values in the attributes.
|
137
|
+
#
|
138
|
+
# @example Get the raw attributes after defaults have been applied.
|
139
|
+
# person.apply_default_attributes
|
140
|
+
#
|
141
|
+
# @return [ Hash ] The raw attributes.
|
142
|
+
#
|
143
|
+
# @since 2.0.0.rc.8
|
144
|
+
def apply_default_attributes
|
145
|
+
(@attributes ||= {}).tap do |h|
|
146
|
+
defaults.each_pair do |key, val|
|
147
|
+
unless h.has_key?(key)
|
148
|
+
h[key] = val.respond_to?(:call) ? typed_value_for(key, val.call) : val
|
149
|
+
end
|
150
|
+
end
|
189
151
|
end
|
190
|
-
default_values || {}
|
191
152
|
end
|
192
153
|
|
193
154
|
# Used for allowing accessor methods for dynamic attributes.
|
@@ -14,10 +14,13 @@ module Mongoid #:nodoc:
|
|
14
14
|
# person.process(:title => "sir", :age => 40)
|
15
15
|
#
|
16
16
|
# @param [ Hash ] attrs The attributes to set.
|
17
|
+
# @param [ Boolean ] guard_protected_attributes False to skip mass assignment protection.
|
17
18
|
#
|
18
19
|
# @since 2.0.0.rc.7
|
19
|
-
def process(attrs = nil)
|
20
|
-
|
20
|
+
def process(attrs = nil, guard_protected_attributes = true)
|
21
|
+
attrs ||= {}
|
22
|
+
attrs = sanitize_for_mass_assignment(attrs) if guard_protected_attributes
|
23
|
+
attrs.each_pair do |key, value|
|
21
24
|
next if pending_attribute?(key, value)
|
22
25
|
process_attribute(key, value)
|
23
26
|
end
|
data/lib/mongoid/callbacks.rb
CHANGED
@@ -2,6 +2,16 @@
|
|
2
2
|
module Mongoid #:nodoc:
|
3
3
|
module Callbacks
|
4
4
|
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
CALLBACKS = [
|
7
|
+
:before_validation, :after_validation,
|
8
|
+
:after_initialize,
|
9
|
+
:before_create, :around_create, :after_create,
|
10
|
+
:before_destroy, :around_destroy, :after_destroy,
|
11
|
+
:before_save, :around_save, :after_save,
|
12
|
+
:before_update, :around_update, :after_update,
|
13
|
+
]
|
14
|
+
|
5
15
|
included do
|
6
16
|
extend ActiveModel::Callbacks
|
7
17
|
include ActiveModel::Validations::Callbacks
|
data/lib/mongoid/collection.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
require "mongoid/collections/retry"
|
2
3
|
require "mongoid/collections/operations"
|
3
4
|
require "mongoid/collections/cyclic_iterator"
|
4
5
|
require "mongoid/collections/master"
|
@@ -122,6 +123,29 @@ module Mongoid #:nodoc
|
|
122
123
|
@slaves ||= Collections::Slaves.new(slaves, @name)
|
123
124
|
end
|
124
125
|
|
126
|
+
# Updates one or more documents in the collection.
|
127
|
+
#
|
128
|
+
# @example Update documents.
|
129
|
+
# collection.update(
|
130
|
+
# { "_id" => BSON::OjectId.new },
|
131
|
+
# { "$push" => { "addresses" => { "_id" => "street" } } },
|
132
|
+
# :safe => true
|
133
|
+
# )
|
134
|
+
#
|
135
|
+
# @param [ Hash ] selector The document selector.
|
136
|
+
# @param [ Hash ] document The modifier.
|
137
|
+
# @param [ Hash ] options The options.
|
138
|
+
#
|
139
|
+
# @since 2.0.0
|
140
|
+
def update(selector, document, options = {})
|
141
|
+
updater = Thread.current[:mongoid_atomic_update]
|
142
|
+
if updater
|
143
|
+
updater.consume(selector, document, options)
|
144
|
+
else
|
145
|
+
master.update(selector, document, options)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
125
149
|
protected
|
126
150
|
|
127
151
|
# Determine if the read is going to the master or the slaves.
|
@@ -1,26 +1,34 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
module Mongoid #:nodoc:
|
3
3
|
module Collections #:nodoc:
|
4
|
+
|
5
|
+
# This class wraps the MongoDB master database.
|
4
6
|
class Master
|
7
|
+
include Mongoid::Collections::Retry
|
5
8
|
|
6
9
|
attr_reader :collection
|
7
10
|
|
8
11
|
# All read and write operations should delegate to the master connection.
|
9
12
|
# These operations mimic the methods on a Mongo:Collection.
|
10
13
|
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
# <tt>collection.save({ :name => "Al" })</tt>
|
14
|
+
# @example Proxy the driver save.
|
15
|
+
# collection.save({ :name => "Al" })
|
14
16
|
Operations::ALL.each do |name|
|
15
|
-
define_method(name)
|
17
|
+
define_method(name) do |*args|
|
18
|
+
retry_on_connection_failure do
|
19
|
+
collection.send(name, *args)
|
20
|
+
end
|
21
|
+
end
|
16
22
|
end
|
17
23
|
|
18
24
|
# Create the new database writer. Will create a collection from the
|
19
25
|
# master database.
|
20
26
|
#
|
21
|
-
#
|
27
|
+
# @example Create a new wrapped master.
|
28
|
+
# Master.new(db, "testing")
|
22
29
|
#
|
23
|
-
#
|
30
|
+
# @param [ Mongo::DB ] master The master database.
|
31
|
+
# @param [ String ] name The name of the database.
|
24
32
|
def initialize(master, name)
|
25
33
|
@collection = master.collection(name)
|
26
34
|
end
|
@@ -36,7 +36,7 @@ module Mongoid #:nodoc:
|
|
36
36
|
|
37
37
|
# Convenience constant for getting back all collection operations.
|
38
38
|
ALL = (READ + WRITE)
|
39
|
-
PROXIED = ALL - [ :find, :find_one, :map_reduce, :mapreduce ]
|
39
|
+
PROXIED = ALL - [ :find, :find_one, :map_reduce, :mapreduce, :update ]
|
40
40
|
end
|
41
41
|
end
|
42
42
|
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Collections #:nodoc:
|
4
|
+
|
5
|
+
# Provides behaviour for retrying commands on connection failure.
|
6
|
+
module Retry
|
7
|
+
|
8
|
+
# Retries command on connection failures.
|
9
|
+
#
|
10
|
+
# This is useful when using replica sets. When a primary server wents
|
11
|
+
# down and a command is issued, the driver will raise a
|
12
|
+
# Mongo::ConnectionFailure. We wait a little bit, because nodes are
|
13
|
+
# electing themselves, and then retry the given command.
|
14
|
+
#
|
15
|
+
# By setting Mongoid.max_retries_on_connection_failure to a value of 0,
|
16
|
+
# no attempt will be made, immediately raising connection failure.
|
17
|
+
# Otherwise it will attempt to make the specified number of retries
|
18
|
+
# and then raising the exception to clients.
|
19
|
+
#
|
20
|
+
# @example Retry the command.
|
21
|
+
# retry_on_connection_failure do
|
22
|
+
# collection.send(name, *args)
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# @since 2.0.0
|
26
|
+
def retry_on_connection_failure
|
27
|
+
retries = 0
|
28
|
+
begin
|
29
|
+
yield
|
30
|
+
rescue Mongo::ConnectionFailure => ex
|
31
|
+
retries += 1
|
32
|
+
raise ex if retries > Mongoid.max_retries_on_connection_failure
|
33
|
+
Kernel.sleep(0.5)
|
34
|
+
retry
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|