Class: Jsm::Callbacks::Chain
- Inherits:
-
Object
- Object
- Jsm::Callbacks::Chain
- Defined in:
- lib/jsm/callbacks/chain.rb
Overview
chain is a repository to store list of callbacks of an event, for example store before and after callback of an event called ‘run` e.g: before_callback = Jsm::Callbacks::Callback.new(:before) do |user| log.info(’before callback’) end before_callback2 = Jsm::Callbacks::Callback.new(:before) do |user|
user.age = 20
end after_callback = Jsm::Callbacks::Callback.new(:after) do |result, user|
log.info('after callback')
end chain = Jsm::Callbacks::Chain.new(:run) chain.insert_callback(before_callback) chain.insert_callback(before_callback2) chain.insert_callback(after_callback) user = User.new result = chain.compile user do |user|
user.address = 'Indonesia'
end result -> true log -> before callback after callback user.address -> ‘Indonesia’
Instance Attribute Summary collapse
-
#callbacks ⇒ Object
readonly
Returns the value of attribute callbacks.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Instance Method Summary collapse
- #compile(*args, &block) ⇒ Object
-
#initialize(context) ⇒ Chain
constructor
A new instance of Chain.
- #insert_callback(callback) ⇒ Object
Constructor Details
#initialize(context) ⇒ Chain
Returns a new instance of Chain.
27 28 29 30 |
# File 'lib/jsm/callbacks/chain.rb', line 27 def initialize(context) @context = context @callbacks = [] end |
Instance Attribute Details
#callbacks ⇒ Object (readonly)
Returns the value of attribute callbacks.
26 27 28 |
# File 'lib/jsm/callbacks/chain.rb', line 26 def callbacks @callbacks end |
#context ⇒ Object (readonly)
Returns the value of attribute context.
26 27 28 |
# File 'lib/jsm/callbacks/chain.rb', line 26 def context @context end |
Instance Method Details
#compile(*args, &block) ⇒ Object
36 37 38 |
# File 'lib/jsm/callbacks/chain.rb', line 36 def compile(*args, &block) arrange_callbacks(*args, &block) end |
#insert_callback(callback) ⇒ Object
32 33 34 |
# File 'lib/jsm/callbacks/chain.rb', line 32 def insert_callback(callback) @callbacks.push(callback) end |