Class: Subserver::Middleware::Chain
- Inherits:
-
Object
- Object
- Subserver::Middleware::Chain
- Includes:
- Enumerable
- Defined in:
- lib/subserver/middleware/chain.rb
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
Instance Method Summary collapse
- #add(klass, *args) ⇒ Object
- #clear ⇒ Object
- #each(&block) ⇒ Object
- #exists?(klass) ⇒ Boolean
-
#initialize {|_self| ... } ⇒ Chain
constructor
A new instance of Chain.
- #initialize_copy(copy) ⇒ Object
- #insert_after(oldklass, newklass, *args) ⇒ Object
- #insert_before(oldklass, newklass, *args) ⇒ Object
- #invoke(*args) ⇒ Object
- #prepend(klass, *args) ⇒ Object
- #remove(klass) ⇒ Object
- #retrieve ⇒ Object
Constructor Details
#initialize {|_self| ... } ⇒ Chain
Returns a new instance of Chain.
57 58 59 60 |
# File 'lib/subserver/middleware/chain.rb', line 57 def initialize @entries = [] yield self if block_given? end |
Instance Attribute Details
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
47 48 49 |
# File 'lib/subserver/middleware/chain.rb', line 47 def entries @entries end |
Instance Method Details
#add(klass, *args) ⇒ Object
66 67 68 69 |
# File 'lib/subserver/middleware/chain.rb', line 66 def add(klass, *args) remove(klass) if exists?(klass) entries << Entry.new(klass, *args) end |
#clear ⇒ Object
98 99 100 |
# File 'lib/subserver/middleware/chain.rb', line 98 def clear entries.clear end |
#each(&block) ⇒ Object
53 54 55 |
# File 'lib/subserver/middleware/chain.rb', line 53 def each(&block) entries.each(&block) end |
#exists?(klass) ⇒ Boolean
90 91 92 |
# File 'lib/subserver/middleware/chain.rb', line 90 def exists?(klass) any? { |entry| entry.klass == klass } end |
#initialize_copy(copy) ⇒ Object
49 50 51 |
# File 'lib/subserver/middleware/chain.rb', line 49 def initialize_copy(copy) copy.instance_variable_set(:@entries, entries.dup) end |
#insert_after(oldklass, newklass, *args) ⇒ Object
83 84 85 86 87 88 |
# File 'lib/subserver/middleware/chain.rb', line 83 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
76 77 78 79 80 81 |
# File 'lib/subserver/middleware/chain.rb', line 76 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
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/subserver/middleware/chain.rb', line 102 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
71 72 73 74 |
# File 'lib/subserver/middleware/chain.rb', line 71 def prepend(klass, *args) remove(klass) if exists?(klass) entries.insert(0, Entry.new(klass, *args)) end |
#remove(klass) ⇒ Object
62 63 64 |
# File 'lib/subserver/middleware/chain.rb', line 62 def remove(klass) entries.delete_if { |entry| entry.klass == klass } end |
#retrieve ⇒ Object
94 95 96 |
# File 'lib/subserver/middleware/chain.rb', line 94 def retrieve map(&:make_new) end |