class Chook::Configuration
The configuration object
Constants
- CONF_KEYS
The attribute keys we maintain, and how they should be converted to the value used by chook internally.
For descriptions of the keys, see data/chook.conf.example
- DEFAULT_CONF_FILE
The location of the default config file
- SAMPLE_CONF_FILE
Public Class Methods
new()
click to toggle source
Initialize!
# File lib/chook/configuration.rb, line 84 def initialize read_global end
Public Instance Methods
clear_all()
click to toggle source
Clear all values
@return [void]
# File lib/chook/configuration.rb, line 95 def clear_all CONF_KEYS.keys.each { |k| send "#{k}=".to_sym, nil } end
print()
click to toggle source
Print out the current settings to stdout
@return [void]
# File lib/chook/configuration.rb, line 164 def print CONF_KEYS.keys.sort.each { |k| puts "#{k}: #{send k}" } end
read_global()
click to toggle source
(Re)read the global prefs, if it exists.
@return [Boolean] was the file loaded?
# File lib/chook/configuration.rb, line 103 def read_global return false unless DEFAULT_CONF_FILE.file? && DEFAULT_CONF_FILE.readable? read DEFAULT_CONF_FILE end
reload(file = DEFAULT_CONF_FILE)
click to toggle source
Clear the settings and reload the prefs file, or another file if provided
@param file a non-standard prefs file to load
@return [Boolean] was the file reloaded?
# File lib/chook/configuration.rb, line 114 def reload(file = DEFAULT_CONF_FILE) file = Pathname.new file return false unless file.file? && file.readable? clear_all read file end
save(file)
click to toggle source
Save the prefs into a file
@param file either :user, :global, or an arbitrary file to save.
@return [void]
# File lib/chook/configuration.rb, line 127 def save(file) path = Pathname.new(file) # file already exists? read it in and update the values. # Don't overwrite it, since the user might have comments # in there. if path.readable? data = path.read # go thru the known attributes/keys CONF_KEYS.keys.sort.each do |k| # if the key exists, update it. if data =~ /^#{k}:/ data.sub!(/^#{k}:.*$/, "#{k}: #{send k}") # if not, add it to the end unless it's nil else data += "\n#{k}: #{send k}" unless send(k).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.open('w') { |f| f.write data } end
Private Instance Methods
read(file)
click to toggle source
Read in a prefs file
@param file the file to read
@return [Boolean] was the file read?
# File lib/chook/configuration.rb, line 178 def read(file) available_conf_keys = CONF_KEYS.keys Pathname.new(file).read.each_line do |line| # skip blank lines and those starting with # next if line =~ /^\s*(#|$)/ line.strip =~ /^(\w+?):\s*(\S.*)$/ key = Regexp.last_match(1) next unless key attr = key.to_sym next unless available_conf_keys.include? attr setter = "#{key}=".to_sym value = Regexp.last_match(2).strip # convert the string value read from the file # to the correct class value &&= case CONF_KEYS[attr] when Proc # If its a proc, pass it to the proc CONF_KEYS[attr].call value when Symbol # otherwise its a symbol method name to call on the string value.send(CONF_KEYS[attr]) else value end send(setter, value) end # do line true end