Sha256: ec08511b50ae1a1543cf6e95a090728f3a89854ad2727b735d6890d66b006c1f

Contents?: true

Size: 1.23 KB

Versions: 6

Compression:

Stored size: 1.23 KB

Contents

module CarrierBotAPI
  module Configuration
    @defaults = {}
    @config = nil

    class << self
      def load(options={})
        filename = options[:file] || File.join(Rails.root, 'config', 'configuration.yml')
        env = options[:env] || Rails.env

        @config = @defaults.dup
        if File.file?(filename)
          @config.merge!(load_from_yaml(filename, env))
        end
        @config
      end

      def [](name)
        load unless @config
        @config[name]
      end

      private

      def load_from_yaml(filename, env)
        yaml = nil
        begin
          yaml = YAML::load(ERB.new(File.read(filename)).result)
        rescue ArgumentError
          abort "could not load YAML file #{filename}"
        rescue SyntaxError => e
          abort "could not load YAML file #{filename} - syntax error:\n#{e.message}"
        end
        conf = {}
        if yaml.is_a?(Hash)
          if yaml['default']
            conf.merge!(yaml['default'])
          end
          if yaml[env]
            conf.merge!(yaml[env])
          end
        else
          abort "could not load YAML configuration #{filename} - unexpected formatting"
        end
        conf
      end
    end # class methods
  end # module Configuration
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
carrierbot_api-0.5.3 lib/carrierbot_api/configuration.rb
carrierbot_api-0.5.2 lib/carrierbot_api/configuration.rb
carrierbot_api-0.5.1 lib/carrierbot_api/configuration.rb
carrierbot_api-0.5.0 lib/carrierbot_api/configuration.rb
carrierbot_api-0.4.0 lib/carrierbot_api/configuration.rb
carrierbot_api-0.3.0 lib/carrierbot_api/configuration.rb