Sha256: e9ed6235d8cbd7d8477f4f94dff61069364f6e19b2d9e797606810bb31a021e3

Contents?: true

Size: 1.27 KB

Versions: 8

Compression:

Stored size: 1.27 KB

Contents

module Stages
  class Stage
    attr_accessor :source
    
    def initialize(&block)
      @block = block
      initialize_loop
    end
    
    def initialize_loop
       @fiber_delegate = Fiber.new do
        process
        die
      end
    end
    
    def run
      @fiber_delegate.resume    
    end
    
    #seperate from reset! for restrict/resume purposes.
    def reset
      initialize_loop
      @source.reset if @source
    end
    
    def reset!
      initialize_loop
      @source.reset! if @source
    end
    
    def die
      loop do
        output nil
      end
    end
    
    def process
      while value = input
        handle_value value
      end
    end
    
    def handle_value(value)
      output value
    end
    
    def input
      source.run
    end
    
    def output(value)
      Fiber.yield value
    end
      
    def |(other)
      other.root_source.source = self
      other
    end
    
    def root_source
      source.nil? ? self : source.root_source
    end
    
    def drop_leftmost!
      if @source.end?
        @source = nil
      else
        @source.drop_leftmost!
      end
    end
    
    def end?
      @source.nil?
    end
    
    def length
      if source
        source.length + 1
      else
        1
      end
    end
  end  
end 
 

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
stages-0.2.8 lib/stage_base.rb
stages-0.2.7 lib/stage_base.rb
stages-0.2.6 lib/stage_base.rb
stages-0.2.5 lib/stage_base.rb
stages-0.2.4 lib/stage_base.rb
stages-0.2.3 lib/stage_base.rb
stages-0.2.2 lib/stage_base.rb
stages-0.2.1 lib/stage_base.rb