Class: PikaQue::Middleware::Chain

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pika_que/middleware/chain.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Chain

Returns a new instance of Chain.

Yields:

  • (_self)

Yield Parameters:



19
20
21
22
# File 'lib/pika_que/middleware/chain.rb', line 19

def initialize
  @entries = []
  yield self if block_given?
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



9
10
11
# File 'lib/pika_que/middleware/chain.rb', line 9

def entries
  @entries
end

Instance Method Details

#add(klass, *args) ⇒ Object



28
29
30
31
# File 'lib/pika_que/middleware/chain.rb', line 28

def add(klass, *args)
  remove(klass) if exists?(klass)
  entries << Entry.new(klass, *args)
end

#clearObject



60
61
62
# File 'lib/pika_que/middleware/chain.rb', line 60

def clear
  entries.clear
end

#each(&block) ⇒ Object



15
16
17
# File 'lib/pika_que/middleware/chain.rb', line 15

def each(&block)
  entries.each(&block)
end

#exists?(klass) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/pika_que/middleware/chain.rb', line 52

def exists?(klass)
  any? { |entry| entry.klass == klass }
end

#initialize_copy(copy) ⇒ Object



11
12
13
# File 'lib/pika_que/middleware/chain.rb', line 11

def initialize_copy(copy)
  copy.instance_variable_set(:@entries, entries.dup)
end

#insert_after(oldklass, newklass, *args) ⇒ Object



45
46
47
48
49
50
# File 'lib/pika_que/middleware/chain.rb', line 45

def insert_after(oldklass, newklass, *args)
  i = entries.index { |entry| entry.klass == newklass }
  new_entry = i.nil? ? Entry.new(newklass, *args) : entries.delete_at(i)
  i = entries.index { |entry| entry.klass == oldklass } || entries.count - 1
  entries.insert(i+1, new_entry)
end

#insert_before(oldklass, newklass, *args) ⇒ Object



38
39
40
41
42
43
# File 'lib/pika_que/middleware/chain.rb', line 38

def insert_before(oldklass, newklass, *args)
  i = entries.index { |entry| entry.klass == newklass }
  new_entry = i.nil? ? Entry.new(newklass, *args) : entries.delete_at(i)
  i = entries.index { |entry| entry.klass == oldklass } || 0
  entries.insert(i, new_entry)
end

#invoke(*args) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/pika_que/middleware/chain.rb', line 64

def invoke(*args)
  chain = retrieve.dup
  traverse_chain = lambda do
    if chain.empty?
      yield
    else
      chain.shift.call(*args, &traverse_chain)
    end
  end
  traverse_chain.call
end

#prepend(klass, *args) ⇒ Object



33
34
35
36
# File 'lib/pika_que/middleware/chain.rb', line 33

def prepend(klass, *args)
  remove(klass) if exists?(klass)
  entries.insert(0, Entry.new(klass, *args))
end

#remove(klass) ⇒ Object



24
25
26
# File 'lib/pika_que/middleware/chain.rb', line 24

def remove(klass)
  entries.delete_if { |entry| entry.klass == klass }
end

#retrieveObject



56
57
58
# File 'lib/pika_que/middleware/chain.rb', line 56

def retrieve
  map(&:make_new)
end