mongoid-braxton 2.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.rdoc +50 -0
- data/Rakefile +51 -0
- data/lib/config/locales/bg.yml +41 -0
- data/lib/config/locales/de.yml +41 -0
- data/lib/config/locales/en.yml +45 -0
- data/lib/config/locales/es.yml +41 -0
- data/lib/config/locales/fr.yml +42 -0
- data/lib/config/locales/hu.yml +44 -0
- data/lib/config/locales/id.yml +46 -0
- data/lib/config/locales/it.yml +39 -0
- data/lib/config/locales/ja.yml +40 -0
- data/lib/config/locales/kr.yml +65 -0
- data/lib/config/locales/nl.yml +39 -0
- data/lib/config/locales/pl.yml +39 -0
- data/lib/config/locales/pt-BR.yml +40 -0
- data/lib/config/locales/pt.yml +40 -0
- data/lib/config/locales/ro.yml +46 -0
- data/lib/config/locales/ru.yml +41 -0
- data/lib/config/locales/sv.yml +40 -0
- data/lib/config/locales/vi.yml +45 -0
- data/lib/config/locales/zh-CN.yml +33 -0
- data/lib/mongoid.rb +140 -0
- data/lib/mongoid/atomicity.rb +111 -0
- data/lib/mongoid/attributes.rb +185 -0
- data/lib/mongoid/attributes/processing.rb +145 -0
- data/lib/mongoid/callbacks.rb +23 -0
- data/lib/mongoid/collection.rb +137 -0
- data/lib/mongoid/collections.rb +71 -0
- data/lib/mongoid/collections/master.rb +37 -0
- data/lib/mongoid/collections/operations.rb +42 -0
- data/lib/mongoid/collections/retry.rb +39 -0
- data/lib/mongoid/components.rb +45 -0
- data/lib/mongoid/config.rb +349 -0
- data/lib/mongoid/config/database.rb +167 -0
- data/lib/mongoid/config/replset_database.rb +78 -0
- data/lib/mongoid/contexts.rb +19 -0
- data/lib/mongoid/contexts/enumerable.rb +275 -0
- data/lib/mongoid/contexts/enumerable/sort.rb +43 -0
- data/lib/mongoid/contexts/mongo.rb +345 -0
- data/lib/mongoid/copyable.rb +46 -0
- data/lib/mongoid/criteria.rb +357 -0
- data/lib/mongoid/criterion/builder.rb +34 -0
- data/lib/mongoid/criterion/complex.rb +34 -0
- data/lib/mongoid/criterion/creational.rb +34 -0
- data/lib/mongoid/criterion/exclusion.rb +108 -0
- data/lib/mongoid/criterion/inclusion.rb +198 -0
- data/lib/mongoid/criterion/inspection.rb +22 -0
- data/lib/mongoid/criterion/optional.rb +193 -0
- data/lib/mongoid/criterion/selector.rb +143 -0
- data/lib/mongoid/criterion/unconvertable.rb +20 -0
- data/lib/mongoid/cursor.rb +86 -0
- data/lib/mongoid/default_scope.rb +36 -0
- data/lib/mongoid/dirty.rb +253 -0
- data/lib/mongoid/document.rb +284 -0
- data/lib/mongoid/errors.rb +13 -0
- data/lib/mongoid/errors/document_not_found.rb +29 -0
- data/lib/mongoid/errors/invalid_collection.rb +19 -0
- data/lib/mongoid/errors/invalid_database.rb +20 -0
- data/lib/mongoid/errors/invalid_field.rb +19 -0
- data/lib/mongoid/errors/invalid_options.rb +16 -0
- data/lib/mongoid/errors/invalid_type.rb +26 -0
- data/lib/mongoid/errors/mixed_relations.rb +37 -0
- data/lib/mongoid/errors/mongoid_error.rb +27 -0
- data/lib/mongoid/errors/too_many_nested_attribute_records.rb +21 -0
- data/lib/mongoid/errors/unsaved_document.rb +23 -0
- data/lib/mongoid/errors/unsupported_version.rb +21 -0
- data/lib/mongoid/errors/validations.rb +24 -0
- data/lib/mongoid/extensions.rb +123 -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 +25 -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/conversions.rb +19 -0
- data/lib/mongoid/extensions/hash/criteria_helpers.rb +22 -0
- data/lib/mongoid/extensions/hash/scoping.rb +12 -0
- data/lib/mongoid/extensions/integer/conversions.rb +20 -0
- data/lib/mongoid/extensions/nil/collectionization.rb +12 -0
- data/lib/mongoid/extensions/object/checks.rb +32 -0
- data/lib/mongoid/extensions/object/conversions.rb +25 -0
- data/lib/mongoid/extensions/object/reflections.rb +17 -0
- data/lib/mongoid/extensions/object/yoda.rb +27 -0
- data/lib/mongoid/extensions/object_id/conversions.rb +96 -0
- data/lib/mongoid/extensions/proc/scoping.rb +12 -0
- data/lib/mongoid/extensions/range/conversions.rb +25 -0
- data/lib/mongoid/extensions/set/conversions.rb +20 -0
- data/lib/mongoid/extensions/string/conversions.rb +34 -0
- data/lib/mongoid/extensions/string/inflections.rb +97 -0
- data/lib/mongoid/extensions/symbol/conversions.rb +21 -0
- data/lib/mongoid/extensions/symbol/inflections.rb +40 -0
- data/lib/mongoid/extensions/time_conversions.rb +38 -0
- data/lib/mongoid/extensions/true_class/equality.rb +13 -0
- data/lib/mongoid/extras.rb +42 -0
- data/lib/mongoid/factory.rb +37 -0
- data/lib/mongoid/field.rb +162 -0
- data/lib/mongoid/fields.rb +183 -0
- data/lib/mongoid/finders.rb +127 -0
- data/lib/mongoid/hierarchy.rb +85 -0
- data/lib/mongoid/identity.rb +92 -0
- data/lib/mongoid/indexes.rb +38 -0
- data/lib/mongoid/inspection.rb +54 -0
- data/lib/mongoid/javascript.rb +21 -0
- data/lib/mongoid/javascript/functions.yml +37 -0
- data/lib/mongoid/json.rb +16 -0
- data/lib/mongoid/keys.rb +131 -0
- data/lib/mongoid/logger.rb +18 -0
- data/lib/mongoid/matchers.rb +32 -0
- data/lib/mongoid/matchers/all.rb +11 -0
- data/lib/mongoid/matchers/default.rb +70 -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/or.rb +30 -0
- data/lib/mongoid/matchers/size.rb +11 -0
- data/lib/mongoid/matchers/strategies.rb +63 -0
- data/lib/mongoid/multi_database.rb +11 -0
- data/lib/mongoid/multi_parameter_attributes.rb +82 -0
- data/lib/mongoid/named_scope.rb +137 -0
- data/lib/mongoid/nested_attributes.rb +51 -0
- data/lib/mongoid/observer.rb +67 -0
- data/lib/mongoid/paranoia.rb +103 -0
- data/lib/mongoid/paths.rb +61 -0
- data/lib/mongoid/persistence.rb +240 -0
- data/lib/mongoid/persistence/atomic.rb +88 -0
- data/lib/mongoid/persistence/atomic/add_to_set.rb +32 -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/persistence/command.rb +71 -0
- data/lib/mongoid/persistence/insert.rb +53 -0
- data/lib/mongoid/persistence/insert_embedded.rb +43 -0
- data/lib/mongoid/persistence/remove.rb +44 -0
- data/lib/mongoid/persistence/remove_all.rb +40 -0
- data/lib/mongoid/persistence/remove_embedded.rb +48 -0
- data/lib/mongoid/persistence/update.rb +77 -0
- data/lib/mongoid/railtie.rb +139 -0
- data/lib/mongoid/railties/database.rake +171 -0
- data/lib/mongoid/railties/document.rb +12 -0
- data/lib/mongoid/relations.rb +107 -0
- data/lib/mongoid/relations/accessors.rb +175 -0
- data/lib/mongoid/relations/auto_save.rb +34 -0
- data/lib/mongoid/relations/binding.rb +26 -0
- data/lib/mongoid/relations/bindings.rb +9 -0
- data/lib/mongoid/relations/bindings/embedded/in.rb +82 -0
- data/lib/mongoid/relations/bindings/embedded/many.rb +98 -0
- data/lib/mongoid/relations/bindings/embedded/one.rb +66 -0
- data/lib/mongoid/relations/bindings/referenced/in.rb +74 -0
- data/lib/mongoid/relations/bindings/referenced/many.rb +96 -0
- data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +103 -0
- data/lib/mongoid/relations/bindings/referenced/one.rb +66 -0
- data/lib/mongoid/relations/builder.rb +42 -0
- data/lib/mongoid/relations/builders.rb +79 -0
- data/lib/mongoid/relations/builders/embedded/in.rb +25 -0
- data/lib/mongoid/relations/builders/embedded/many.rb +32 -0
- data/lib/mongoid/relations/builders/embedded/one.rb +26 -0
- data/lib/mongoid/relations/builders/nested_attributes/many.rb +126 -0
- data/lib/mongoid/relations/builders/nested_attributes/one.rb +135 -0
- data/lib/mongoid/relations/builders/referenced/in.rb +29 -0
- data/lib/mongoid/relations/builders/referenced/many.rb +47 -0
- data/lib/mongoid/relations/builders/referenced/many_to_many.rb +29 -0
- data/lib/mongoid/relations/builders/referenced/one.rb +27 -0
- data/lib/mongoid/relations/cascading.rb +55 -0
- data/lib/mongoid/relations/cascading/delete.rb +19 -0
- data/lib/mongoid/relations/cascading/destroy.rb +19 -0
- data/lib/mongoid/relations/cascading/nullify.rb +18 -0
- data/lib/mongoid/relations/cascading/strategy.rb +26 -0
- data/lib/mongoid/relations/constraint.rb +42 -0
- data/lib/mongoid/relations/cyclic.rb +103 -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/in.rb +173 -0
- data/lib/mongoid/relations/embedded/many.rb +499 -0
- data/lib/mongoid/relations/embedded/one.rb +170 -0
- data/lib/mongoid/relations/macros.rb +310 -0
- data/lib/mongoid/relations/many.rb +215 -0
- data/lib/mongoid/relations/metadata.rb +539 -0
- data/lib/mongoid/relations/nested_builder.rb +68 -0
- data/lib/mongoid/relations/one.rb +47 -0
- data/lib/mongoid/relations/polymorphic.rb +54 -0
- data/lib/mongoid/relations/proxy.rb +143 -0
- data/lib/mongoid/relations/referenced/batch.rb +71 -0
- data/lib/mongoid/relations/referenced/batch/insert.rb +57 -0
- data/lib/mongoid/relations/referenced/in.rb +216 -0
- data/lib/mongoid/relations/referenced/many.rb +516 -0
- data/lib/mongoid/relations/referenced/many_to_many.rb +396 -0
- data/lib/mongoid/relations/referenced/one.rb +222 -0
- data/lib/mongoid/relations/reflections.rb +45 -0
- data/lib/mongoid/safe.rb +23 -0
- data/lib/mongoid/safety.rb +207 -0
- data/lib/mongoid/scope.rb +31 -0
- data/lib/mongoid/serialization.rb +99 -0
- data/lib/mongoid/sharding.rb +51 -0
- data/lib/mongoid/state.rb +67 -0
- data/lib/mongoid/timestamps.rb +14 -0
- data/lib/mongoid/timestamps/created.rb +31 -0
- data/lib/mongoid/timestamps/updated.rb +33 -0
- data/lib/mongoid/validations.rb +124 -0
- data/lib/mongoid/validations/associated.rb +44 -0
- data/lib/mongoid/validations/referenced.rb +58 -0
- data/lib/mongoid/validations/uniqueness.rb +85 -0
- data/lib/mongoid/version.rb +4 -0
- data/lib/mongoid/versioning.rb +113 -0
- data/lib/rails/generators/mongoid/config/config_generator.rb +25 -0
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +20 -0
- data/lib/rails/generators/mongoid/model/model_generator.rb +24 -0
- data/lib/rails/generators/mongoid/model/templates/model.rb +19 -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 +70 -0
- data/lib/rails/mongoid.rb +58 -0
- metadata +406 -0
@@ -0,0 +1,45 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid # :nodoc:
|
3
|
+
module Relations #:nodoc:
|
4
|
+
|
5
|
+
# The reflections module provides convenience methods that can retrieve
|
6
|
+
# useful information about associations.
|
7
|
+
module Reflections
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
included do
|
11
|
+
|
12
|
+
delegate \
|
13
|
+
:reflect_on_association,
|
14
|
+
:reflect_on_all_associations, :to => "self.class"
|
15
|
+
end
|
16
|
+
|
17
|
+
module ClassMethods #:nodoc
|
18
|
+
|
19
|
+
# Returns the relation metadata for the supplied name.
|
20
|
+
#
|
21
|
+
# @example Find relation metadata by name.
|
22
|
+
# Person.reflect_on_association(:addresses)
|
23
|
+
#
|
24
|
+
# @param [ String, Symbol ] name The name of the relation to find.
|
25
|
+
#
|
26
|
+
# @return [ Metadata ] The matching relation metadata.
|
27
|
+
def reflect_on_association(name)
|
28
|
+
relations[name.to_s]
|
29
|
+
end
|
30
|
+
|
31
|
+
# Returns all relation metadata for the supplied macros.
|
32
|
+
#
|
33
|
+
# @example Find multiple relation metadata by macro.
|
34
|
+
# Person.reflect_on_all_associations(:embeds_many)
|
35
|
+
#
|
36
|
+
# @param [ Array<String, Symbol> ] *macros The relation macros.
|
37
|
+
#
|
38
|
+
# @return [ Array<Metadata> ] The matching relation metadata.
|
39
|
+
def reflect_on_all_associations(*macros)
|
40
|
+
relations.values.select { |meta| macros.include?(meta.macro) }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/mongoid/safe.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
|
4
|
+
# Contains behaviour for determining if Mongoid is in safe mode.
|
5
|
+
module Safe
|
6
|
+
|
7
|
+
# Determine based on configuration if we are persisting in safe mode or
|
8
|
+
# not.
|
9
|
+
#
|
10
|
+
# The query option will always override the global configuration.
|
11
|
+
#
|
12
|
+
# @example Are we in safe mode?
|
13
|
+
# document.safe_mode?(:safe => true)
|
14
|
+
#
|
15
|
+
# @param [ Hash ] options Persistence options.
|
16
|
+
#
|
17
|
+
# @return [ true, false ] True if in safe mode, false if not.
|
18
|
+
def safe_mode?(options)
|
19
|
+
safe = options[:safe]
|
20
|
+
safe.nil? ? Mongoid.persist_in_safe_mode : safe
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,207 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
|
4
|
+
# The +Safety+ module is used to provide a DSL to execute database operations
|
5
|
+
# in safe mode on a per query basis, either from the +Document+ class level
|
6
|
+
# or instance level.
|
7
|
+
module Safety
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
# Execute the following class-level persistence operation in safe mode.
|
11
|
+
#
|
12
|
+
# @example Upsert in safe mode.
|
13
|
+
# person.safely.upsert
|
14
|
+
#
|
15
|
+
# @example Destroy in safe mode with w and fsync options.
|
16
|
+
# person.safely(:w => 2, :fsync => true).destroy
|
17
|
+
#
|
18
|
+
# @param [ Hash ] options The safe mode options.
|
19
|
+
#
|
20
|
+
# @option options [ Integer ] :w The number of nodes to write to.
|
21
|
+
# @option options [ Integer ] :wtimeout Time to wait for return from all
|
22
|
+
# nodes.
|
23
|
+
# @option options [ true, false ] :fsync Should a fsync occur.
|
24
|
+
#
|
25
|
+
# @return [ Proxy ] The safety proxy.
|
26
|
+
def safely(safety = true)
|
27
|
+
Proxy.new(self, safety)
|
28
|
+
end
|
29
|
+
|
30
|
+
module ClassMethods #:nodoc:
|
31
|
+
|
32
|
+
# Execute the following class-level persistence operation in safe mode.
|
33
|
+
#
|
34
|
+
# @example Create in safe mode.
|
35
|
+
# Person.safely.create(:name => "John")
|
36
|
+
#
|
37
|
+
# @example Delete all in safe mode with options.
|
38
|
+
# Person.safely(:w => 2, :fsync => true).delete_all
|
39
|
+
#
|
40
|
+
# @param [ Hash ] options The safe mode options.
|
41
|
+
#
|
42
|
+
# @option options [ Integer ] :w The number of nodes to write to.
|
43
|
+
# @option options [ Integer ] :wtimeout Time to wait for return from all
|
44
|
+
# nodes.
|
45
|
+
# @option options [ true, false ] :fsync Should a fsync occur.
|
46
|
+
#
|
47
|
+
# @return [ Proxy ] The safety proxy.
|
48
|
+
def safely(safety = true)
|
49
|
+
Proxy.new(self, safety)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# When this class proxies a document or class, the next persistence
|
54
|
+
# operation executed on it will query in safe mode.
|
55
|
+
#
|
56
|
+
# Operations that took a hash of attributes had to be somewhat duplicated
|
57
|
+
# here since we do not want to allow a :safe attribute to be included in
|
58
|
+
# the args. This is because safe could be a common attribute name and we
|
59
|
+
# don't want the collision between the attribute and determining whether or
|
60
|
+
# not safe mode is allowed.
|
61
|
+
class Proxy
|
62
|
+
|
63
|
+
attr_reader :target, :safety_options
|
64
|
+
|
65
|
+
# Create a new +Document+. This will instantiate a new document and
|
66
|
+
# insert it in a single call. Will always return the document
|
67
|
+
# whether save passed or not.
|
68
|
+
#
|
69
|
+
# @example Safely create a document.
|
70
|
+
# Person.safely.create(:title => "Mr")
|
71
|
+
#
|
72
|
+
# @param [ Hash ] attributes The attributes to create with.
|
73
|
+
#
|
74
|
+
# @return [ Document ] The new document.
|
75
|
+
def create(attributes = {})
|
76
|
+
target.new(attributes).tap { |doc| doc.insert(:safe => safety_options) }
|
77
|
+
end
|
78
|
+
|
79
|
+
# Create a new +Document+. This will instantiate a new document and
|
80
|
+
# insert it in a single call. Will always return the document
|
81
|
+
# whether save passed or not, and if validation fails an error will be
|
82
|
+
# raise.
|
83
|
+
#
|
84
|
+
# @example Safely create a document.
|
85
|
+
# Person.safely.create!(:title => "Mr")
|
86
|
+
#
|
87
|
+
# @param [ Hash ] attributes The attributes to create with.
|
88
|
+
#
|
89
|
+
# @raise [ Errors::Validations ] If validation failed.
|
90
|
+
#
|
91
|
+
# @return [ Document ] If validation passed.
|
92
|
+
def create!(attributes = {})
|
93
|
+
target.new(attributes).tap do |document|
|
94
|
+
fail_validate!(document) if document.insert(:safe => safety_options).errors.any?
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# Delete all documents given the supplied conditions. If no conditions
|
99
|
+
# are passed, the entire collection will be dropped for performance
|
100
|
+
# benefits. Does not fire any callbacks.
|
101
|
+
#
|
102
|
+
# @example Delete all documents.
|
103
|
+
# Person.safely.delete_all
|
104
|
+
#
|
105
|
+
# @example Conditionally delete all documents.
|
106
|
+
# Person.safely.delete_all(:conditions => { :title => "Sir" })
|
107
|
+
#
|
108
|
+
# @param [ Hash ] conditions The conditions to delete with.
|
109
|
+
#
|
110
|
+
# @return [ Integer ] The number of documents deleted.
|
111
|
+
def delete_all(conditions = {})
|
112
|
+
Mongoid::Persistence::RemoveAll.new(
|
113
|
+
target,
|
114
|
+
{ :validate => false, :safe => safety_options },
|
115
|
+
conditions[:conditions] || {}
|
116
|
+
).persist
|
117
|
+
end
|
118
|
+
|
119
|
+
# destroy all documents given the supplied conditions. If no conditions
|
120
|
+
# are passed, the entire collection will be dropped for performance
|
121
|
+
# benefits. Fires the destroy callbacks if conditions were passed.
|
122
|
+
#
|
123
|
+
# @example destroy all documents.
|
124
|
+
# Person.safely.destroy_all
|
125
|
+
#
|
126
|
+
# @example Conditionally destroy all documents.
|
127
|
+
# Person.safely.destroy_all(:conditions => { :title => "Sir" })
|
128
|
+
#
|
129
|
+
# @param [ Hash ] conditions The conditions to destroy with.
|
130
|
+
#
|
131
|
+
# @return [ Integer ] The number of documents destroyd.
|
132
|
+
def destroy_all(conditions = {})
|
133
|
+
documents = target.all(conditions)
|
134
|
+
documents.count.tap do |count|
|
135
|
+
documents.each { |doc| doc.destroy(:safe => safety_options) }
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
# Increment the field by the provided value, else if it doesn't exists set
|
140
|
+
# it to that value.
|
141
|
+
#
|
142
|
+
# @example Safely increment a field.
|
143
|
+
# person.safely.inc(:age, 1)
|
144
|
+
#
|
145
|
+
# @param [ Symbol, String ] field The field to increment.
|
146
|
+
# @param [ Integer ] value The value to increment by.
|
147
|
+
# @param [ Hash ] options Options to pass through to the driver.
|
148
|
+
def inc(field, value, options = {})
|
149
|
+
target.inc(field, value, :safe => safety_options)
|
150
|
+
end
|
151
|
+
|
152
|
+
# Create the new +Proxy+.
|
153
|
+
#
|
154
|
+
# @example Create the proxy.
|
155
|
+
# Proxy.new(document, :w => 3)
|
156
|
+
#
|
157
|
+
# @param [ Document, Class ] target Either the class or the instance.
|
158
|
+
# @param [ true, Hash ] safety_options The options.
|
159
|
+
def initialize(target, safety_options)
|
160
|
+
@target = target
|
161
|
+
@safety_options = safety_options
|
162
|
+
end
|
163
|
+
|
164
|
+
# We will use method missing to proxy calls to the target.
|
165
|
+
#
|
166
|
+
# @example Save safely.
|
167
|
+
# person.safely.save
|
168
|
+
#
|
169
|
+
# @param [ Array ] *args The arguments to pass on.
|
170
|
+
def method_missing(*args)
|
171
|
+
name = args[0]
|
172
|
+
attributes = args[1] || {}
|
173
|
+
target.send(name, attributes.merge(:safe => safety_options))
|
174
|
+
end
|
175
|
+
|
176
|
+
# Update the +Document+ attributes in the datbase.
|
177
|
+
#
|
178
|
+
# @example Safely update attributes.
|
179
|
+
# person.safely.update_attributes(:title => "Sir")
|
180
|
+
#
|
181
|
+
# @param [ Hash ] attributes The attributes to update.
|
182
|
+
#
|
183
|
+
# @return [ true, false ] Whether the document was saved.
|
184
|
+
def update_attributes(attributes = {})
|
185
|
+
target.write_attributes(attributes)
|
186
|
+
target.update(:safe => safety_options)
|
187
|
+
end
|
188
|
+
|
189
|
+
# Update the +Document+ attributes in the datbase.
|
190
|
+
#
|
191
|
+
# @example Safely update attributes.
|
192
|
+
# person.safely.update_attributes(:title => "Sir")
|
193
|
+
#
|
194
|
+
# @param [ Hash ] attributes The attributes to update.
|
195
|
+
#
|
196
|
+
# @raise [ Errors::Validations ] If validation failed.
|
197
|
+
#
|
198
|
+
# @return [ true ] If the document was saved.
|
199
|
+
def update_attributes!(attributes = {})
|
200
|
+
target.write_attributes(attributes)
|
201
|
+
update(:safe => safety_options).tap do |result|
|
202
|
+
target.class.fail_validate!(target) unless result
|
203
|
+
end
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
# This module handles behaviour for defining scopes on classes.
|
4
|
+
class Scope
|
5
|
+
attr_reader :conditions, :extensions
|
6
|
+
|
7
|
+
# Create the new +Scope+. If a block is passed in, this Scope will store
|
8
|
+
# the block for future calls to #extend.
|
9
|
+
#
|
10
|
+
# @example Create a new scope.
|
11
|
+
# Scope.new(:title => "Sir")
|
12
|
+
#
|
13
|
+
# @param [ Hash ] conditions The scoping limitations.
|
14
|
+
def initialize(conditions = {}, &block)
|
15
|
+
@conditions = conditions
|
16
|
+
@extensions = Module.new(&block) if block_given?
|
17
|
+
end
|
18
|
+
|
19
|
+
# Extend a supplied criteria.
|
20
|
+
#
|
21
|
+
# @example Extend the criteria.
|
22
|
+
# scope.extend(criteria)
|
23
|
+
#
|
24
|
+
# @param [ Criteria } criteria A mongoid criteria to extend.
|
25
|
+
#
|
26
|
+
# @return [ Criteria ] The new criteria object.
|
27
|
+
def extend(criteria)
|
28
|
+
extensions ? criteria.extend(extensions) : criteria
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid # :nodoc:
|
3
|
+
|
4
|
+
# This module provides the extra behaviour for including relations in JSON
|
5
|
+
# and XML serialization.
|
6
|
+
module Serialization
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
include ActiveModel::Serialization
|
9
|
+
|
10
|
+
# Gets the document as a serializable hash, used by ActiveModel's JSON and
|
11
|
+
# XML serializers. This override is just to be able to pass the :include
|
12
|
+
# and :except options to get associations in the hash.
|
13
|
+
#
|
14
|
+
# @example Get the serializable hash.
|
15
|
+
# document.serializable_hash
|
16
|
+
#
|
17
|
+
# @example Get the serializable hash with options.
|
18
|
+
# document.serializable_hash(:include => :addresses)
|
19
|
+
#
|
20
|
+
# @param [ Hash ] options The options to pass.
|
21
|
+
#
|
22
|
+
# @option options [ Symbol ] :include What relations to include
|
23
|
+
# @option options [ Symbol ] :only Limit the fields to only these.
|
24
|
+
# @option options [ Symbol ] :except Dont include these fields.
|
25
|
+
#
|
26
|
+
# @return [ Hash ] The document, ready to be serialized.
|
27
|
+
#
|
28
|
+
# @since 2.0.0.rc.6
|
29
|
+
def serializable_hash(options = nil)
|
30
|
+
options ||= {}
|
31
|
+
super(options).tap do |attrs|
|
32
|
+
serialize_relations(attrs, options) if options[:include]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
# For each of the provided include options, get the relation needed and
|
39
|
+
# provide it in the hash.
|
40
|
+
#
|
41
|
+
# @example Serialize the included relations.
|
42
|
+
# document.serialize_relations({}, :include => :addresses)
|
43
|
+
#
|
44
|
+
# @param [ Hash ] attributes The attributes to serialize.
|
45
|
+
# @param [ Hash ] options The serialization options.
|
46
|
+
#
|
47
|
+
# @option options [ Symbol ] :include What relations to include
|
48
|
+
# @option options [ Symbol ] :only Limit the fields to only these.
|
49
|
+
# @option options [ Symbol ] :except Dont include these fields.
|
50
|
+
#
|
51
|
+
# @since 2.0.0.rc.6
|
52
|
+
def serialize_relations(attributes = {}, options = {})
|
53
|
+
inclusions = options[:include]
|
54
|
+
relation_names(inclusions).each do |name|
|
55
|
+
metadata = relations[name.to_s]
|
56
|
+
relation = send(metadata.name, false, :eager => true)
|
57
|
+
if relation
|
58
|
+
attributes[metadata.name.to_s] =
|
59
|
+
relation.serializable_hash(relation_options(inclusions, options, name))
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# Since the inclusions can be a hash, symbol, or array of symbols, this is
|
65
|
+
# provided as a convenience to parse out the names.
|
66
|
+
#
|
67
|
+
# @example Get the relation names.
|
68
|
+
# document.relation_names(:include => [ :addresses ])
|
69
|
+
#
|
70
|
+
# @param [ Hash, Symbol, Array<Symbol ] inclusions The inclusions.
|
71
|
+
#
|
72
|
+
# @return [ Array<Symbol> ] The names of the included relations.
|
73
|
+
#
|
74
|
+
# @since 2.0.0.rc.6
|
75
|
+
def relation_names(inclusions)
|
76
|
+
inclusions.is_a?(Hash) ? inclusions.keys : Array.wrap(inclusions)
|
77
|
+
end
|
78
|
+
|
79
|
+
# Since the inclusions can be a hash, symbol, or array of symbols, this is
|
80
|
+
# provided as a convenience to parse out the options.
|
81
|
+
#
|
82
|
+
# @example Get the relation options.
|
83
|
+
# document.relation_names(:include => [ :addresses ])
|
84
|
+
#
|
85
|
+
# @param [ Hash, Symbol, Array<Symbol ] inclusions The inclusions.
|
86
|
+
# @param [ Symbol ] name The name of the relation.
|
87
|
+
#
|
88
|
+
# @return [ Hash ] The options for the relation.
|
89
|
+
#
|
90
|
+
# @since 2.0.0.rc.6
|
91
|
+
def relation_options(inclusions, options, name)
|
92
|
+
if inclusions.is_a?(Hash)
|
93
|
+
inclusions[name]
|
94
|
+
else
|
95
|
+
{ :except => options[:except], :only => options[:only] }
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc
|
3
|
+
|
4
|
+
# This module contains behaviour for adding shard key fields to updates.
|
5
|
+
module Sharding
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
cattr_accessor :shard_key_fields
|
10
|
+
self.shard_key_fields = []
|
11
|
+
|
12
|
+
delegate :shard_key_fields, :to => "self.class"
|
13
|
+
end
|
14
|
+
|
15
|
+
# Get the document selector with the defined shard keys.
|
16
|
+
#
|
17
|
+
# @example Get the selector for the shard keys.
|
18
|
+
# person.shard_key_selector
|
19
|
+
#
|
20
|
+
# @return [ Hash ] The shard key selector.
|
21
|
+
#
|
22
|
+
# @since 2.0.0
|
23
|
+
def shard_key_selector
|
24
|
+
{}.tap do |selector|
|
25
|
+
shard_key_fields.each do |field|
|
26
|
+
selector[field.to_s] = send(field)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
module ClassMethods #:nodoc
|
32
|
+
|
33
|
+
# Specifies a shard key with the field(s) specified.
|
34
|
+
#
|
35
|
+
# @example Specify the shard key.
|
36
|
+
#
|
37
|
+
# class Person
|
38
|
+
# include Mongoid::Document
|
39
|
+
# field :first_name, :type => String
|
40
|
+
# field :last_name, :type => String
|
41
|
+
#
|
42
|
+
# shard_key :first_name, :last_name
|
43
|
+
# end
|
44
|
+
#
|
45
|
+
# @since 2.0.0
|
46
|
+
def shard_key(*names)
|
47
|
+
self.shard_key_fields = names
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|