Class: Hollaback::Chain

Inherits:
Object
  • Object
show all
Defined in:
lib/hollaback/chain.rb

Overview

A set of callbacks

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeChain

Returns a new instance of Chain.



8
9
10
# File 'lib/hollaback/chain.rb', line 8

def initialize
  @callbacks = []
end

Instance Attribute Details

#callbacksObject (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

Returns:

  • (Boolean)


29
30
31
# File 'lib/hollaback/chain.rb', line 29

def empty?
  callbacks.empty?
end