Module: Lightning::Util

Extended by:
Util
Included in:
Util
Defined in:
lib/lightning/util.rb

Instance Method Summary collapse

Instance Method Details

#find_homeString

Returns Cross-platform way to determine a user’s home. From Rubygems.

Returns:

  • (String)

    Cross-platform way to determine a user’s home. From Rubygems.



33
34
35
36
37
38
39
# File 'lib/lightning/util.rb', line 33

def find_home
  ['HOME', 'USERPROFILE'].each {|e| return ENV[e] if ENV[e] }
  return "#{ENV['HOMEDRIVE']}#{ENV['HOMEPATH']}" if ENV['HOMEDRIVE'] && ENV['HOMEPATH']
  File.expand_path("~")
rescue
  File::ALT_SEPARATOR ? "C:/" : "/"
end

#load_plugins(base_dir, sub_dir) ⇒ Object

Loads *.rb plugins in given directory and sub directory under it



55
56
57
58
59
60
# File 'lib/lightning/util.rb', line 55

def load_plugins(base_dir, sub_dir)
  if File.exists?(dir = File.join(base_dir, sub_dir))
    plugin_type = sub_dir.sub(/s$/, '')
    Dir[dir + '/*.rb'].each {|file| load_plugin(file, plugin_type) }
  end
end

#shell_command_exists?(command) ⇒ Boolean

Returns Determines if a shell command exists by searching for it in ENV.

Returns:

  • (Boolean)

    Determines if a shell command exists by searching for it in ENV.



42
43
44
45
# File 'lib/lightning/util.rb', line 42

def shell_command_exists?(command)
  (@path ||= ENV['PATH'].split(File::PATH_SEPARATOR)).
    any? {|d| File.exists? File.join(d, command) }
end

#shellescape(str) ⇒ Object

From Ruby 1.9’s Shellwords#shellescape



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/lightning/util.rb', line 8

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

  str = str.dup

  # Process as a single byte sequence because not all shell
  # implementations are multibyte aware.
  str.gsub!(/([^A-Za-z0-9_\-.,:\/@\n])/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

#symbolize_keys(hash) ⇒ Hash

Returns Symbolizes keys of given hash.

Returns:

  • (Hash)

    Symbolizes keys of given hash



48
49
50
51
52
# File 'lib/lightning/util.rb', line 48

def symbolize_keys(hash)
  hash.inject({}) do |h, (key, value)|
    h[key.to_sym] = value; h
  end
end