Class: Media::Command::Subshell

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/media/command/subshell.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Subshell

Returns a new instance of Subshell.



12
13
14
# File 'lib/media/command/subshell.rb', line 12

def initialize(args)
  @cmd = Array(args.fetch(:cmd))
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



10
11
12
# File 'lib/media/command/subshell.rb', line 10

def error
  @error
end

#outObject (readonly)

Returns the value of attribute out.



10
11
12
# File 'lib/media/command/subshell.rb', line 10

def out
  @out
end

#statusObject (readonly)

Returns the value of attribute status.



10
11
12
# File 'lib/media/command/subshell.rb', line 10

def status
  @status
end

Instance Method Details

#callObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/media/command/subshell.rb', line 16

def call
  @out, @error, @status = Open3.popen3(*@cmd) {|stdin,stdout,stderr,thread|
    
    out = Thread.new do
      stdout.each_with_object([]) do |line, memo|
        memo << line
        yield line.strip if block_given?
      end.join
    end
    
    err = Thread.new do
      stderr.each_with_object([]) do |line, memo|
        memo << line
        yield line.strip if block_given?
      end.join
    end
    
    stdin.close
    
    [out.value, err.value, thread.value]
  }
  self
end

#success?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/media/command/subshell.rb', line 40

def success?
  exitstatus == 0
end