module Riagent::Persistence
Provides a common persistence API for Riagent
Documents. Most persistence calls are delegated to the Collection class instance, which are implemented in persistence/*_strategy.rb modules.
Constants
- COLLECTION_TYPES
- VALID_KEY_LISTS
Key Listing strategies for
:riak_kv
collections
Public Instance Methods
Delete the document from its collection
# File lib/riagent/persistence.rb, line 44 def destroy return nil if self.new_record? run_callbacks(:destroy) do self.class.persistence.remove(self) @destroyed = true end end
Performs validations and saves the document The validation process can be skipped by passing validate: false
. Also triggers :before_create / :after_create type callbacks @return [String] Returns the key for the inserted document
# File lib/riagent/persistence.rb, line 56 def save(options={:validate => true}) context = self.new_record? ? :create : :update return false if options[:validate] && !valid?(context) run_callbacks(context) do if context == :create key = self.class.persistence.insert(self) else key = self.class.persistence.update(self) end self.persist! key end end
Attempts to validate and save the document just like save
but will raise a Riagent::InvalidDocumentError
exception instead of returning false
if the doc is not valid.
# File lib/riagent/persistence.rb, line 73 def save!(options={:validate => true}) unless save(options) raise Riagent::InvalidDocumentError.new(self) end true end
Update an object’s attributes and save it
# File lib/riagent/persistence.rb, line 81 def update(attrs) run_callbacks(:update) do self.attributes = attrs self.save end end
Perform an update(), raise an error if the doc is not valid
# File lib/riagent/persistence.rb, line 89 def update!(attrs) unless update(attrs) raise Riagent::InvalidDocumentError.new(self) end true end
Update attributes (alias for update() for Rails versions < 4)
# File lib/riagent/persistence.rb, line 97 def update_attributes(attrs) self.update(attrs) end