Class: Hollaback::Sequence

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}, &main) ⇒ Sequence

Returns a new instance of Sequence.



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

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.



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

def afters
  @afters
end

#beforesObject (readonly)

Returns the value of attribute befores.



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

def befores
  @befores
end

#mainObject (readonly)

Returns the value of attribute main.



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

def main
  @main
end

Instance Method Details

#after(&after) ⇒ Object



16
17
18
19
# File 'lib/hollaback/sequence.rb', line 16

def after(&after)
  afters << after
  self
end

#around(&around) ⇒ Object

rubocop:disable Performance/RedundantBlockCall



22
23
24
25
26
# File 'lib/hollaback/sequence.rb', line 22

def around(&around)
  Sequence.new do |target|
    around.call(target) { call(target) }
  end
end

#before(&before) ⇒ Object



11
12
13
14
# File 'lib/hollaback/sequence.rb', line 11

def before(&before)
  befores << before
  self
end

#call(target) ⇒ Object



28
29
30
31
32
33
# File 'lib/hollaback/sequence.rb', line 28

def call(target)
  befores.each { |before| before.call(target) }
  value = main.call(target)
  afters.each { |after| after.call(target) }
  value
end