Class: Mongoid::Safety::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/safety.rb

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

Instance Method Summary collapse

Constructor Details

#initialize(target, safety_options) ⇒ Proxy

Create the new Proxy.

Examples:

Create the proxy.

Proxy.new(document, :w => 3)

Parameters:

  • target (Document, Class)

    Either the class or the instance.

  • safety_options (true, Hash)

    The options.



159
160
161
162
# File 'lib/mongoid/safety.rb', line 159

def initialize(target, safety_options)
  @target = target
  @safety_options = 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.

Examples:

Save safely.

person.safely.save

Parameters:

  • *args (Array)

    The arguments to pass on.



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 => safety_options))
end

Instance Attribute Details

#safety_optionsObject (readonly)

Returns the value of attribute safety_options.



63
64
65
# File 'lib/mongoid/safety.rb', line 63

def safety_options
  @safety_options
end

#targetObject (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.

Examples:

Safely create a document.

Person.safely.create(:title => "Mr")

Parameters:

  • attributes (Hash) (defaults to: {})

    The attributes to create with.

Returns:



75
76
77
# File 'lib/mongoid/safety.rb', line 75

def create(attributes = {})
  target.new(attributes).tap { |doc| doc.insert(:safe => safety_options) }
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.

Examples:

Safely create a document.

Person.safely.create!(:title => "Mr")

Parameters:

  • attributes (Hash) (defaults to: {})

    The attributes to create with.

Returns:

Raises:



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 => safety_options).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.

Examples:

Delete all documents.

Person.safely.delete_all

Conditionally delete all documents.

Person.safely.delete_all(:conditions => { :title => "Sir" })

Parameters:

  • conditions (Hash) (defaults to: {})

    The conditions to delete with.

Returns:

  • (Integer)

    The number of documents deleted.



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 => safety_options },
    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.

Examples:

destroy all documents.

Person.safely.destroy_all

Conditionally destroy all documents.

Person.safely.destroy_all(:conditions => { :title => "Sir" })

Parameters:

  • conditions (Hash) (defaults to: {})

    The conditions to destroy with.

Returns:

  • (Integer)

    The number of documents destroyd.



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 => safety_options) }
  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.

Examples:

Safely increment a field.

person.safely.inc(:age, 1)

Parameters:

  • field (Symbol, String)

    The field to increment.

  • value (Integer)

    The value to increment by.

  • options (Hash) (defaults to: {})

    Options to pass through to the driver.



148
149
150
# File 'lib/mongoid/safety.rb', line 148

def inc(field, value, options = {})
  target.inc(field, value, :safe => safety_options)
end

#update_attributes(attributes = {}) ⇒ true, false

Update the Document attributes in the datbase.

Examples:

Safely update attributes.

person.safely.update_attributes(:title => "Sir")

Parameters:

  • attributes (Hash) (defaults to: {})

    The attributes to update.

Returns:

  • (true, false)

    Whether the document was saved.



184
185
186
187
# File 'lib/mongoid/safety.rb', line 184

def update_attributes(attributes = {})
  target.write_attributes(attributes)
  target.update(:safe => safety_options)
end

#update_attributes!(attributes = {}) ⇒ true

Update the Document attributes in the datbase.

Examples:

Safely update attributes.

person.safely.update_attributes(:title => "Sir")

Parameters:

  • attributes (Hash) (defaults to: {})

    The attributes to update.

Returns:

  • (true)

    If the document was saved.

Raises:



199
200
201
202
203
204
# File 'lib/mongoid/safety.rb', line 199

def update_attributes!(attributes = {})
  target.write_attributes(attributes)
  update(:safe => safety_options).tap do |result|
    target.class.fail_validate!(target) unless result
  end
end