Class: Tonic::Shell

Inherits:
String
  • Object
show all
Defined in:
lib/tonic/shell.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*command) ⇒ Shell

Returns a new instance of Shell.



16
17
18
# File 'lib/tonic/shell.rb', line 16

def initialize(*command)
  @command = command
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



9
10
11
# File 'lib/tonic/shell.rb', line 9

def error
  @error
end

#outObject (readonly)

Returns the value of attribute out.



10
11
12
# File 'lib/tonic/shell.rb', line 10

def out
  @out
end

Class Method Details

.run(*command) ⇒ Object



12
13
14
# File 'lib/tonic/shell.rb', line 12

def self.run(*command)
  self.new(*command).run
end

Instance Method Details

#commandObject



33
34
35
# File 'lib/tonic/shell.rb', line 33

def command
  @command.join(' ')
end

#error?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/tonic/shell.rb', line 37

def error?
  !!@error
end

#out?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/tonic/shell.rb', line 41

def out?
  !!@out
end

#runObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/tonic/shell.rb', line 20

def run
  out = err = nil
  Open3.popen3(*@command) do |_, pout, perr|
    out = pout.read.strip
    err = perr.read.strip
  end

  replace @error = err unless err.empty?
  replace @out = out unless out.empty?

  self
end