Sha256: 263e762a52a70d0f41c4e023a7c06f73c2c63aa9f2878213b31e3c2d016f9275
Contents?: true
Size: 1.85 KB
Versions: 7
Compression:
Stored size: 1.85 KB
Contents
module Toro module Middleware class Chain include Enumerable attr_reader :entries def initialize_copy(copy) copy.instance_variable_set(:@entries, entries.dup) end def each(&block) entries.each(&block) end def initialize @entries = [] yield self if block_given? end def remove(klass) entries.delete_if { |entry| entry.klass == klass } end def add(klass, *args) remove(klass) if exists?(klass) entries << Entry.new(klass, *args) end 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 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 def exists?(klass) any? { |entry| entry.klass == klass } end def retrieve map(&:make_new) end def clear entries.clear end def invoke(*args, &final_action) chain = retrieve.dup traverse_chain = lambda do if chain.empty? final_action.call else chain.shift.call(*args, &traverse_chain) end end traverse_chain.call end end class Entry attr_reader :klass def initialize(klass, *args) @klass = klass @args = args end def make_new @klass.new(*@args) end end end end
Version data entries
7 entries across 7 versions & 1 rubygems