mongoid-braxton 2.0.2
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/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,61 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Paths #:nodoc:
|
|
4
|
+
extend ActiveSupport::Concern
|
|
5
|
+
included do
|
|
6
|
+
cattr_accessor :__path
|
|
7
|
+
attr_accessor :_index
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
# Get the insertion modifier for the document. Will be nil on root
|
|
11
|
+
# documents, $set on embeds_one, $push on embeds_many.
|
|
12
|
+
#
|
|
13
|
+
# Example:
|
|
14
|
+
#
|
|
15
|
+
# <tt>name.inserter</tt>
|
|
16
|
+
def _inserter
|
|
17
|
+
embedded? ? (embedded_many? ? "$push" : "$set") : nil
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Return the path to this +Document+ in JSON notation, used for atomic
|
|
21
|
+
# updates via $set in MongoDB.
|
|
22
|
+
#
|
|
23
|
+
# Example:
|
|
24
|
+
#
|
|
25
|
+
# <tt>address.path # returns "addresses"</tt>
|
|
26
|
+
def _path
|
|
27
|
+
_position.sub!(/\.\d+$/, '') || _position
|
|
28
|
+
end
|
|
29
|
+
alias :_pull :_path
|
|
30
|
+
|
|
31
|
+
# Returns the positional operator of this document for modification.
|
|
32
|
+
#
|
|
33
|
+
# Example:
|
|
34
|
+
#
|
|
35
|
+
# <tt>address.position</tt>
|
|
36
|
+
def _position
|
|
37
|
+
locator = _index ? (new_record? ? "" : ".#{_index}") : ""
|
|
38
|
+
embedded? ? "#{_parent._position}#{"." unless _parent._position.blank?}#{metadata.name.to_s}#{locator}" : ""
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Get the removal modifier for the document. Will be nil on root
|
|
42
|
+
# documents, $unset on embeds_one, $set on embeds_many.
|
|
43
|
+
#
|
|
44
|
+
# Example:
|
|
45
|
+
#
|
|
46
|
+
# <tt>name.remover</tt>
|
|
47
|
+
def _remover
|
|
48
|
+
embedded? ? (_index ? "$pull" : "$unset") : nil
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Return the selector for this document to be matched exactly for use
|
|
52
|
+
# with MongoDB's $ operator.
|
|
53
|
+
#
|
|
54
|
+
# Example:
|
|
55
|
+
#
|
|
56
|
+
# <tt>address.selector</tt>
|
|
57
|
+
def _selector
|
|
58
|
+
(embedded? ? _parent._selector.merge("#{_path}._id" => id) : { "_id" => id }).merge(shard_key_selector)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
require "mongoid/persistence/atomic"
|
|
3
|
+
require "mongoid/persistence/command"
|
|
4
|
+
require "mongoid/persistence/insert"
|
|
5
|
+
require "mongoid/persistence/insert_embedded"
|
|
6
|
+
require "mongoid/persistence/remove"
|
|
7
|
+
require "mongoid/persistence/remove_all"
|
|
8
|
+
require "mongoid/persistence/remove_embedded"
|
|
9
|
+
require "mongoid/persistence/update"
|
|
10
|
+
|
|
11
|
+
module Mongoid #:nodoc:
|
|
12
|
+
|
|
13
|
+
# The persistence module is a mixin to provide database accessor methods for
|
|
14
|
+
# the document. These correspond to the appropriate accessors on a
|
|
15
|
+
# mongo collection and retain the same DSL.
|
|
16
|
+
#
|
|
17
|
+
# @example Sample persistence operations.
|
|
18
|
+
# document.insert
|
|
19
|
+
# document.update
|
|
20
|
+
# document.upsert
|
|
21
|
+
module Persistence
|
|
22
|
+
extend ActiveSupport::Concern
|
|
23
|
+
include Atomic
|
|
24
|
+
|
|
25
|
+
# Remove the document from the datbase with callbacks.
|
|
26
|
+
#
|
|
27
|
+
# @example Destroy a document.
|
|
28
|
+
# document.destroy
|
|
29
|
+
#
|
|
30
|
+
# @param [ Hash ] options Options to pass to destroy.
|
|
31
|
+
#
|
|
32
|
+
# @return [ true, false ] True if successful, false if not.
|
|
33
|
+
def destroy(options = {})
|
|
34
|
+
run_callbacks(:destroy) { remove(options) }
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Insert a new document into the database. Will return the document
|
|
38
|
+
# itself whether or not the save was successful.
|
|
39
|
+
#
|
|
40
|
+
# @example Insert a document.
|
|
41
|
+
# document.insert
|
|
42
|
+
#
|
|
43
|
+
# @param [ Hash ] options Options to pass to insert.
|
|
44
|
+
#
|
|
45
|
+
# @return [ Document ] The persisted document.
|
|
46
|
+
def insert(options = {})
|
|
47
|
+
Insert.new(self, options).persist
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Remove the document from the datbase.
|
|
51
|
+
#
|
|
52
|
+
# @example Remove the document.
|
|
53
|
+
# document.remove
|
|
54
|
+
#
|
|
55
|
+
# @param [ Hash ] options Options to pass to remove.
|
|
56
|
+
#
|
|
57
|
+
# @return [ TrueClass ] True.
|
|
58
|
+
def remove(options = {})
|
|
59
|
+
if Remove.new(self, options).persist
|
|
60
|
+
attributes.freeze
|
|
61
|
+
self.destroyed = true
|
|
62
|
+
cascade!
|
|
63
|
+
end; true
|
|
64
|
+
end
|
|
65
|
+
alias :delete :remove
|
|
66
|
+
|
|
67
|
+
# Save the document - will perform an insert if the document is new, and
|
|
68
|
+
# update if not. If a validation error occurs an error will get raised.
|
|
69
|
+
#
|
|
70
|
+
# @example Save the document.
|
|
71
|
+
# document.save!
|
|
72
|
+
#
|
|
73
|
+
# @param [ Hash ] options Options to pass to the save.
|
|
74
|
+
#
|
|
75
|
+
# @return [ true, false ] True if validation passed.
|
|
76
|
+
def save!(options = {})
|
|
77
|
+
self.class.fail_validate!(self) unless upsert(options); true
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Update the document in the datbase.
|
|
81
|
+
#
|
|
82
|
+
# @example Update an existing document.
|
|
83
|
+
# document.update
|
|
84
|
+
#
|
|
85
|
+
# @param [ Hash ] options Options to pass to update.
|
|
86
|
+
#
|
|
87
|
+
# @return [ true, false ] True if succeeded, false if not.
|
|
88
|
+
def update(options = {})
|
|
89
|
+
Update.new(self, options).persist
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Update a single attribute and persist the entire document.
|
|
93
|
+
# This skips validation but fires the callbacks.
|
|
94
|
+
#
|
|
95
|
+
# @example Update the attribute.
|
|
96
|
+
# person.update_attribute(:title, "Sir")
|
|
97
|
+
#
|
|
98
|
+
# @param [ Symbol, String ] name The name of the attribute.
|
|
99
|
+
# @param [ Object ] value The new value of the attribute.a
|
|
100
|
+
#
|
|
101
|
+
# @return [ true, false ] True if save was successfull, false if not.
|
|
102
|
+
#
|
|
103
|
+
# @since 2.0.0.rc.6
|
|
104
|
+
def update_attribute(name, value)
|
|
105
|
+
write_attribute(name, value) ? save(:validate => false) : true
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Update the document attributes in the datbase.
|
|
109
|
+
#
|
|
110
|
+
# @example Update the document's attributes
|
|
111
|
+
# document.update_attributes(:title => "Sir")
|
|
112
|
+
#
|
|
113
|
+
# @param [ Hash ] attributes The attributes to update.
|
|
114
|
+
#
|
|
115
|
+
# @return [ true, false ] True if validation passed, false if not.
|
|
116
|
+
def update_attributes(attributes = {})
|
|
117
|
+
write_attributes(attributes); save
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# Update the document attributes in the database and raise an error if
|
|
121
|
+
# validation failed.
|
|
122
|
+
#
|
|
123
|
+
# @example Update the document's attributes.
|
|
124
|
+
# document.update_attributes(:title => "Sir")
|
|
125
|
+
#
|
|
126
|
+
# @param [ Hash ] attributes The attributes to update.
|
|
127
|
+
#
|
|
128
|
+
# @raise [ Errors::Validations ] If validation failed.
|
|
129
|
+
#
|
|
130
|
+
# @return [ true, false ] True if validation passed.
|
|
131
|
+
def update_attributes!(attributes = {})
|
|
132
|
+
update_attributes(attributes).tap do |result|
|
|
133
|
+
self.class.fail_validate!(self) unless result
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# Upsert the document - will perform an insert if the document is new, and
|
|
138
|
+
# update if not.
|
|
139
|
+
#
|
|
140
|
+
# @example Upsert the document.
|
|
141
|
+
# document.upsert
|
|
142
|
+
#
|
|
143
|
+
# @param [ Hash ] options Options to pass to the upsert.
|
|
144
|
+
#
|
|
145
|
+
# @return [ true, false ] True is success, false if not.
|
|
146
|
+
def upsert(options = {})
|
|
147
|
+
if new_record?
|
|
148
|
+
insert(options).persisted?
|
|
149
|
+
else
|
|
150
|
+
update(options)
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
alias :save :upsert
|
|
154
|
+
|
|
155
|
+
module ClassMethods #:nodoc:
|
|
156
|
+
|
|
157
|
+
# Create a new document. This will instantiate a new document and
|
|
158
|
+
# insert it in a single call. Will always return the document
|
|
159
|
+
# whether save passed or not.
|
|
160
|
+
#
|
|
161
|
+
# @example Create a new document.
|
|
162
|
+
# Person.create(:title => "Mr")
|
|
163
|
+
#
|
|
164
|
+
# @param [ Hash ] attributes The attributes to create with.
|
|
165
|
+
#
|
|
166
|
+
# @return [ Document ] The newly created document.
|
|
167
|
+
def create(attributes = {}, &block)
|
|
168
|
+
new(attributes, &block).tap(&:save)
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
# Create a new document. This will instantiate a new document and
|
|
172
|
+
# insert it in a single call. Will always return the document
|
|
173
|
+
# whether save passed or not, and if validation fails an error will be
|
|
174
|
+
# raise.
|
|
175
|
+
#
|
|
176
|
+
# @example Create a new document.
|
|
177
|
+
# Person.create!(:title => "Mr")
|
|
178
|
+
#
|
|
179
|
+
# @param [ Hash ] attributes The attributes to create with.
|
|
180
|
+
#
|
|
181
|
+
# @return [ Document ] The newly created document.
|
|
182
|
+
def create!(attributes = {}, &block)
|
|
183
|
+
document = new(attributes, &block)
|
|
184
|
+
fail_validate!(document) if document.insert.errors.any?
|
|
185
|
+
document
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
# Delete all documents given the supplied conditions. If no conditions
|
|
189
|
+
# are passed, the entire collection will be dropped for performance
|
|
190
|
+
# benefits. Does not fire any callbacks.
|
|
191
|
+
#
|
|
192
|
+
# @example Delete matching documents from the collection.
|
|
193
|
+
# Person.delete_all(:conditions => { :title => "Sir" })
|
|
194
|
+
#
|
|
195
|
+
# @example Delete all documents from the collection.
|
|
196
|
+
# Person.delete_all
|
|
197
|
+
#
|
|
198
|
+
# @param [ Hash ] conditions Optional conditions to delete by.
|
|
199
|
+
#
|
|
200
|
+
# @return [ Integer ] The number of documents deleted.
|
|
201
|
+
def delete_all(conditions = {})
|
|
202
|
+
RemoveAll.new(
|
|
203
|
+
self,
|
|
204
|
+
{ :validate => false },
|
|
205
|
+
conditions[:conditions] || {}
|
|
206
|
+
).persist
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
# Delete all documents given the supplied conditions. If no conditions
|
|
210
|
+
# are passed, the entire collection will be dropped for performance
|
|
211
|
+
# benefits. Fires the destroy callbacks if conditions were passed.
|
|
212
|
+
#
|
|
213
|
+
# @example Destroy matching documents from the collection.
|
|
214
|
+
# Person.destroy_all(:conditions => { :title => "Sir" })
|
|
215
|
+
#
|
|
216
|
+
# @example Destroy all documents from the collection.
|
|
217
|
+
# Person.destroy_all
|
|
218
|
+
#
|
|
219
|
+
# @param [ Hash ] conditions Optional conditions to destroy by.
|
|
220
|
+
#
|
|
221
|
+
# @return [ Integer ] The number of documents destroyed.
|
|
222
|
+
def destroy_all(conditions = {})
|
|
223
|
+
documents = all(conditions)
|
|
224
|
+
documents.count.tap do
|
|
225
|
+
documents.each { |doc| doc.destroy }
|
|
226
|
+
end
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
# Raise an error if validation failed.
|
|
230
|
+
#
|
|
231
|
+
# @example Raise the validation error.
|
|
232
|
+
# Person.fail_validate!(person)
|
|
233
|
+
#
|
|
234
|
+
# @param [ Document ] document The document to fail.
|
|
235
|
+
def fail_validate!(document)
|
|
236
|
+
raise Errors::Validations.new(document)
|
|
237
|
+
end
|
|
238
|
+
end
|
|
239
|
+
end
|
|
240
|
+
end
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
require "mongoid/persistence/atomic/operation"
|
|
3
|
+
require "mongoid/persistence/atomic/add_to_set"
|
|
4
|
+
require "mongoid/persistence/atomic/inc"
|
|
5
|
+
require "mongoid/persistence/atomic/pull_all"
|
|
6
|
+
require "mongoid/persistence/atomic/push"
|
|
7
|
+
|
|
8
|
+
module Mongoid #:nodoc:
|
|
9
|
+
module Persistence #:nodoc:
|
|
10
|
+
|
|
11
|
+
# This module provides the explicit atomic operations helpers on the
|
|
12
|
+
# document itself.
|
|
13
|
+
module Atomic
|
|
14
|
+
|
|
15
|
+
# Performs an atomic $addToSet of the provided value on the supplied field.
|
|
16
|
+
# If the field does not exist it will be initialized as an empty array.
|
|
17
|
+
#
|
|
18
|
+
# If the value already exists on the array it will not be added.
|
|
19
|
+
#
|
|
20
|
+
# @example Add only a unique value on the field.
|
|
21
|
+
# person.add_to_set(:aliases, "Bond")
|
|
22
|
+
#
|
|
23
|
+
# @param [ Symbol ] field The name of the field.
|
|
24
|
+
# @param [ Object ] value The value to add.
|
|
25
|
+
# @param [ Hash ] options The mongo persistence options.
|
|
26
|
+
#
|
|
27
|
+
# @return [ Array<Object> ] The new value of the field.
|
|
28
|
+
#
|
|
29
|
+
# @since 2.0.0
|
|
30
|
+
def add_to_set(field, value, options = {})
|
|
31
|
+
AddToSet.new(self, field, value, options).persist
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Performs an atomic $inc of the provided value on the supplied
|
|
35
|
+
# field. If the field does not exist it will be initialized as
|
|
36
|
+
# the provided value.
|
|
37
|
+
#
|
|
38
|
+
# @example Increment a field.
|
|
39
|
+
# person.inc(:score, 2)
|
|
40
|
+
#
|
|
41
|
+
# @param [ Symbol ] field The name of the field.
|
|
42
|
+
# @param [ Integer ] value The value to increment.
|
|
43
|
+
# @param [ Hash ] options The mongo persistence options.
|
|
44
|
+
#
|
|
45
|
+
# @return [ Array<Object> ] The new value of the field.
|
|
46
|
+
#
|
|
47
|
+
# @since 2.0.0
|
|
48
|
+
def inc(field, value, options = {})
|
|
49
|
+
Inc.new(self, field, value, options).persist
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Performs an atomic $pullAll of the provided value on the supplied
|
|
53
|
+
# field. If the field does not exist it will be initialized as an
|
|
54
|
+
# empty array.
|
|
55
|
+
#
|
|
56
|
+
# @example Pull the values from the field.
|
|
57
|
+
# person.pull_all(:aliases, [ "Bond", "James" ])
|
|
58
|
+
#
|
|
59
|
+
# @param [ Symbol ] field The name of the field.
|
|
60
|
+
# @param [ Array<Object> ] value The values to pull.
|
|
61
|
+
# @param [ Hash ] options The mongo persistence options.
|
|
62
|
+
#
|
|
63
|
+
# @return [ Array<Object> ] The new value of the field.
|
|
64
|
+
#
|
|
65
|
+
# @since 2.0.0
|
|
66
|
+
def pull_all(field, value, options = {})
|
|
67
|
+
PullAll.new(self, field, value, options).persist
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# Performs an atomic $push of the provided value on the supplied field. If
|
|
71
|
+
# the field does not exist it will be initialized as an empty array.
|
|
72
|
+
#
|
|
73
|
+
# @example Push a value on the field.
|
|
74
|
+
# person.push(:aliases, "Bond")
|
|
75
|
+
#
|
|
76
|
+
# @param [ Symbol ] field The name of the field.
|
|
77
|
+
# @param [ Object ] value The value to push.
|
|
78
|
+
# @param [ Hash ] options The mongo persistence options.
|
|
79
|
+
#
|
|
80
|
+
# @return [ Array<Object> ] The new value of the field.
|
|
81
|
+
#
|
|
82
|
+
# @since 2.0.0
|
|
83
|
+
def push(field, value, options = {})
|
|
84
|
+
Push.new(self, field, value, options).persist
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Persistence #:nodoc:
|
|
4
|
+
module Atomic #:nodoc:
|
|
5
|
+
|
|
6
|
+
# This class provides the ability to perform an explicit $addToSet
|
|
7
|
+
# modification on a specific field.
|
|
8
|
+
class AddToSet < Operation
|
|
9
|
+
|
|
10
|
+
# Sends the atomic $addToSet operation to the database.
|
|
11
|
+
#
|
|
12
|
+
# @example Persist the new values.
|
|
13
|
+
# addToSet.persist
|
|
14
|
+
#
|
|
15
|
+
# @return [ Object ] The new array value.
|
|
16
|
+
#
|
|
17
|
+
# @since 2.0.0
|
|
18
|
+
def persist
|
|
19
|
+
document[field] = [] unless document[field]
|
|
20
|
+
values = document.send(field)
|
|
21
|
+
values.push(value) unless values.include?(value)
|
|
22
|
+
values.tap do
|
|
23
|
+
if document.persisted?
|
|
24
|
+
document.collection.update(document._selector, operation("$addToSet"), options)
|
|
25
|
+
document.changes.delete(field.to_s)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Persistence #:nodoc:
|
|
4
|
+
module Atomic #:nodoc:
|
|
5
|
+
|
|
6
|
+
# This class provides atomic $inc behaviour.
|
|
7
|
+
class Inc < Operation
|
|
8
|
+
|
|
9
|
+
# Sends the atomic $inc operation to the database.
|
|
10
|
+
#
|
|
11
|
+
# @example Persist the new values.
|
|
12
|
+
# inc.persist
|
|
13
|
+
#
|
|
14
|
+
# @return [ Object ] The new integer value.
|
|
15
|
+
#
|
|
16
|
+
# @since 2.0.0
|
|
17
|
+
def persist
|
|
18
|
+
current = document[field] || 0
|
|
19
|
+
document[field] = current + value
|
|
20
|
+
document[field].tap do
|
|
21
|
+
document.collection.update(document._selector, operation("$inc"), options)
|
|
22
|
+
document.changes.delete(field.to_s)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|