class Needle::Pipeline::Collection

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.

Public Class Methods

new( service_point ) click to toggle source

Create a new pipeline element collection, initially empty.

# File lib/needle/pipeline/collection.rb, line 49
def initialize( service_point )
  @elements = []
  @service_point = service_point
end

Public Instance Methods

add( *args, &block ) click to toggle source

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
def add( *args, &block )
  name = priority = nil
  options = {}
  klass = nil
  element = nil

  if [ Symbol, String ].any? { |i| args.first.is_a?( i ) }
    name = args.shift.to_s.intern
  end
  priority = args.shift if args.first.is_a?( Numeric )
  klass = args.shift if args.first.respond_to?( :new ) && block.nil?
  options = args.shift if args.first.is_a?( Hash )

  raise ArgumentError,
    "bad argument list #{args.inspect}" unless args.empty?

  if block
    element = BlockElement.new( @service_point, name, priority,
      options, block )
  else
    klass ||= @service_point.container[:pipeline_elements].fetch( name )
    element = klass.new( @service_point, name, priority, options )
  end

  @elements << element
  self
end
chain_to( block ) click to toggle source

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
def chain_to( block )
  head = tail = nil
  @elements.sort.reverse.each do |el|
    if head
      tail.succ = el
      tail = el
    else # first time through...
      head = tail = el
    end
  end

  if tail
    tail.succ = block
    return head
  else
    return block
  end
end
get( name ) click to toggle source

Returns the first pipeline element with the given name, or nil if no such element exists.

# File lib/needle/pipeline/collection.rb, line 103
def get( name )
  name = name.to_s.intern
  @elements.find { |el| name == el.name }
end
reset!( *args ) click to toggle source

Clears the state for each named element. If no elements are named, all elements are cleared.

# File lib/needle/pipeline/collection.rb, line 110
def reset!( *args )
  @elements.each do |element|
    element.reset! if args.empty? || args.include?( element.name )
  end
  self
end