class PackageChanger::Configuration

Constants

CONF_KEYS
DEFAULT_CONF_FILE

The location of the default config file

SAMPLE_CONF_FILE

Public Class Methods

new() click to toggle source

Initialize!

# File lib/PackageChanger/config.rb, line 31
def initialize
  read_global
end

Public Instance Methods

print() click to toggle source
read_global() click to toggle source
# File lib/PackageChanger/config.rb, line 35
def read_global
  return false unless DEFAULT_CONF_FILE.file? && DEFAULT_CONF_FILE.readable?

  read DEFAULT_CONF_FILE
end

Private Instance Methods

read(file) click to toggle source
# File lib/PackageChanger/config.rb, line 47
def read(file)
  available_conf_keys = CONF_KEYS.keys

  # puts file
  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