Module: Diametric::Config::Environment

Extended by:
Environment
Included in:
Environment
Defined in:
lib/diametric/config/environment.rb

Overview

Encapsulates logic for getting environment information.

This is nearly a word-for-word copy of Mongoid's lib/mongoid/config/environment.rb file. Thanks!

Instance Method Summary collapse

Instance Method Details

#env_nameString

Get the name of the environment that we are running under. This first looks for Rails, then Sinatra, then a RACK_ENV environment variable, and if none of those are found raises an error.

Examples:

Get the env name.

Environment.env_name

Returns:

  • (String)

    The name of the current environment.

Raises:

  • ("No environment specified")

    If no environment was set.


21
22
23
24
25
# File 'lib/diametric/config/environment.rb', line 21

def env_name
  return Rails.env if defined?(Rails)
  return Sinatra::Base.environment.to_s if defined?(Sinatra)
  ENV["RACK_ENV"] || ENV["DIAMETRIC_ENV"] || raise("No environment specified")
end

#load_yaml(path, environment = nil) ⇒ Hash

Load the yaml from the provided path and return the settings for the current environment.

Examples:

Load the yaml.

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

Parameters:

  • path (String)

    The location of the file.

Returns:

  • (Hash)

    The settings.


36
37
38
39
# File 'lib/diametric/config/environment.rb', line 36

def load_yaml(path, environment = nil)
  env = environment ? environment.to_s : env_name
  YAML.load(ERB.new(File.new(path).read).result)[env]
end