Module: ActiveData::Model::Lifecycle
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/active_data/model/lifecycle.rb
Overview
Lifecycle methods for ActiveData::Model
Provides methods save and destroy and its bang variants. Also, patches create and update_attributes methods by adding save at the end.
You can define save or destroy performers with define_<action> methods. Create and update performers might be defined instead of save performer:
class Book
include ActiveData::Model
include ActiveData::Model::Lifecycle
attribute :id, Integer
attribute :title, String
define_save do # executes in the instance scope
REDIS.set(id, attributes.to_json)
end
define_destroy do
REDIS.del(id)
end
end
class Author
include ActiveData::Model
include ActiveData::Model::Lifecycle
attribute :id, Integer
attribute :name, String
define_create do # will be called on create only
REDIS.sadd('author_ids', id)
REDIS.set(id, attributes.to_json)
end
define_update do # will be called on update only
REDIS.set(id, attributes.to_json)
end
end
In case of undefined performer ActiveData::UnsavableObject or ActiveData::UndestroyableObject will be raised respectively.
If performers was not defined in model, they cat be passed as blocks to ‘save`, `update` and `destroy` methods:
authos.save { REDIS.set(id, attributes.to_json) }
authos.update { REDIS.set(id, attributes.to_json) }
authos.destroy { REDIS.del(id) }
Save and destroy processes acts almost the save way as ActiveRecord’s (with persisted? and destroyed? methods affecting).
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#destroy(&block) ⇒ Object
Destroys object by calling the destroy performer.
-
#destroy!(&block) ⇒ Object
Destroys object by calling the destroy performer.
-
#save(options = {}, &block) ⇒ Object
# Saves object by calling save performer defined with
define_save,define_createordefine_updatemethods. -
#save!(options = {}, &block) ⇒ Object
Saves object by calling save performer defined with
define_save,define_createordefine_updatemethods. -
#update(attributes, &block) ⇒ Object
(also: #update_attributes)
Assigns passed attributes and calls
saveReturns true or false in case of successful or unsuccessful saving respectively. -
#update!(attributes, &block) ⇒ Object
(also: #update_attributes!)
Assigns passed attributes and calls
save!Returns true in case of success and raises ActiveData::ValidationError or ActiveData::ObjectNotSaved in case of validation or saving fail respectively.
Instance Method Details
#destroy(&block) ⇒ Object
Destroys object by calling the destroy performer. Returns instance in any case. Changes persisted? to false and destroyed? to true in case of success.
.destroy
If destroy performer is not defined with ‘define_destroy`, it raises ActiveData::UndestroyableObject. Also destroy performer block might be passed instead of in-class performer definition:
.destroy { REDIS.del(id) }
238 239 240 241 242 |
# File 'lib/active_data/model/lifecycle.rb', line 238 def destroy &block raise ActiveData::UndestroyableObject unless block || destroyable? destroy_object(&block) self end |
#destroy!(&block) ⇒ Object
Destroys object by calling the destroy performer. In case of success returns instance and changes persisted? to false and destroyed? to true. Raises ActiveData::ObjectNotDestroyed in case of fail.
.destroy!
If destroy performer is not defined with ‘define_destroy`, it raises ActiveData::UndestroyableObject. Also destroy performer block might be passed instead of in-class performer definition:
.destroy! { REDIS.del(id) }
258 259 260 261 262 |
# File 'lib/active_data/model/lifecycle.rb', line 258 def destroy! &block raise ActiveData::UndestroyableObject unless block || destroyable? destroy_object(&block) or raise ActiveData::ObjectNotDestroyed self end |
#save(options = {}, &block) ⇒ Object
# Saves object by calling save performer defined with define_save, define_create or define_update methods. Returns true or false in case of successful or unsuccessful saving respectively. Changes persisted? to true
.save
If save performer is not defined with ‘define_update` or `define_create` or `define_save`, it raises ActiveData::UnsavableObject. Also save performer block might be passed instead of in-class performer definition:
.save { REDIS.set(id, attributes.to_json) }
199 200 201 202 |
# File 'lib/active_data/model/lifecycle.rb', line 199 def save = {}, &block raise ActiveData::UnsavableObject unless block || savable? valid? && save_object(&block) end |
#save!(options = {}, &block) ⇒ Object
Saves object by calling save performer defined with define_save, define_create or define_update methods. Returns true in case of success and raises ActiveData::ValidationError or ActiveData::ObjectNotSaved in case of validation or saving fail respectively. Changes persisted? to true
.save!
If save performer is not defined with ‘define_update` or `define_create` or `define_save`, it raises ActiveData::UnsavableObject. Also save performer block might be passed instead of in-class performer definition:
.save! { REDIS.set(id, attributes.to_json) }
219 220 221 222 223 |
# File 'lib/active_data/model/lifecycle.rb', line 219 def save! = {}, &block raise ActiveData::UnsavableObject unless block || savable? validate! save_object(&block) or raise ActiveData::ObjectNotSaved end |
#update(attributes, &block) ⇒ Object Also known as: update_attributes
Assigns passed attributes and calls save Returns true or false in case of successful or unsuccessful saving respectively.
.update(name: 'Donald')
If update performer is not defined with ‘define_update` or `define_save`, it raises ActiveData::UnsavableObject. Also save performer block might be passed instead of in-class performer definition:
.update(name: 'Donald') { REDIS.set(id, attributes.to_json) }
161 162 163 |
# File 'lib/active_data/model/lifecycle.rb', line 161 def update attributes, &block assign_attributes(attributes) && save(&block) end |
#update!(attributes, &block) ⇒ Object Also known as: update_attributes!
Assigns passed attributes and calls save! Returns true in case of success and raises ActiveData::ValidationError or ActiveData::ObjectNotSaved in case of validation or saving fail respectively.
.update!(name: 'Donald')
If update performer is not defined with ‘define_update` or `define_save`, it raises ActiveData::UnsavableObject. Also save performer block might be passed instead of in-class performer definition:
.update!(name: 'Donald') { REDIS.set(id, attributes.to_json) }
180 181 182 |
# File 'lib/active_data/model/lifecycle.rb', line 180 def update! attributes, &block assign_attributes(attributes) && save!(&block) end |