Module: Riagent::Persistence
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/riagent/persistence.rb,
lib/riagent/persistence/riak_json_strategy.rb
Defined Under Namespace
Modules: ClassMethods, RiakJsonStrategy
Instance Method Summary collapse
-
#destroy ⇒ Object
Delete the document from its collection.
-
#save(options = {:validate => true}) ⇒ String
Performs validations and saves the document The validation process can be skipped by passing
validate: false. -
#save!(options = {:validate => true}) ⇒ Object
Attempts to validate and save the document just like
savebut will raise aRiagent::InvalidDocumentErrorexception instead of returningfalseif the doc is not valid. -
#update(attrs) ⇒ Object
Update an object’s attributes and save it.
-
#update!(attrs) ⇒ Object
Perform an update(), raise an error if the doc is not valid.
-
#update_attributes(attrs) ⇒ Object
Update attributes (alias for update() for Rails versions < 4).
Instance Method Details
#destroy ⇒ Object
Delete the document from its collection
34 35 36 37 38 39 |
# File 'lib/riagent/persistence.rb', line 34 def destroy run_callbacks(:destroy) do self.class.collection.remove(self) @destroyed = true end end |
#save(options = {:validate => true}) ⇒ String
Performs validations and saves the document The validation process can be skipped by passing validate: false. Also triggers :before_create / :after_create type callbacks
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/riagent/persistence.rb', line 45 def save(={:validate => true}) context = new_record? ? :create : :update return false if [:validate] && !valid?(context) run_callbacks(context) do result = self.class.collection.insert(self) self.persist! result end end |
#save!(options = {:validate => true}) ⇒ Object
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.
58 59 60 61 62 63 |
# File 'lib/riagent/persistence.rb', line 58 def save!(={:validate => true}) unless save() raise Riagent::InvalidDocumentError.new(self) end true end |
#update(attrs) ⇒ Object
Update an object’s attributes and save it
66 67 68 69 70 71 |
# File 'lib/riagent/persistence.rb', line 66 def update(attrs) run_callbacks(:update) do self.attributes = attrs self.save end end |
#update!(attrs) ⇒ Object
Perform an update(), raise an error if the doc is not valid
74 75 76 77 78 79 |
# File 'lib/riagent/persistence.rb', line 74 def update!(attrs) unless update(attrs) raise Riagent::InvalidDocumentError.new(self) end true end |
#update_attributes(attrs) ⇒ Object
Update attributes (alias for update() for Rails versions < 4)
82 83 84 |
# File 'lib/riagent/persistence.rb', line 82 def update_attributes(attrs) self.update(attrs) end |