class Maxwell::Agent::Middleware::Chain

Attributes

entries[R]

Public Class Methods

new() { |self| ... } click to toggle source
# File lib/maxwell/agent/middleware/chain.rb, line 8
def initialize
  @entries = []
  yield self if block_given?
end

Public Instance Methods

add(klass, *args) click to toggle source
# 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) click to toggle source
# File lib/maxwell/agent/middleware/chain.rb, line 13
def each(&block)
  entries.each(&block)
end
insert_after(existing_klass, new_klass, *args) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/maxwell/agent/middleware/chain.rb, line 26
def remove(entry)
  entries.delete_if {|e| e }
end

Private Instance Methods

add_at(index, new_entry) click to toggle source
# File lib/maxwell/agent/middleware/chain.rb, line 56
def add_at(index, new_entry)
  remove(new_entry) if exists?(new_entry)
  entries.insert(index, new_entry)
end
exists?(entry) click to toggle source
# File lib/maxwell/agent/middleware/chain.rb, line 65
def exists?(entry)
  include?(entry)
end
get_index(klass) click to toggle source
# File lib/maxwell/agent/middleware/chain.rb, line 61
def get_index(klass)
  find_index {|entry| entry.klass == klass }
end
retrieve() click to toggle source
# File lib/maxwell/agent/middleware/chain.rb, line 69
def retrieve
  entries.map(&:build)
end