Class: ProcBuffer

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/proc_buffer.rb,
lib/proc_buffer/version.rb

Overview

TODO: Documentation ProcBuffer

Constant Summary collapse

VERSION =
'0.1.0'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(*actions) ⇒ ProcBuffer

Returns a new instance of ProcBuffer.



8
9
10
11
# File 'lib/proc_buffer.rb', line 8

def initialize(*actions)
  # For now we will not handle actions that do not respond to #call
  @actions = actions.select { |action| action.respond_to?(:call) }
end

Instance Method Details

#actionsObject



13
14
15
# File 'lib/proc_buffer.rb', line 13

def actions
  @actions.dup
end

#append(&block) ⇒ Object Also known as: push



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

def append(&block)
  @actions << block
end

#eachObject



17
18
19
# File 'lib/proc_buffer.rb', line 17

def each
  @actions.each { |action| yield action }
end

#pop(n = nil) ⇒ Object



35
36
37
38
# File 'lib/proc_buffer.rb', line 35

def pop(n = nil)
  return @actions.pop.call unless n
  (1..n).map { @actions.pop.call }
end

#reverse_runObject



25
26
27
# File 'lib/proc_buffer.rb', line 25

def reverse_run
  @actions.reverse.map { |action| call(action) }
end

#runObject



21
22
23
# File 'lib/proc_buffer.rb', line 21

def run
  map(&:call)
end