module Mongoid::Config::Environment

Encapsulates logic for getting environment information.

Public Instance Methods

env_name() click to toggle source

Get the name of the environment that Mongoid is running under.

Uses the following sources in order:

  • If ::Rails is defined, Rails.env.

  • If ::Sinatra is defined, Sinatra::Base.environment.

  • RACK_ENV

  • +MONGOID_ENV*

@example Get the env name.

Environment.env_name

@raise [ Errors::NoEnvironment ] If environment name cannot be

determined because none of the sources was set.

@return [ String ] The name of the current environment.

@since 2.3.0 @api public

# File lib/mongoid/config/environment.rb, line 29
def env_name
  if defined?(::Rails)
    return ::Rails.env
  end
  if defined?(::Sinatra)
    return ::Sinatra::Base.environment.to_s
  end
  ENV["RACK_ENV"] || ENV["MONGOID_ENV"] or raise Errors::NoEnvironment
end
load_yaml(path, environment = nil) click to toggle source

Load the yaml from the provided path and return the settings for the specified environment, or for the current Mongoid environment.

@example Load the yaml.

Environment.load_yaml("/work/mongoid.yml")

@param [ String ] path The location of the file. @param [ String | Symbol ] environment Optional environment name to

override the current Mongoid environment.

@return [ Hash ] The settings.

@since 2.3.0 @api private

# File lib/mongoid/config/environment.rb, line 53
def load_yaml(path, environment = nil)
  env = environment ? environment.to_s : env_name
  contents = File.new(path).read
  if contents.empty?
    raise Mongoid::Errors::EmptyConfigFile.new(path)
  end
  data = YAML.load(ERB.new(contents).result)
  unless data.is_a?(Hash)
    raise Mongoid::Errors::InvalidConfigFile.new(path)
  end
  data[env]
end