Sha256: 41f70ba5e06001cacd822c7fba3807e5785ecb7c86090883d65c7da8d3b8a798

Contents?: true

Size: 1.72 KB

Versions: 7

Compression:

Stored size: 1.72 KB

Contents

# encoding: utf-8
module Mongoid
  module Config

    # Encapsulates logic for getting environment information.
    module Environment
      extend self

      # 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
      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 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
      def load_yaml(path, environment = nil)
        env = environment ? environment.to_s : env_name
        YAML.load(ERB.new(File.new(path).read).result)[env]
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
mongoid-7.0.13 lib/mongoid/config/environment.rb
mongoid-7.0.12 lib/mongoid/config/environment.rb
mongoid-7.0.11 lib/mongoid/config/environment.rb
mongoid-7.0.10 lib/mongoid/config/environment.rb
mongoid-7.0.8 lib/mongoid/config/environment.rb
mongoid-7.0.7 lib/mongoid/config/environment.rb
mongoid-7.0.6 lib/mongoid/config/environment.rb