Module: EnvironmentHelpers::BooleanHelpers

Included in:
EnvironmentHelpers
Defined in:
lib/environment_helpers/boolean_helpers.rb

Constant Summary collapse

BOOLEAN_VALUES =
[true, false, nil].to_set

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.falsey_stringsObject



23
24
25
26
27
28
# File 'lib/environment_helpers/boolean_helpers.rb', line 23

def self.falsey_strings
  @_falsey_strings ||=
    ENV.fetch("ENVIRONMENT_HELPERS_FALSEY_STRINGS", "false,no,off,disabled,disable,deny,f,n,0,nope")
      .split(",")
      .to_set
end

.truthy_stringsObject



16
17
18
19
20
21
# File 'lib/environment_helpers/boolean_helpers.rb', line 16

def self.truthy_strings
  @_truthy_strings ||=
    ENV.fetch("ENVIRONMENT_HELPERS_TRUTHY_STRINGS", "true,yes,on,enabled,enable,allow,t,y,1,ok,okay")
      .split(",")
      .to_set
end

Instance Method Details

#boolean(name, default: nil, required: false) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/environment_helpers/boolean_helpers.rb', line 5

def boolean(name, default: nil, required: false)
  check_default_value(:boolean, default, allow: BOOLEAN_VALUES)
  text = fetch_value(name, required: required)

  return true if truthy_text?(text)
  return false if falsey_text?(text)

  return default unless required
  fail(InvalidBooleanText, "Required boolean environment variable #{name} had inappropriate content '#{text}'")
end