Module: Mongoid::Config::Environment

Extended by:
Environment
Included in:
Environment
Defined in:
build/mongoid-7.3/lib/mongoid/config/environment.rb

Overview

Encapsulates logic for getting environment information.

Instance Method Summary collapse

Instance Method Details

#env_nameString

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*

Examples:

Get the env name.

Environment.env_name

Returns:

  • (String)

    The name of the current environment.

Raises:

  • (Errors::NoEnvironment)

    If environment name cannot be determined because none of the sources was set.

Since:

  • 2.3.0



29
30
31
32
33
34
35
36
37
# File 'build/mongoid-7.3/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) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Examples:

Load the yaml.

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

Parameters:

  • path (String)

    The location of the file.

  • environment (String | Symbol) (defaults to: nil)

    Optional environment name to override the current Mongoid environment.

Returns:

  • (Hash)

    The settings.

Since:

  • 2.3.0



53
54
55
56
57
58
59
60
61
62
63
64
# File 'build/mongoid-7.3/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