Module: CassandraMapper::Support::SupportCallbacks
- Extended by:
- Concern
- Defined in:
- lib/cassandra_mapper/support/support_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
set_callback :save, :before, :saving_message
def
puts "saving..."
end
set_callback :save, :after do |object|
puts "saved"
end
def save
run_callbacks :save 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
set_callback :save, :before, :prepare
def prepare
puts "preparing save"
end
end
class ConfigStorage < Storage
set_callback :save, :before, :saving_message
def
puts "saving..."
end
set_callback :save, :after do |object|
puts "saved"
end
def save
run_callbacks :save 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
Instance Method Summary collapse
Methods included from Concern
append_features, extended, included
Instance Method Details
#run_callbacks(kind, *args, &block) ⇒ Object
94 95 96 |
# File 'lib/cassandra_mapper/support/support_callbacks.rb', line 94 def run_callbacks(kind, *args, &block) send("_run_#{kind}_callbacks", *args, &block) end |