Class: ChupaText::ExternalCommand

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

Defined Under Namespace

Classes: SpawnLimitOptions

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ ExternalCommand



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

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

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path



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

def path
  @path
end

Instance Method Details

#exist?Boolean



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

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

#run(*arguments) ⇒ Object



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

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