summaryrefslogtreecommitdiff
path: root/lib/ats/configuration.rb
blob: 961563a5b099248659d900da8ce1970ecf4fdc81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
module ATS
  class Configuration
    attr_accessor :logger, :config_files, :debug

    def initialize
      @logger = Logger.new(STDOUT)
      @logger.level = Logger::DEBUG
      @debug = false
      @config_files = [
        File.join(Dir.home, ".atsrc"),
        File.expand_path('.atsrc'),
        ENV['ATSRC']
      ].compact
    end

    def fetch(key)
      hash.fetch(key.to_sym)
    end

    def to_h
      hash.dup
    end

    private

    def hash
      @hash ||= load_configuration
    end

    def load_configuration(files = config_files)
      files.inject({}) do |memo, file|
        logger.debug("Searching for #{file}...") if debug
        memo.merge!(YAML.load(IO.read(file))) if File.exist?(file)
        memo
      end
    end
  end
end