class Qwik::Config

Constants

DEBUG_BASEDIR
DebugConfig
DefaultConfig
LIBDIR
QuickMLInternal
TestConfig

Public Class Methods

load_args_and_config(config, progname, args) click to toggle source

class method

# File vendor/qwik/lib/qwik/config.rb, line 152
def self.load_args_and_config(config, progname, args)
  args_conf = Config.parse_args(progname, args)
  config.update(args_conf)          # config file is specified by args
  file_conf = Config.load_config_file(config[:config_file])
  config.update(file_conf)
  config.update(args_conf)          # Set args again to override.
end
load_config_file(file) click to toggle source
# File vendor/qwik/lib/qwik/config.rb, line 160
def self.load_config_file(file)
  raise "can not open #{file}" if ! FileTest.exist?(file)
  content = open(file) {|fh| fh.read }
  return parse_config(content)
end
make_accessor(klass, config, debug=false) click to toggle source
# File vendor/qwik/lib/qwik/config.rb, line 200
def self.make_accessor(klass, config, debug=false)
  config.each_key {|k|
    if ! klass.method_defined?(k)
      klass.class_eval "
        def #{k}
          return @config[:#{k}]
        end
      "
    end
  }
end
new() click to toggle source
# File vendor/qwik/lib/qwik/config.rb, line 131
def initialize
  @config = {}
  @config.update(QuickMLInternal)
  @config.update(DefaultConfig)
  Config.make_accessor(Config, @config, @config[:debug])
end
parse_args(myprog, args) click to toggle source
# File vendor/qwik/lib/qwik/config.rb, line 212
def self.parse_args(myprog, args)
  config = {}
  optionparser = OptionParser.new {|opts|
    opts.banner = "Usage: #{myprog} [options]"
    opts.separator ''
    opts.separator 'Specific options:'
    opts.on('-c', '--config file', 'Specify config file.') {|a|
      config[:config_file] = a
    }
    opts.on('-d', '--[no-]debug', 'Run in debug mode') {|a|
      config[:debug] = a
    }
    opts.separator ''
    opts.separator 'Common options:'
    opts.on_tail('-h', '--help', 'Show this message') {
      puts opts
      exit
    }
    opts.on_tail('-v', '--version', 'Show version') {
      puts VERSION
      exit
    }
  }
  optionparser.parse!(args)
  return config
end
parse_config(str) click to toggle source
# File vendor/qwik/lib/qwik/config.rb, line 166
def self.parse_config(str)
  config = {}
  str.each_line {|line|
    next unless /\A\:/ =~ line
    ar = line.chomp.split(':', 3)
    next if ar[1].empty?
    config[ar[1].intern] = parse_value(ar[2])
  }
  return config
end
parse_value(v) click to toggle source
# File vendor/qwik/lib/qwik/config.rb, line 177
def self.parse_value(v)
  v = $1 if /\A(.+?)\#.*\z/ =~ v    # remove comment
  v = v.strip
  case v
  when 'true';      return true
  when 'false';     return false
  when 'nil';       return nil
  when /\A\d+\z/;   return v.to_i
  # Only numbers, * and spaces are allowable.
  # It is allowable to use eval in this context.
  when /\A[\d\ \*]+\z/;     return eval(v)
  when /\A(\d+)m\z/;        return $1.to_i * 60
  when /\A(\d+)h\z/;        return $1.to_i * 60 * 60
  when /\A(\d+)d\z/;        return $1.to_i * 60 * 60 * 24
  when /\A(\d+)w\z/;        return $1.to_i * 60 * 60 * 24 * 7
  when /\A(\d+)KB\z/;       return $1.to_i * 1024
  when /\A(\d+)MB\z/;       return $1.to_i * 1024 * 1024
  when /\A(\d+)GB\z/;       return $1.to_i * 1024 * 1024 * 1024
  end
  v.gsub!('$BASEDIR') { DEBUG_BASEDIR }
  return v
end

Public Instance Methods

[](k) click to toggle source
# File vendor/qwik/lib/qwik/config.rb, line 138
def [](k)
  return @config[k]
end
[]=(k, v) click to toggle source
# File vendor/qwik/lib/qwik/config.rb, line 142
def []=(k, v)
  @config[k] = v
end
update(hash) click to toggle source
# File vendor/qwik/lib/qwik/config.rb, line 146
def update(hash)
  @config.update(hash)
end