Sha256: 28555813763570668d4c879bbfd4f3db6ba457b66827322c4759f991d3d65baa

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 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}"
      erb = File.read(path)
      yaml = ERB.new(erb).result(binding)
      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

1 entries across 1 versions & 1 rubygems

Version Path
radiospiel-app-0.2.1 lib/app/app/config.rb