Class: ChupaText::ExternalCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/chupa-text/external-command.rb

Defined Under Namespace

Classes: SpawnLimitOptions

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ ExternalCommand

Returns a new instance of ExternalCommand.



23
24
25
# File 'lib/chupa-text/external-command.rb', line 23

def initialize(command)
  @command = Pathname.new(command)
end

Instance Method Details

#exist?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
# File 'lib/chupa-text/external-command.rb', line 42

def exist?
  if @command.absolute?
    @command.file? and @command.executable?
  else
    (ENV['PATH'] || "").split(File::PATH_SEPARATOR).any? do |path|
      (Pathname.new(path) + @command).expand_path.exist?
    end
  end
end

#run(*arguments) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/chupa-text/external-command.rb', line 27

def run(*arguments)
  if arguments.last.is_a?(Hash)
    options = arguments.pop
  else
    options = {}
  end
  spawn_options = options[:spawn_options] || {}
  pid = spawn(options[:env] || {},
              @command.to_s,
              *arguments,
              spawn_options.merge(default_spawn_options))
  pid, status = Process.waitpid2(pid)
  status.success?
end