Class: Kommando::Stdout

Inherits:
Object
  • Object
show all
Defined in:
lib/kommando/stdout.rb

Instance Method Summary collapse

Constructor Details

#initialize(shell) ⇒ Stdout

Returns a new instance of Stdout.



2
3
4
5
6
7
8
# File 'lib/kommando/stdout.rb', line 2

def initialize(shell)
  @buffer = []
  @matchers = []
  @matcher_buffer = ""

  @shell = shell
end

Instance Method Details

#<<(c) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/kommando/stdout.rb', line 10

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

#to_sObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/kommando/stdout.rb', line 32

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