Class: Windoo::Configuration
- Inherits:
-
Object
- Object
- Windoo::Configuration
- Includes:
- Singleton
- Defined in:
- lib/windoo/configuration.rb
Overview
Many values in Windoo will also have a hard-coded default, if not defined
A class for working with pre-defined settings & preferences for Windoo
This is a singleton class, only one instance can exist at a time.
When the module loads, that instance is created, and is used to provide default values throughout Windoo. It can be accessed via Windoo.config in applications.
in the configuration.
When the Windoo::Configuration instance is created, the GLOBAL_CONF file (/etc/windoo.conf) is examined if it exists, and the items in it are loaded into the attributes.
Then the user-specific USER_CONF file (~/.windoo.conf) is examined if it exists, and any attributes defined there will override those values from the GLOBAL_CONF.
The file format is one attribute per line, thus:
attr_name: value
Lines that don’t start with a known attribute name followed by a colon are ignored. If an attribute is defined more than once, the last one wins.
See CONF_KEYS for the available attributes, and how they are converted to the appropriate Ruby class when loaded.
At any point, the attributes can read or changed using standard Ruby getter/setter methods matching the name of the attribute, e.g.
# read the current title_editor_server_name configuration value
Windoo.config.title_editor_server_name # => 'foobar.appcatalog.jamfcloud.com'
# sets the title_editor_server_name to a new value
Windoo.config.title_editor_server_name = 'baz.appcatalog.jamfcloud.com'
The current settings may be saved to the GLOBAL_CONF file, the USER_CONF file, or an arbitrary file using #save. The argument to #save should be either :user, :global, or a String or Pathname file path. NOTE: This overwrites any existing file with the current values of the Configuration object.
To re-load the configuration use #reload. This clears the current settings, and re-reads both the global and user files. If a pathname is provided, e.g.
Windoo.config.reload '/path/to/other/file'
the current settings are cleared and reloaded from that other file.
To view the current settings, use #print.
Constant Summary collapse
- CONF_FILENAME =
The filename for storing the config, globally or user-level. The first matching file is used - the array provides backward compatibility with earlier versions. Saving will always happen to the first filename
'windoo.conf'
- GLOBAL_CONF =
The Pathname to the machine-wide preferences plist
Pathname.new "/etc/#{CONF_FILENAME}"
- USER_CONF =
The Pathname to the user-specific preferences plist
Pathname.new("~/.#{CONF_FILENAME}").
- CONF_KEYS =
The attribute keys we maintain, and the type they should be stored as
{ title_editor_server_name: :to_s, title_editor_server_port: :to_i, title_editor_ssl_version: :to_s, title_editor_verify_cert: :to_bool, title_editor_username: :to_s, title_editor_open_timeout: :to_i, title_editor_timeout: :to_i }
Instance Method Summary collapse
-
#clear_all ⇒ void
Clear all values.
-
#initialize ⇒ Configuration
constructor
Initialize!.
-
#print ⇒ void
Print out the current settings to stdout.
-
#reload(file = nil) ⇒ void
Clear the settings and reload the prefs files, or another file if provided.
-
#save(file) ⇒ void
Save the prefs into a file.
Constructor Details
#initialize ⇒ Configuration
Initialize!
101 102 103 104 |
# File 'lib/windoo/configuration.rb', line 101 def initialize read GLOBAL_CONF read USER_CONF end |
Instance Method Details
#clear_all ⇒ void
This method returns an undefined value.
Clear all values
113 114 115 |
# File 'lib/windoo/configuration.rb', line 113 def clear_all CONF_KEYS.keys.each { |k| send "#{k}=", nil } end |
#print ⇒ void
This method returns an undefined value.
Print out the current settings to stdout
182 183 184 |
# File 'lib/windoo/configuration.rb', line 182 def print CONF_KEYS.keys.sort.each { |k| puts "#{k}: #{send k}" } end |
#reload(file = nil) ⇒ void
This method returns an undefined value.
Clear the settings and reload the prefs files, or another file if provided
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/windoo/configuration.rb', line 123 def reload(file = nil) clear_all if file read file return true end read GLOBAL_CONF read USER_CONF true end |
#save(file) ⇒ void
This method returns an undefined value.
Save the prefs into a file
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/windoo/configuration.rb', line 140 def save(file) path = case file when :global then GLOBAL_CONF when :user then USER_CONF else Pathname.new(file) end # file already exists? read it in and update the values. if path.readable? data = path.read # go thru the known attributes/keys CONF_KEYS.keys.sort.each do |k| curr_val = send(k) # if the key exists, update it. if data =~ /^\s*#{k}:/ data.sub!(/^\s*#{k}:.*$/, "#{k}: #{curr_val}") # if not, add it to the end unless it's nil else data += "\n#{k}: #{curr_val}" unless curr_val.nil? end # if data =~ /^#{k}:/ end # each do |k| else # not readable, make a new file data = '' CONF_KEYS.keys.sort.each do |k| data << "#{k}: #{send k}\n" unless send(k).nil? end end # if path readable # make sure we end with a newline, the save it. data << "\n" unless data.end_with?("\n") path.x_save data end |