Class: Hollaback::Chain

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeChain

Returns a new instance of Chain.



5
6
7
# File 'lib/hollaback/chain.rb', line 5

def initialize
  @callbacks = []
end

Instance Attribute Details

#callbacksObject (readonly)

Returns the value of attribute callbacks.



3
4
5
# File 'lib/hollaback/chain.rb', line 3

def callbacks
  @callbacks
end

Instance Method Details

#+(other) ⇒ Object



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

def +(other)
  @callbacks += other.callbacks
  self
end

#after(execute = nil, &block) ⇒ Object



18
19
20
# File 'lib/hollaback/chain.rb', line 18

def after(execute = nil, &block)
  build(:after, execute, &block)
end

#around(execute = nil, &block) ⇒ Object



22
23
24
# File 'lib/hollaback/chain.rb', line 22

def around(execute = nil, &block)
  build(:around, execute, &block)
end

#before(execute = nil, &block) ⇒ Object



14
15
16
# File 'lib/hollaback/chain.rb', line 14

def before(execute = nil, &block)
  build(:before, execute, &block)
end

#compile(&block) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/hollaback/chain.rb', line 30

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)


26
27
28
# File 'lib/hollaback/chain.rb', line 26

def empty?
  callbacks.empty?
end