Class: ISE::PreferenceFile

Inherits:
IniFile
  • Object
show all
Defined in:
lib/ise/preference_file.rb

Overview

Represents a set of ISE Project Navigator Preferences.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ise_preference_file_pathObject

Determines the location of the ISE preferences file. TODO: Generalize to work on Windows?



16
17
18
# File 'lib/ise/preference_file.rb', line 16

def self.ise_preference_file_path
  "~/.config/Xilinx/ISE.conf"
end

.load(filename = nil, opts = {}) ⇒ Object

Loads an ISE preference file.



23
24
25
26
27
28
29
30
31
# File 'lib/ise/preference_file.rb', line 23

def self.load(filename=nil, opts={})
   
  #If no filename was specified, use the default ISE preference file.
  filename ||= File.expand_path(ise_preference_file_path)

  #Parse the preference file as an INI file.
  super

end

Instance Method Details

#get_by_path(path, target = @ini) ⇒ Object

Gets the value of a key in a hash-of-hashes via a unix-style path.

path: The path to the target key, in a unix-style path structure. See example below. target: The hash to operate on. If target isn’t provided, we work with the base INI.

Example:

set_by_path('foo/bar/tab', 3, a) would set
a[foo][bar][tab] = 3; setting foo and bar to


74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ise/preference_file.rb', line 74

def get_by_path(path, target=@ini)

  #Split the path into its components...
  keys = path.split('/')

  #And traverse the hasn until we've fully navigated the path.
  target = target[keys.shift] until keys.empty?

  #Returns the final value.
  target

end

#process_property(property, value) ⇒ Object

Processes a given name-value pair, adding them to the current INI database.

Code taken from the ‘inifile’ gem.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ise/preference_file.rb', line 93

def process_property(property, value)
  
  value.chomp!

  #If either the property or value are empty (or contain invalid whitespace),
  #abort.
  return if property.empty? and value.empty?
  return if value.sub!(%r/\\\s*\z/, '')

  #Strip any leading/trailing characters.
  property.strip!
  value.strip!

  #Raise an error if we have an invalid property name.
  parse_error if property.empty?

  #Parse ISE's value into a path.
  set_by_path(CGI::unescape(property), unescape_value(value.dup), current_section)

  #And continue processing the property and value.
  property.slice!(0, property.length)
  value.slice!(0, value.length)

  #Return nil.
  nil

end

#set_by_path(path, value, target = @ini) ⇒ Object

Sets the value of a key in a hash-of-hashes via a unix-style path.

path: The path to the target key, in a unix-style path structure. See example below. value: The value to put into the key. target: The hash to operate on. If target isn’t provided, we work with the base INI.

Example:

set_by_path('foo/bar/tab', 3, a) would set
a[foo][bar][tab] = 3; setting foo and bar to


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ise/preference_file.rb', line 46

def set_by_path(path, value, target=@ini)

  #Split the path into its components.
  keys = path.split('/')  

  #Traverse the path, creating any "folders" necessary along the way.
  until keys.one?
    target[keys.first] = {} unless target[keys.first].is_a?(Hash)
    target = target[keys.shift]
  end
 
  #And finally, place the value into the appropriate "leaf".
  target[keys.shift] = value

end