Class: Hollaback::Sequence

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#aftersObject (readonly)

Returns the value of attribute afters.



7
8
9
# File 'lib/hollaback/sequence.rb', line 7

def afters
  @afters
end

#beforesObject (readonly)

Returns the value of attribute befores.



7
8
9
# File 'lib/hollaback/sequence.rb', line 7

def befores
  @befores
end

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