Module: ActiveData::Model::Lifecycle

Extended by:
ActiveSupport::Concern
Included in:
Base
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

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.

author.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:

author.destroy { REDIS.del(id) }


236
237
238
239
240
# File 'lib/active_data/model/lifecycle.rb', line 236

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.

author.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:

author.destroy! { REDIS.del(id) }


256
257
258
259
260
# File 'lib/active_data/model/lifecycle.rb', line 256

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

author.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:

author.save { REDIS.set(id, attributes.to_json) }


197
198
199
200
# File 'lib/active_data/model/lifecycle.rb', line 197

def save(_options = {}, &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

author.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:

author.save! { REDIS.set(id, attributes.to_json) }


217
218
219
220
221
# File 'lib/active_data/model/lifecycle.rb', line 217

def save!(_options = {}, &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.

author.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:

author.update(name: 'Donald') { REDIS.set(id, attributes.to_json) }


159
160
161
# File 'lib/active_data/model/lifecycle.rb', line 159

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.

author.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:

author.update!(name: 'Donald') { REDIS.set(id, attributes.to_json) }


178
179
180
# File 'lib/active_data/model/lifecycle.rb', line 178

def update!(attributes, &block)
  assign_attributes(attributes) && save!(&block)
end