class Envlogic::Env

Env module to get and set environment

Constants

ENV_KEY_POSTFIX

Postfix for ENV keys

FALLBACK_ENV

What default environment should be assumed when there's nothing else

FALLBACK_ENV_KEY

What environment key should be used by default

INFLECTOR

String inflecting engine

Public Class Methods

new(klass) click to toggle source

@param klass [Class, Module] class/module for which we want to build a Envlogic::Env object @return [Envlogic::Env] envlogic env object] @note Will load appropriate environment automatically @example

Envlogic::Env.new(User)
Calls superclass method
# File lib/envlogic/env.rb, line 26
def initialize(klass)
  super('')

  env = ENV[to_env_key(app_dir_name)]
  env ||= ENV[to_env_key(klass.to_s)]
  env ||= ENV[FALLBACK_ENV_KEY]

  update(env || FALLBACK_ENV)
end

Public Instance Methods

method_missing(method_name, *arguments) click to toggle source

Reacts to missing methods, from which some might be the env checks. If the method ends with '?' we assume, that it is an env check @param method_name [String] method name for missing or env name with question mark @param arguments [Array] any arguments that we pass to the method

Calls superclass method
# File lib/envlogic/env.rb, line 47
def method_missing(method_name, *arguments)
  method_name[-1] == '?' ? self == method_name[0..-2] : super
end
respond_to_missing?(method_name, include_private = false) click to toggle source

@param method_name [String] method name @param include_private [Boolean] should we include private methods as well @return [Boolean] true if we respond to a given missing method, otherwise false

Calls superclass method
# File lib/envlogic/env.rb, line 39
def respond_to_missing?(method_name, include_private = false)
  (method_name[-1] == '?') || super
end

Private Instance Methods

app_dir_name() click to toggle source

@return [String] name of the directory in which this application is @note Will return only the last part, so if the dir is /home/apps/my_app it will

only return the 'my_app' part
# File lib/envlogic/env.rb, line 56
def app_dir_name
  Pathname
    .new(ENV['BUNDLE_GEMFILE'])
    .dirname
    .basename
    .to_s
end
to_env_key(string) click to toggle source

Converts any string into a bash ENV key @param string [String] string we want to convert into an env key @return [String] converted name that can be an ENV key

# File lib/envlogic/env.rb, line 67
def to_env_key(string)
  INFLECTOR
    .underscore(string)
    .tr('/', '_')
    .upcase + ENV_KEY_POSTFIX
end