Sha256: 48fdb0b763506777e46c1e4103102b1b37bbc69f22aeceac6e868ee155660f16

Contents?: true

Size: 914 Bytes

Versions: 2

Compression:

Stored size: 914 Bytes

Contents

class RackEnvironment

  attr_reader :app, :options

  def initialize(app, options={})
    @app     = app
    @options = options
  end

  def call(env)
    update_environment!
    app.call(env)
  end

private ######################################################################

  def default_config_file
    File.join(Dir.getwd, 'config', 'environment.yml')
  end

  def environment
    normalize case
      when options[:environment] then options[:environment]
      when options[:file]        then read_config_file(options[:file])
      else read_config_file(default_config_file)
    end
  end

  def normalize(environment)
    environment.inject({}) do |hash, (key, value)|
      hash.update(key.to_s.upcase => value)
    end
  end

  def read_config_file(filename)
    YAML::load_file(filename)
  end

  def update_environment!
    environment.each do |key, value|
      ENV[key] = value
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rack-environment-1.0.1 lib/rack_environment.rb
rack-environment-1.0.0 lib/rack_environment.rb