Class | Needle::Pipeline::Collection |
In: |
lib/needle/pipeline/collection.rb
|
Parent: | Object |
Represents a collection of pipeline elements. Elements may be added to the pipeline, and then returned as a single object representing the chain of pipeline elements.
Create a new pipeline element collection, initially empty.
# File lib/needle/pipeline/collection.rb, line 49 49: def initialize( service_point ) 50: @elements = [] 51: @service_point = service_point 52: end
Add a new pipeline element to this collection. If the first parameter is a symbol or a string, it is taken to be the name of the element to add. If no explicit implementation is given, then the implementation is looked up in the :pipeline_elements service, using the given name.
After the first parameter, if the next parameter is numeric, it is taken to mean the priority of the element. This overrides whatever default priority is given for the selected element.
If the next parameter responds to the :new message, it is taken to be an explicit implementation of the element. This is only valid if a block is not given. If a block is given, then it is always taken to be the implementation of the element.
If the last parameter is a Hash, it is taken to be a map of options that should be passed to the element when it is instantiated.
This returns self, so that add calls may be chained.
# File lib/needle/pipeline/collection.rb, line 73 73: def add( *args, &block ) 74: name = priority = nil 75: options = {} 76: klass = nil 77: element = nil 78: 79: if [ Symbol, String ].any? { |i| args.first.is_a?( i ) } 80: name = args.shift.to_s.intern 81: end 82: priority = args.shift if args.first.is_a?( Numeric ) 83: klass = args.shift if args.first.respond_to?( :new ) && block.nil? 84: options = args.shift if args.first.is_a?( Hash ) 85: 86: raise ArgumentError, 87: "bad argument list #{args.inspect}" unless args.empty? 88: 89: if block 90: element = BlockElement.new( @service_point, name, priority, 91: options, block ) 92: else 93: klass ||= @service_point.container[:pipeline_elements].fetch( name ) 94: element = klass.new( @service_point, name, priority, options ) 95: end 96: 97: @elements << element 98: self 99: end
Builds a linked list of the elements, with the last element being the block (or block-like) object given by the parameter. Higher-priority elements will be closer to the head of the list.
# File lib/needle/pipeline/collection.rb, line 120 120: def chain_to( block ) 121: head = tail = nil 122: @elements.sort.reverse.each do |el| 123: if head 124: tail.succ = el 125: tail = el 126: else # first time through... 127: head = tail = el 128: end 129: end 130: 131: if tail 132: tail.succ = block 133: return head 134: else 135: return block 136: end 137: end
Returns the first pipeline element with the given name, or nil if no such element exists.
# File lib/needle/pipeline/collection.rb, line 103 103: def get( name ) 104: name = name.to_s.intern 105: @elements.find { |el| name == el.name } 106: end