Sha256: 3e9329081d04e428d1a79518a1a9e77b1625f02d6c7d1350d3cc88490a4df987

Contents?: true

Size: 1.46 KB

Versions: 3

Compression:

Stored size: 1.46 KB

Contents

class Kommando; class Stdout
  def initialize(shell)
    @buffer = []
    @matchers = []
    @matcher_buffer = ""

    @shell = shell
  end

  def <<(c)
    @buffer << c
    @matcher_buffer << c

    matchers_copy = @matchers.clone # blocks can insert to @matchers while iteration is undergoing
    matchers_copy.each do |matcher|
      if (match_data = matcher.match @matcher_buffer)
        matcher.call match_data
        unless matcher.class == Kommando::Matchers::Every
          @matchers.delete matcher  #TODO: is this safe?
        end

        matcher.nested_matchers.each do |nested_matcher|
          if nested_matcher.class == Kommando::Matchers::Every
            nested_matcher.skip_by(@matcher_buffer)
          end
          @matchers = @matchers + [nested_matcher] #TODO: is this safe?
        end
      end
    end
  end

  def to_s
    string = @buffer.join ""

    #TODO: this behaviour maybe makes no sense?
    string.strip! if @shell

    matchers = @matchers
    #TODO: deprecate .on
    string.define_singleton_method(:on) do |regexp, &block|
      m = Kommando::Matchers::Once.new regexp, block
      matchers << m
      m
    end

    string.define_singleton_method(:once) do |regexp, &block|
      m = Kommando::Matchers::Once.new regexp, block
      matchers << m
      m
    end

    string.define_singleton_method(:every) do |regexp, &block|
      m = Kommando::Matchers::Every.new regexp, block
      matchers << m
      m
    end

    string
  end

end; end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
kommando-0.1.2 lib/kommando/stdout.rb
kommando-0.1.1 lib/kommando/stdout.rb
kommando-0.1.0 lib/kommando/stdout.rb