Sha256: b615e675de88326cab96b886117559b750cebafbd3db97e7aec1237ed0e800ec

Contents?: true

Size: 1.17 KB

Versions: 3

Compression:

Stored size: 1.17 KB

Contents

require_relative "root"
require_relative "logger"

require "yaml"

module App
  module Config
    def self.paths
      [ "#{App.root}/config/app.yml", "#{App.root}/config.yml" ]
    end

    def self.read(path)
      return unless File.exist?(path)
      App.logger.info "Reading configuration from #{path}"
      yaml = File.read(path)
      YAML.load(yaml) || {}
    end
    
    def self.load
      config = paths.inject(nil) do |c, path|
        c || read(path)
      end

      config ||= begin
        App.logger.warn "No configuration found in #{App.root}"
        {}
      end
    end

    @@configurations = {}
    
    def self.current
      @@configurations[App.root] ||= begin
        config = self.load
        default_settings = config["default"] || {}
        current_settings = config[App.env] || {}
        default_settings.update current_settings
      end.extend(TreatSymbolsAsStrings)
    end
    
    module TreatSymbolsAsStrings
      def [](key)
        fetch(key) do
          case key
          when Symbol then fetch(key.to_s, nil)
          when String then fetch(key.to_sym, nil)
          end
        end
      end
    end
  end

  def config
    Config.current
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
radiospiel-app-0.2.0 lib/app/app/config.rb
radiospiel-app-0.1.1 lib/app/app/config.rb
radiospiel-app-0.1.0 lib/app/app/config.rb