Class: PikaQue::Middleware::Chain
- Inherits:
-
Object
- Object
- PikaQue::Middleware::Chain
- Includes:
- Enumerable
- Defined in:
- lib/pika_que/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.
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
#entries ⇒ Object (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 |
#clear ⇒ Object
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
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 |
#retrieve ⇒ Object
56 57 58 |
# File 'lib/pika_que/middleware/chain.rb', line 56 def retrieve map(&:make_new) end |