Class: Hollaback::Chain
- Inherits:
-
Object
- Object
- Hollaback::Chain
- Defined in:
- lib/hollaback/chain.rb
Overview
A set of callbacks
Instance Attribute Summary collapse
-
#callbacks ⇒ Object
readonly
Returns the value of attribute callbacks.
Instance Method Summary collapse
- #+(other) ⇒ Object
- #after(execute = nil, &block) ⇒ Object
- #around(execute = nil, &block) ⇒ Object
- #before(execute = nil, &block) ⇒ Object
- #compile(&block) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize ⇒ Chain
constructor
A new instance of Chain.
Constructor Details
#initialize ⇒ Chain
Returns a new instance of Chain.
8 9 10 |
# File 'lib/hollaback/chain.rb', line 8 def initialize @callbacks = [] end |
Instance Attribute Details
#callbacks ⇒ Object (readonly)
Returns the value of attribute callbacks.
6 7 8 |
# File 'lib/hollaback/chain.rb', line 6 def callbacks @callbacks end |
Instance Method Details
#+(other) ⇒ Object
12 13 14 15 |
# File 'lib/hollaback/chain.rb', line 12 def +(other) @callbacks += other.callbacks self end |
#after(execute = nil, &block) ⇒ Object
21 22 23 |
# File 'lib/hollaback/chain.rb', line 21 def after(execute = nil, &block) build(:after, execute, &block) end |
#around(execute = nil, &block) ⇒ Object
25 26 27 |
# File 'lib/hollaback/chain.rb', line 25 def around(execute = nil, &block) build(:around, execute, &block) end |
#before(execute = nil, &block) ⇒ Object
17 18 19 |
# File 'lib/hollaback/chain.rb', line 17 def before(execute = nil, &block) build(:before, execute, &block) end |
#compile(&block) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/hollaback/chain.rb', line 33 def compile(&block) if empty? block else callbacks.inject(Sequence.new(&block)) do |sequence, callback| sequence.send(callback.type, &callback.build) end end end |
#empty? ⇒ Boolean
29 30 31 |
# File 'lib/hollaback/chain.rb', line 29 def empty? callbacks.empty? end |