Module: CouchRest::Mixins::Callbacks
- Included in:
- ExtendedDocument
- Defined in:
- lib/couchrest/mixins/callbacks.rb
Overview
Callbacks are hooks into the lifecycle of an object that allow you to trigger logic before or after an alteration of the object state.
Mixing in this module allows you to define callbacks in your class.
Example:
class Storage
include ActiveSupport::Callbacks
define_callbacks :save
end
class ConfigStorage < Storage
save_callback :before, :saving_message
def
puts "saving..."
end
save_callback :after do |object|
puts "saved"
end
def save
_run_save_callbacks do
puts "- save"
end
end
end
config = ConfigStorage.new
config.save
Output:
saving...
- save
saved
Callbacks from parent classes are inherited.
Example:
class Storage
include ActiveSupport::Callbacks
define_callbacks :save
save_callback :before, :prepare
def prepare
puts "preparing save"
end
end
class ConfigStorage < Storage
save_callback :before, :saving_message
def
puts "saving..."
end
save_callback :after do |object|
puts "saved"
end
def save
_run_save_callbacks do
puts "- save"
end
end
end
config = ConfigStorage.new
config.save
Output:
preparing save
saving...
- save
saved
Defined Under Namespace
Modules: ClassMethods Classes: Callback, CallbackChain
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.included(klass) ⇒ Object
105 106 107 |
# File 'lib/couchrest/mixins/callbacks.rb', line 105 def self.included(klass) klass.extend ClassMethods end |
Instance Method Details
#run_callbacks(kind, options = {}, &blk) ⇒ Object
109 110 111 |
# File 'lib/couchrest/mixins/callbacks.rb', line 109 def run_callbacks(kind, = {}, &blk) send("_run_#{kind}_callbacks", &blk) end |