Class: MicroQ::Middleware::Chain::Base
- Inherits:
-
Object
- Object
- MicroQ::Middleware::Chain::Base
- Defined in:
- lib/micro_q/middleware/chain.rb
Instance Method Summary collapse
-
#add(*items) ⇒ Object
Add any number of entries to the middleware chain.
- #add_after(after, *items) ⇒ Object
- #add_before(before, *items) ⇒ Object
-
#call(*args, &block) ⇒ Object
Traverse the middleware chain by recursing until we reach the end of the chain and are able to invoke the given block.
- #clear ⇒ Object
- #entries ⇒ Object
-
#initialize ⇒ Base
constructor
A new instance of Base.
-
#remove(*items) ⇒ Object
Remove any number of entries from the middleware chain.
Constructor Details
#initialize ⇒ Base
Returns a new instance of Base.
43 44 45 |
# File 'lib/micro_q/middleware/chain.rb', line 43 def initialize clear end |
Instance Method Details
#add(*items) ⇒ Object
Add any number of entries to the middleware chain
54 55 56 |
# File 'lib/micro_q/middleware/chain.rb', line 54 def add(*items) @entries.concat(items.flatten).uniq! end |
#add_after(after, *items) ⇒ Object
63 64 65 66 |
# File 'lib/micro_q/middleware/chain.rb', line 63 def add_after(after, *items) remove(*items) @entries.insert(@entries.index(after)+1, *items).uniq! if items.any? end |
#add_before(before, *items) ⇒ Object
58 59 60 61 |
# File 'lib/micro_q/middleware/chain.rb', line 58 def add_before(before, *items) remove(*items) @entries.insert(@entries.index(before), *items).uniq! if items.any? end |
#call(*args, &block) ⇒ Object
Traverse the middleware chain by recursing until we reach the end of the chain and are able to invoke the given block. The block represents a message push (client) or a message invocation (server).
It is, however very generic and can be used as middleware around anything.
88 89 90 91 92 93 94 95 |
# File 'lib/micro_q/middleware/chain.rb', line 88 def call(*args, &block) chain = (index = -1) && -> { (index += 1) == entries.length ? block.call : entries.at(index).new.call(*args, &chain) } chain.call end |
#clear ⇒ Object
77 78 79 |
# File 'lib/micro_q/middleware/chain.rb', line 77 def clear @entries = [] end |
#entries ⇒ Object
47 48 49 |
# File 'lib/micro_q/middleware/chain.rb', line 47 def entries [*@entries, @last].compact end |
#remove(*items) ⇒ Object
Remove any number of entries from the middleware chain
71 72 73 74 75 |
# File 'lib/micro_q/middleware/chain.rb', line 71 def remove(*items) @entries.tap do items.flatten.each {|item| @entries.delete(item) } end end |