Class: Envlogic::Env

Inherits:
String
  • Object
show all
Defined in:
lib/envlogic/env.rb

Overview

Env module to get and set environment

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Envlogic::Env

Note:

Will load appropriate environment automatically

Returns envlogic env object].

Examples:

Envlogic::Env.new(User)

Parameters:

  • klass (Class, Module)

    class/module for which we want to build a Envlogic::Env object



26
27
28
29
30
31
32
# File 'lib/envlogic/env.rb', line 26

def initialize(klass)
  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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments) ⇒ Object

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

Parameters:

  • method_name (String)

    method name for missing or env name with question mark

  • arguments (Array)

    any arguments that we pass to the method



45
46
47
# File 'lib/envlogic/env.rb', line 45

def method_missing(method_name, *arguments)
  method_name[-1] == '?' ? self == method_name[0..-2] : super
end

Instance Method Details

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns true if we respond to a given missing method, otherwise false.

Parameters:

  • method_name (String)

    method name

  • include_private (Boolean) (defaults to: false)

    should we include private methods as well

Returns:

  • (Boolean)

    true if we respond to a given missing method, otherwise false



37
38
39
# File 'lib/envlogic/env.rb', line 37

def respond_to_missing?(method_name, include_private = false)
  (method_name[-1] == '?') || super
end