Class: Maxwell::Agent::Middleware::Chain
- Inherits:
-
Object
- Object
- Maxwell::Agent::Middleware::Chain
- Includes:
- Enumerable
- Defined in:
- lib/maxwell/agent/middleware/chain.rb
Defined Under Namespace
Classes: Entry
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
Instance Method Summary collapse
- #add(klass, *args) ⇒ Object
- #each(&block) ⇒ Object
-
#initialize {|_self| ... } ⇒ Chain
constructor
A new instance of Chain.
- #insert_after(existing_klass, new_klass, *args) ⇒ Object
- #insert_before(existing_klass, new_klass, *args) ⇒ Object
- #invoke(*args, &final_action) ⇒ Object
- #remove(entry) ⇒ Object
Constructor Details
#initialize {|_self| ... } ⇒ Chain
Returns a new instance of Chain.
8 9 10 11 |
# File 'lib/maxwell/agent/middleware/chain.rb', line 8 def initialize @entries = [] yield self if block_given? end |
Instance Attribute Details
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
6 7 8 |
# File 'lib/maxwell/agent/middleware/chain.rb', line 6 def entries @entries end |
Instance Method Details
#add(klass, *args) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/maxwell/agent/middleware/chain.rb', line 17 def add(klass, *args) new_entry = Entry.new(klass, *args) if count > 0 add_at(count + 1, new_entry) else add_at(count, new_entry) end end |
#each(&block) ⇒ Object
13 14 15 |
# File 'lib/maxwell/agent/middleware/chain.rb', line 13 def each(&block) entries.each(&block) end |
#insert_after(existing_klass, new_klass, *args) ⇒ Object
36 37 38 39 40 |
# File 'lib/maxwell/agent/middleware/chain.rb', line 36 def insert_after(existing_klass, new_klass, *args) new_entry = Entry.new(new_klass, *args) i = get_index(existing_klass) || count - 1 add_at(i + 1, new_entry) end |
#insert_before(existing_klass, new_klass, *args) ⇒ Object
30 31 32 33 34 |
# File 'lib/maxwell/agent/middleware/chain.rb', line 30 def insert_before(existing_klass, new_klass, *args) new_entry = Entry.new(new_klass, *args) i = get_index(existing_klass) || 0 add_at(i, new_entry) end |
#invoke(*args, &final_action) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/maxwell/agent/middleware/chain.rb', line 42 def invoke(*args, &final_action) chain = retrieve.dup traverse_chain = -> do if chain.empty? final_action.call if final_action else chain.shift.call(*args, &traverse_chain) end end traverse_chain.call end |
#remove(entry) ⇒ Object
26 27 28 |
# File 'lib/maxwell/agent/middleware/chain.rb', line 26 def remove(entry) entries.delete_if {|e| e } end |