Module: PgEventstore::Extensions::CallbacksExtension
- Included in:
- BasicRunner
- Defined in:
- lib/pg_eventstore/extensions/callbacks_extension.rb
Overview
Integrates PgEventstore::Calbacks into your object. Example usage:
class MyAwesomeClass
include CallbacksExtension
end
Now you have #define_callback public method to define callbacks outside your class’ object, and you can use #callbacks private method to run callbacks inside your class’ object. You can also use .has_callbacks public class method to wrap the desired method into Callbacks#run_callbacks. Example:
class MyAwesomeClass
include PgEventstore::Extensions::CallbacksExtension
def initialize(foo)
@foo = foo
end
def do_something
puts "I did something useful: #{@foo.inspect}!"
end
has_callbacks :something_happened, :do_something
def do_something_else
callbacks.run_callbacks(:something_else_happened) do
puts "I did something else!"
end
end
end
obj = MyAwesomeClass.new(:foo)
obj.define_callback(
:something_happened, :before, proc { puts "In before callback of :something_happened." }
)
obj.define_callback(
:something_else_happened, :before, proc { puts "In before callback of :something_else_happened." }
)
obj.do_something
obj.do_something_else
Outputs:
In before callback of :something_happened.
I did something useful: :foo!
In before callback of :something_else_happened.
I did something else!
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.included(klass) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/pg_eventstore/extensions/callbacks_extension.rb', line 46 def self.included(klass) klass.extend(ClassMethods) klass.prepend(InitCallbacks) klass.class_eval do attr_reader :callbacks private :callbacks end end |
Instance Method Details
#define_callback ⇒ void
This method returns an undefined value.
56 57 58 |
# File 'lib/pg_eventstore/extensions/callbacks_extension.rb', line 56 def define_callback(...) callbacks.define_callback(...) end |