Class: UnifiedSettings::Handlers::Env

Inherits:
Base
  • Object
show all
Defined in:
lib/unified_settings/handlers/env.rb

Overview

Settings handler for ENV variables

Constant Summary collapse

ENV_KEY_NESTING_SEPARATOR =
'__'

Constants inherited from Base

Base::KEY_NESTING_SEPARATOR

Instance Method Summary collapse

Instance Method Details

#defined?(key, case_sensitive: nil) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/unified_settings/handlers/env.rb', line 11

def defined?(key, case_sensitive: nil)
  key_arr = to_symbol_array(key)
  case_sensitive = case_sensitive?(case_sensitive)

  return true if ENV.key?(key_arr.join(ENV_KEY_NESTING_SEPARATOR))
  return false if case_sensitive

  return true if ENV.key?(
    key_arr.map(&:upcase).join(ENV_KEY_NESTING_SEPARATOR)
  )
  return true if ENV.key?(
    key_arr.map(&:downcase).join(ENV_KEY_NESTING_SEPARATOR)
  )

  false
end

#get(key, case_sensitive: nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/unified_settings/handlers/env.rb', line 28

def get(key, case_sensitive: nil)
  key_arr = to_symbol_array(key)
  case_sensitive = case_sensitive?(case_sensitive)

  val = ENV.fetch(key_arr.join(ENV_KEY_NESTING_SEPARATOR), nil)
  return val if val
  return nil if case_sensitive

  val = ENV.fetch(
    key_arr.map(&:upcase).join(ENV_KEY_NESTING_SEPARATOR), nil
  )
  return val if val

  ENV.fetch(
    key_arr.map(&:downcase).join(ENV_KEY_NESTING_SEPARATOR), nil
  )
end