Class: StatSailr::Output::OutputMessage

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/statsailr/sts_output/output_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, content, parent) ⇒ OutputMessage

Returns a new instance of OutputMessage.



12
13
14
15
16
17
18
19
20
# File 'lib/statsailr/sts_output/output_manager.rb', line 12

def initialize(type, content, parent)
  @type = type
  @content = content
  @parent = parent

  if capture == false
    print @content
  end
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



10
11
12
# File 'lib/statsailr/sts_output/output_manager.rb', line 10

def content
  @content
end

#typeObject (readonly)

Returns the value of attribute type.



9
10
11
# File 'lib/statsailr/sts_output/output_manager.rb', line 9

def type
  @type
end

Instance Method Details

#run(stream) ⇒ Object

Raises:

  • (ArgumentError)


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
60
61
# File 'lib/statsailr/sts_output/output_manager.rb', line 33

def run(stream)
  raise ArgumentError, 'missing block' unless block_given?
  if capture == false
    begin 
      yield
    rescue => e
      raise e
    end
  else
    orig_stream = stream.dup
    IO.pipe do |r, w|
      # system call dup2() replaces the file descriptor 
      stream.reopen(w) 
      # there must be only one write end of the pipe;
      # otherwise the read end does not get an EOF 
      # by the final `reopen`
      w.close  # Now 'stream=$stdout' and 'r' are paired. orig_stream points to original stream(STDOUT).
      t = Thread.new { r.read }
      begin
        yield
      rescue => e
        raise e
      ensure
        stream.reopen orig_stream # restore file descriptor
        @content << t.value # join and get the result of the thread
      end
    end
  end
end

#set_content(content) ⇒ Object



22
23
24
25
26
27
# File 'lib/statsailr/sts_output/output_manager.rb', line 22

def set_content( content )
  if capture == false
    print content
  end
  @content = content
end

#to_sObject



29
30
31
# File 'lib/statsailr/sts_output/output_manager.rb', line 29

def to_s
  return @content.to_s
end