Class: WinPathUtils::Path
- Inherits:
-
Object
- Object
- WinPathUtils::Path
- Defined in:
- lib/win-path-utils.rb
Defined Under Namespace
Classes: WrongOptionError
Instance Method Summary collapse
-
#add(value, options = {}) ⇒ Object
Adds value to the path.
-
#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.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/win-path-utils.rb', line 9 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 |
Instance Method Details
#add(value, options = {}) ⇒ Object
Adds value to the path
59 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 |
# File 'lib/win-path-utils.rb', line 59 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 |
#get ⇒ Object
Returns the entire PATH variable as string
36 37 38 39 40 41 42 43 44 |
# File 'lib/win-path-utils.rb', line 36 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
54 55 56 |
# File 'lib/win-path-utils.rb', line 54 def get_array get.split(@separator) end |
#include?(value) ⇒ Boolean Also known as: member?
Checks the inclusion of value in path
116 117 118 |
# File 'lib/win-path-utils.rb', line 116 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
96 97 98 |
# File 'lib/win-path-utils.rb', line 96 def push(value, = {}) add(value, .merge(where: :end)) end |
#remove(value) ⇒ Object Also known as: delete
Removes the item from the path
108 109 110 111 112 |
# File 'lib/win-path-utils.rb', line 108 def remove(value) path = get_array path.delete(value) set_array(path) end |
#set(value) ⇒ Object
Sets the entire PATH variable to provided string
29 30 31 32 33 |
# File 'lib/win-path-utils.rb', line 29 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
48 49 50 |
# File 'lib/win-path-utils.rb', line 48 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
102 103 104 |
# File 'lib/win-path-utils.rb', line 102 def unshift(value, = {}) add(value, .merge(where: :start)) end |