Class: Mongoid::Safety::Proxy
Overview
When this class proxies a document or class, the next persistence operation executed on it will query in safe mode.
Operations that took a hash of attributes had to be somewhat duplicated here since we do not want to allow a :safe attribute to be included in the args. This is because safe could be a common attribute name and we don’t want the collision between the attribute and determining whether or not safe mode is allowed.
Instance Attribute Summary collapse
-
#safety_options ⇒ Object
readonly
Returns the value of attribute safety_options.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Instance Method Summary collapse
-
#create(attributes = {}) ⇒ Document
Create a new
Document
. -
#create!(attributes = {}) ⇒ Document
Create a new
Document
. -
#delete_all(conditions = {}) ⇒ Integer
Delete all documents given the supplied conditions.
-
#destroy_all(conditions = {}) ⇒ Integer
destroy all documents given the supplied conditions.
-
#inc(field, value, options = {}) ⇒ Object
Increment the field by the provided value, else if it doesn’t exists set it to that value.
-
#initialize(target, safety_options) ⇒ Proxy
constructor
Create the new
Proxy
. -
#method_missing(*args) ⇒ Object
We will use method missing to proxy calls to the target.
-
#update_attributes(attributes = {}) ⇒ true, false
Update the
Document
attributes in the datbase. -
#update_attributes!(attributes = {}) ⇒ true
Update the
Document
attributes in the datbase.
Constructor Details
#initialize(target, safety_options) ⇒ Proxy
Create the new Proxy
.
159 160 161 162 |
# File 'lib/mongoid/safety.rb', line 159 def initialize(target, ) @target = target @safety_options = end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args) ⇒ Object
We will use method missing to proxy calls to the target.
170 171 172 173 174 |
# File 'lib/mongoid/safety.rb', line 170 def method_missing(*args) name = args[0] attributes = args[1] || {} target.send(name, attributes.merge(:safe => )) end |
Instance Attribute Details
#safety_options ⇒ Object (readonly)
Returns the value of attribute safety_options.
63 64 65 |
# File 'lib/mongoid/safety.rb', line 63 def @safety_options end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
63 64 65 |
# File 'lib/mongoid/safety.rb', line 63 def target @target end |
Instance Method Details
#create(attributes = {}) ⇒ Document
Create a new Document
. This will instantiate a new document and insert it in a single call. Will always return the document whether save passed or not.
75 76 77 |
# File 'lib/mongoid/safety.rb', line 75 def create(attributes = {}) target.new(attributes).tap { |doc| doc.insert(:safe => ) } end |
#create!(attributes = {}) ⇒ Document
Create a new Document
. This will instantiate a new document and insert it in a single call. Will always return the document whether save passed or not, and if validation fails an error will be raise.
92 93 94 95 96 |
# File 'lib/mongoid/safety.rb', line 92 def create!(attributes = {}) target.new(attributes).tap do |document| fail_validate!(document) if document.insert(:safe => ).errors.any? end end |
#delete_all(conditions = {}) ⇒ Integer
Delete all documents given the supplied conditions. If no conditions are passed, the entire collection will be dropped for performance benefits. Does not fire any callbacks.
111 112 113 114 115 116 117 |
# File 'lib/mongoid/safety.rb', line 111 def delete_all(conditions = {}) Mongoid::Persistence::RemoveAll.new( target, { :validate => false, :safe => }, conditions[:conditions] || {} ).persist end |
#destroy_all(conditions = {}) ⇒ Integer
destroy all documents given the supplied conditions. If no conditions are passed, the entire collection will be dropped for performance benefits. Fires the destroy callbacks if conditions were passed.
132 133 134 135 136 137 |
# File 'lib/mongoid/safety.rb', line 132 def destroy_all(conditions = {}) documents = target.all(conditions) documents.count.tap do |count| documents.each { |doc| doc.destroy(:safe => ) } end end |
#inc(field, value, options = {}) ⇒ Object
Increment the field by the provided value, else if it doesn’t exists set it to that value.
148 149 150 |
# File 'lib/mongoid/safety.rb', line 148 def inc(field, value, = {}) target.inc(field, value, :safe => ) end |
#update_attributes(attributes = {}) ⇒ true, false
Update the Document
attributes in the datbase.
184 185 186 187 |
# File 'lib/mongoid/safety.rb', line 184 def update_attributes(attributes = {}) target.write_attributes(attributes) target.update(:safe => ) end |
#update_attributes!(attributes = {}) ⇒ true
Update the Document
attributes in the datbase.
199 200 201 202 203 204 |
# File 'lib/mongoid/safety.rb', line 199 def update_attributes!(attributes = {}) target.write_attributes(attributes) update(:safe => ).tap do |result| target.class.fail_validate!(target) unless result end end |