Sha256: 80a55f0c5a5c9688223abe572737100e84eccd73cedd8d344672ee19a8e914c5

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 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.nil? ? nil : source.run
    end

    def output(value)
      Fiber.yield value
    end

    def |(other)
      return self if other.nil?
      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

2 entries across 2 versions & 1 rubygems

Version Path
stages-0.3.4 lib/stage_base.rb
stages-0.3.3 lib/stage_base.rb