Class: Shoryuken::Middleware::Chain

Inherits:
Object
  • Object
show all
Defined in:
lib/shoryuken/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:


47
48
49
50
# File 'lib/shoryuken/middleware/chain.rb', line 47

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

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries


45
46
47
# File 'lib/shoryuken/middleware/chain.rb', line 45

def entries
  @entries
end

Instance Method Details

#add(klass, *args) ⇒ Object


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

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

#clearObject


86
87
88
# File 'lib/shoryuken/middleware/chain.rb', line 86

def clear
  entries.clear
end

#dupObject


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

def dup
  self.class.new.tap { |new_chain| new_chain.entries.replace(entries) }
end

#exists?(klass) ⇒ Boolean

Returns:

  • (Boolean)

78
79
80
# File 'lib/shoryuken/middleware/chain.rb', line 78

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

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


71
72
73
74
75
76
# File 'lib/shoryuken/middleware/chain.rb', line 71

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.find_index { |entry| entry.klass == oldklass } || entries.count - 1
  entries.insert(i + 1, new_entry)
end

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


64
65
66
67
68
69
# File 'lib/shoryuken/middleware/chain.rb', line 64

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.find_index { |entry| entry.klass == oldklass } || 0
  entries.insert(i, new_entry)
end

#invoke(*args, &final_action) ⇒ Object


90
91
92
93
94
95
96
97
98
99
100
# File 'lib/shoryuken/middleware/chain.rb', line 90

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

#remove(klass) ⇒ Object


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

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

#retrieveObject


82
83
84
# File 'lib/shoryuken/middleware/chain.rb', line 82

def retrieve
  entries.map(&:make_new)
end