Class: LaunchdTools::LaunchdJob

Inherits:
Object
  • Object
show all
Defined in:
lib/launchd_tools/launchd_job.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ LaunchdJob

Returns a new instance of LaunchdJob.



4
5
6
# File 'lib/launchd_tools/launchd_job.rb', line 4

def initialize(attributes = {})
  @attributes = attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



3
4
5
# File 'lib/launchd_tools/launchd_job.rb', line 3

def attributes
  @attributes
end

Instance Method Details

#environment_variablesObject



8
9
10
# File 'lib/launchd_tools/launchd_job.rb', line 8

def environment_variables
  attributes.fetch('EnvironmentVariables', {})
end

#program_argumentsObject



12
13
14
# File 'lib/launchd_tools/launchd_job.rb', line 12

def program_arguments
  attributes.fetch('ProgramArguments', [])
end

#shellescape(str) ⇒ Object

from ruby’s shellescape included here for ruby 1.8 compatibility



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/launchd_tools/launchd_job.rb', line 23

def shellescape(str)
  str = str.to_s

  # An empty argument will be skipped, so return empty quotes.
  return "''" if str.empty?

  str = str.dup

  # Treat multibyte characters as is.  It is caller's responsibility
  # to encode the string in the right encoding for the shell
  # environment.
  str.gsub!(/([^A-Za-z0-9_\-.,:\/@\n])/, "\\\\\\1")

  # A LF cannot be escaped with a backslash because a backslash + LF
  # combo is regarded as line continuation and simply ignored.
  str.gsub!(/\n/, "'\n'")

  return str
end

#to_sObject



16
17
18
19
# File 'lib/launchd_tools/launchd_job.rb', line 16

def to_s
  escaped_args = program_arguments.map {|arg| shellescape(arg) }
  puts (environment_variables + escaped_args).join(" ")
end