class ProxyTester::Config

Attributes

options[R]
config[R]

Public Class Methods

new(file = available_config_file, config_engine = Psych) click to toggle source
# File lib/proxy_tester/config.rb, line 45
def initialize(file = available_config_file, config_engine = Psych)
  config_mutex = Mutex.new
  config_mutex.synchronize do
    yaml = Psych.load_file(file)

    if yaml.respond_to? :[]
      @config = yaml.symbolize_keys
    else
      @config = {}
    end
  end
rescue StandardError => e
  fail Exceptions::ConfigFileNotReadable, JSON.dump(message: e.message, file: file)
end
option(option, default_value) click to toggle source
# File lib/proxy_tester/config.rb, line 37
def option(option, default_value)
  option_reader(option, default_value)
  option_writer(option)
end
option_reader(option, default_value) click to toggle source
# File lib/proxy_tester/config.rb, line 17
def option_reader(option, default_value)
  define_method option.to_sym do
    config.fetch(option.to_sym, default_value)
  end

  @options << option
end
option_writer(option) click to toggle source
# File lib/proxy_tester/config.rb, line 25
def option_writer(option)
  define_method "#{option}=".to_sym do |value|
    begin
      config[option.to_sym] = value
    rescue RuntimeError
      raise Exceptions::ConfigLocked, JSON.dump(option: option)
    end
  end

  @options << option
end

Public Instance Methods

allowed_config_file_paths() click to toggle source
# File lib/proxy_tester/config.rb, line 76
def allowed_config_file_paths
  [
    ::File.expand_path(::File.join(ENV['HOME'], '.config', 'proxy_tester', 'config.yaml')),
    ::File.expand_path(::File.join(ENV['HOME'], '.proxy_tester', 'config.yaml')),
    ::File.expand_path(::File.join('/etc', 'proxy_tester', 'config.yaml')),
    ::File.expand_path('../../../files/config.yaml', __FILE__),
  ]
end
lock() click to toggle source
# File lib/proxy_tester/config.rb, line 60
def lock
  config.freeze
end
to_s() click to toggle source
# File lib/proxy_tester/config.rb, line 64
def to_s
  result = []
  result << sprintf("%20s | %s", 'option', 'value')
  result << sprintf("%s + %s", '-' * 20, '-' * 80)

  Config.options.each do |o|
    result << sprintf("%20s | %s", o, Array(public_send(o)).join(', '))
  end

  result.join("\n")
end

Private Instance Methods

available_config_file() click to toggle source
# File lib/proxy_tester/config.rb, line 95
def available_config_file
  allowed_config_file_paths.find { |f| ::File.exists? f }
end