Class: Hollaback::Sequence
- Inherits:
-
Object
- Object
- Hollaback::Sequence
- Defined in:
- lib/hollaback/sequence.rb
Overview
An object that contains a sequence of callbacks along with a main execution function
Instance Attribute Summary collapse
-
#afters ⇒ Object
readonly
Returns the value of attribute afters.
-
#befores ⇒ Object
readonly
Returns the value of attribute befores.
-
#main ⇒ Object
readonly
Returns the value of attribute main.
Instance Method Summary collapse
- #after(&after) ⇒ Object
- #around(&around) ⇒ Object
- #before(&before) ⇒ Object
- #call(target = nil) ⇒ Object
-
#initialize(args = {}, &main) ⇒ Sequence
constructor
A new instance of Sequence.
Constructor Details
#initialize(args = {}, &main) ⇒ Sequence
Returns a new instance of Sequence.
9 10 11 12 13 |
# File 'lib/hollaback/sequence.rb', line 9 def initialize(args = {}, &main) @main = main @befores = args.fetch(:befores, []) @afters = args.fetch(:afters, []) end |
Instance Attribute Details
#afters ⇒ Object (readonly)
Returns the value of attribute afters.
7 8 9 |
# File 'lib/hollaback/sequence.rb', line 7 def afters @afters end |
#befores ⇒ Object (readonly)
Returns the value of attribute befores.
7 8 9 |
# File 'lib/hollaback/sequence.rb', line 7 def befores @befores end |
#main ⇒ Object (readonly)
Returns the value of attribute main.
7 8 9 |
# File 'lib/hollaback/sequence.rb', line 7 def main @main end |
Instance Method Details
#after(&after) ⇒ Object
20 21 22 23 |
# File 'lib/hollaback/sequence.rb', line 20 def after(&after) afters << after self end |
#around(&around) ⇒ Object
25 26 27 28 29 |
# File 'lib/hollaback/sequence.rb', line 25 def around(&around) Sequence.new do |target| around.call(target) { call(target) } end end |
#before(&before) ⇒ Object
15 16 17 18 |
# File 'lib/hollaback/sequence.rb', line 15 def before(&before) befores << before self end |
#call(target = nil) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/hollaback/sequence.rb', line 31 def call(target = nil) befores.each { |before| before.call(target) } main.call(target).tap do afters.each { |after| after.call(target) } end end |