Sha256: 0be88f5651f5d1ea6375cbcd4d743ad2fed4d6e9637d09f99e0618737a1fd4ad

Contents?: true

Size: 1.69 KB

Versions: 7

Compression:

Stored size: 1.69 KB

Contents

# encoding: utf-8
module LocalPac
  class Config
    private

    attr_reader :config

    @options = []
    class << self

      attr_reader :options

      def option(option, default_value)
        define_method option.to_sym do
          config.transaction do
            config.fetch(option.to_sym, default_value)
          end
        end

        @options << option
      end
    end

    public

    def initialize(file = available_config_file, config_engine = YAML::Store)
      @config = config_engine.new(file)
    end

    option :log_sink, File.join(ENV['HOME'], '.local', 'share', 'local_pac', 'log')
    option :local_storage, File.join(ENV['HOME'], '.local', 'share', 'local_pac', 'data')
    option :executable, File.expand_path('../../../bin/local_pac', __FILE__)
    option :pid_file, File.join(ENV['HOME'], '.local', 'share', 'local_pac', 'run', 'pid')
    option :gem_path, Gem.path

    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

    def candiate_files
      [
        File.expand_path(File.join(Dir.getwd, 'config.yaml')),
        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

    def available_config_file
      candiate_files.find { |f| File.exists? f }
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
local_pac-0.1.6 lib/local_pac/config.rb
local_pac-0.1.5 lib/local_pac/config.rb
local_pac-0.1.4 lib/local_pac/config.rb
local_pac-0.1.3 lib/local_pac/config.rb
local_pac-0.1.2 lib/local_pac/config.rb
local_pac-0.1.1 lib/local_pac/config.rb
local_pac-0.1.0 lib/local_pac/config.rb