Class: WinPathUtils::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/win-path-utils.rb

Defined Under Namespace

Classes: WrongOptionError

Instance Method Summary collapse

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(options = {})
  @separator = options[:separator] || ';'

  options[:type] ||= :system
  
  @hkey, @reg_path, @key_name = case options[: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     = options[:hkey]     if options.key?(:hkey)
  @reg_path = options[:reg_path] if options.key?(:reg_path)
  @key_name = options[:key_name] if options.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, options = {})
  # Set defaults
  options[:duplication_filter] = :do_not_add unless options.key?(:duplication_filter)

  # Get path
  path = get_array

  # Check duplicates
  if path.member?(value)
    case options[: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 options[: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

#getObject

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_arrayObject

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

Returns:

  • (Boolean)


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, options = {})
  add(value, options.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, options = {})
  add(value, options.merge(where: :start))
end