# encoding: utf-8 module LocalPac class Config private attr_reader :config @options = Set.new class << self public attr_reader :options def option_reader(option, default_value) define_method option.to_sym do config.fetch(option.to_sym, default_value) end @options << option end def option_writer(option) define_method "#{option}=".to_sym do |value| config[option.to_sym] = value end @options << option end def option(option, default_value) option_reader(option, default_value) option_writer(option) end end public 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, "Sorry, but there was a problem reading the config file: #{e.message}." end def lock config.freeze end option :local_storage, ::File.expand_path(::File.join(ENV['HOME'], '.local', 'share', 'local_pac', 'data', 'storage.git')) option :executable, ::File.expand_path(::File.expand_path('../../../bin/local_pac', __FILE__)) option :pid_file, ::File.expand_path(::File.join(ENV['HOME'], '.local', 'share', 'local_pac', 'run', 'pid')) option :gem_path, Gem.path.collect {|p|::File.expand_path(p) } option :access_log, ::File.expand_path(::File.join(ENV['HOME'], '.local', 'share', 'local_pac', 'log', 'access.log')) option :sass_cache, ::File.expand_path(::File.join(ENV['HOME'], '.local', 'share', 'local_pac', 'cache')) option :config_file, ::File.expand_path(::File.join(ENV['HOME'], '.config', 'local_pac', 'config.yaml')) option :reload_config_signal, :USR1 option :reload_storage_signal, :USR2 option :debug_mode, false option :log_level, :info option :api_key, SecureRandom.hex option :listen, 'tcp://127.0.0.1:8000' option :environment, 'development' 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 def allowed_config_file_paths [ ::File.expand_path(::File.join(ENV['HOME'], '.config', 'local_pac', 'config.yaml')), ::File.expand_path(::File.join(ENV['HOME'], '.local_pac', 'config.yaml')), ::File.expand_path(::File.join('/etc', 'local_pac', 'config.yaml')), ::File.expand_path('../../../files/config.yaml', __FILE__), ] end private def available_config_file allowed_config_file_paths.find { |f| ::File.exists? f } end end end