Class: WinPathUtils::Path
- Inherits:
-
Object
- Object
- WinPathUtils::Path
- Defined in:
- lib/win-path-utils.rb
Defined Under Namespace
Classes: SetxError, WrongOptionError
Class Method Summary collapse
-
.commit! ⇒ Object
Cause Windows to reload environment from registry.
Instance Method Summary collapse
-
#add(value, options = {}) ⇒ Object
Adds value to the path.
-
#commit! ⇒ Object
Alias.
-
#get ⇒ Object
Returns the entire PATH variable as string.
-
#get_array ⇒ Object
Returns the entire PATH variable as array Splits with @separator.
-
#include?(value) ⇒ Boolean
(also: #member?)
Checks the inclusion of value in path.
-
#initialize(options = {}) ⇒ Path
constructor
A new instance of Path.
-
#push(value, options = {}) ⇒ Object
(also: #append)
Adds element to the end of the path if not exists.
-
#remove(value) ⇒ Object
(also: #delete)
Removes the item from the path.
-
#set(value) ⇒ Object
Sets the entire PATH variable to provided string.
-
#set_array(value) ⇒ Object
Sets the entire PATH variable to provided array Joins with @separator.
-
#unshift(value, options = {}) ⇒ Object
(also: #prepend)
Adds element to the start of the path if not exists.
Constructor Details
#initialize(options = {}) ⇒ Path
Returns a new instance of Path.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/win-path-utils.rb', line 10 def initialize( = {}) @separator = [:separator] || ';' [:type] ||= :system @hkey, @reg_path, @key_name = case [:type] when :system [Win32::Registry::HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 'Path'] when :local, :user [Win32::Registry::HKEY_CURRENT_USER, 'Environment', 'PATH'] else raise WrongOptionError, "Unknown type!" end @hkey = [:hkey] if .key?(:hkey) @reg_path = [:reg_path] if .key?(:reg_path) @key_name = [:key_name] if .key?(:key_name) end |
Class Method Details
.commit! ⇒ Object
Cause Windows to reload environment from registry
123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/win-path-utils.rb', line 123 def self.commit! var = "WIN_PATH_UTILS_TMPVAR" fd_r, fd_w = IO.pipe result = system("setx", var, "", [:out, :err] => fd_w) fd_w.close output = fd_r.read raise SetxError.new("SETX error: #{output}") if result == false result end |
Instance Method Details
#add(value, options = {}) ⇒ Object
Adds value to the path
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/win-path-utils.rb', line 60 def add(value, = {}) # Set defaults [:duplication_filter] = :do_not_add unless .key?(:duplication_filter) # Get path path = get_array # Check duplicates if path.member?(value) case [:duplication_filter] when :do_not_add, :deny # do nothing, we already have one in the list return when :remove_existing path.delete!(value) when :none # just pass through else raise WrongOptionError, "Unknown :duplication_filter!" end end # Change path array case [:where] when :start, :left path.unshift value when :end, :right path.push value else raise WrongOptionError, "Unknown :where!" end # Save new array set_array(path) end |
#commit! ⇒ Object
Alias
137 138 139 |
# File 'lib/win-path-utils.rb', line 137 def commit! self.class.commit! end |
#get ⇒ Object
Returns the entire PATH variable as string
37 38 39 40 41 42 43 44 45 |
# File 'lib/win-path-utils.rb', line 37 def get with_reg do |reg| begin reg.read(@key_name)[1] rescue Win32::Registry::Error nil end end end |
#get_array ⇒ Object
Returns the entire PATH variable as array Splits with @separator
55 56 57 |
# File 'lib/win-path-utils.rb', line 55 def get_array get.split(@separator) end |
#include?(value) ⇒ Boolean Also known as: member?
Checks the inclusion of value in path
117 118 119 |
# File 'lib/win-path-utils.rb', line 117 def include?(value) get_array.include?(value) end |
#push(value, options = {}) ⇒ Object Also known as: append
Adds element to the end of the path if not exists
97 98 99 |
# File 'lib/win-path-utils.rb', line 97 def push(value, = {}) add(value, .merge(where: :end)) end |
#remove(value) ⇒ Object Also known as: delete
Removes the item from the path
109 110 111 112 113 |
# File 'lib/win-path-utils.rb', line 109 def remove(value) path = get_array path.delete(value) set_array(path) end |
#set(value) ⇒ Object
Sets the entire PATH variable to provided string
30 31 32 33 34 |
# File 'lib/win-path-utils.rb', line 30 def set(value) with_reg do |reg| reg[@key_name] = value end end |
#set_array(value) ⇒ Object
Sets the entire PATH variable to provided array Joins with @separator
49 50 51 |
# File 'lib/win-path-utils.rb', line 49 def set_array(value) set(value.join(@separator)) end |
#unshift(value, options = {}) ⇒ Object Also known as: prepend
Adds element to the start of the path if not exists
103 104 105 |
# File 'lib/win-path-utils.rb', line 103 def unshift(value, = {}) add(value, .merge(where: :start)) end |