Sha256: d50117f6387a6165f8802e6d09aacf1d7e7d2b329e33764131033b700cd53a9f

Contents?: true

Size: 1.86 KB

Versions: 3

Compression:

Stored size: 1.86 KB

Contents

module Crystal
  class Conveyor
    inject :workspace => :workspace
    
    def definitions
      @definitions ||= []
    end
        
    def use processor_class, *initialization_arguments
      definitions << [processor_class, initialization_arguments]
    end
    
    def build!
      @chain = lambda{}      
      definitions.reverse.collect do |klass, args| 
        klass.must_be.a Class
        @chain = klass.new @chain, *args
      end
    end
    
    def call workspace_content = {}, &block
      build! unless @chain
      
      crystal.activate :cycle, {} do        
        self.workspace = Workspace.new
        workspace.merge! workspace_content 
        
        if block
          block.call{@chain.call}
        else
          @chain.call
        end
        
        workspace
      end
    end
    
    def inspect
      definitions.inspect
    end
    
    # def trace name, &block
    #   start_time = Time.now
    #   block.call
    #   (workspace.trace ||= []) << [name, Time.now - start_time]
    # end       
    
    protected
      # def build_chain
      #   next_processor_call = nil
      #   chain = reverse.collect do |processor|
      #     next_processor_call = if next_processor_call
      #       lambda{processor.call next_processor}
      #     else
      #       lambda{processor.call lambda{}}
      #     end
      #   end
      #   next_processor_call
      # end
    
    
    # def add_before key, *processors
    #   index = index{|processor| processor.name == key or processor.class == key}
    #   raise "Can't find Processor '#{name}'!" unless index
    #   insert index, *Array(processors)
    # end
    # 
    # def add_after key, *processors
    #   index = rindex{|processor| processor.name == key or processor.class == key}
    #   raise "Can't find Processor '#{name}'!" unless index
    #   insert index + 1, *Array(processors)
    # end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
crystal-0.0.13 lib/crystal/conveyor/conveyor.rb
crystal-0.0.12 lib/crystal/conveyor/conveyor.rb
crystal_ext-0.0.11 lib/crystal/conveyor/conveyor.rb