Module: EnvHelpers

Defined in:
lib/env_helpers.rb,
lib/env_helpers/utils.rb,
lib/env_helpers/version.rb

Overview

Helper methods to improve ENV functionality

Defined Under Namespace

Classes: Utils

Constant Summary collapse

VERSION =
'0.1.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.rescue_jsonObject

Returns the value of attribute rescue_json


11
12
13
# File 'lib/env_helpers.rb', line 11

def rescue_json
  @rescue_json
end

Class Method Details

.array(var_name, sep = ',') ⇒ Array

Get an array value from an ENV variable

Defaults to [] if value is blank.

Parameters:

  • var_name (String)

    ENV variable name

  • sep (String) (defaults to: ',')

    string to split ENV value on

Returns:

  • (Array)

    array of strings representing the ENV value.


60
61
62
63
64
# File 'lib/env_helpers.rb', line 60

def array(var_name, sep = ',')
  return [] unless ENV.key?(var_name)

  ENV[var_name].split(sep)
end

.bool(var_name, default = nil) ⇒ Boolean

Get an boolean value from an ENV variable or return a default. Will return the default if there is a non-boolean like value for the ENV variable.

or doesn't represent a numeric value

Parameters:

  • var_name (String)

    ENV variable name

  • default (Object) (defaults to: nil)

    retured when ENV is blank

Returns:

  • (Boolean)

    boolean representing the ENV value


95
96
97
98
99
100
101
102
# File 'lib/env_helpers.rb', line 95

def bool(var_name, default = nil)
  return default unless ENV.key?(var_name)

  return true if true_value?(ENV[var_name])
  return false if false_value?(ENV[var_name])

  default
end

.equal?(var_name, value) ⇒ Boolean

Compare the value of an ENV variable to a given string.

Parameters:

  • var_name (String)

    ENV variable name

  • value (String)

    comparison string

Returns:

  • (Boolean)

    `true` if the value of the ENV variable is the same as value


18
19
20
21
22
# File 'lib/env_helpers.rb', line 18

def equal?(var_name, value)
  return false unless ENV.key?(var_name)

  ENV[var_name] == value
end

.false?(var_name) ⇒ Boolean

Check if an ENV variable represents `false`. ('false', 'f', or '0'). Returns false if environment variable isn't defined.

Parameters:

  • var_name (String)

    ENV variable name

Returns:

  • (Boolean)

120
121
122
123
124
# File 'lib/env_helpers.rb', line 120

def false?(var_name)
  return false unless ENV.key?(var_name)

  false_value?(ENV[var_name])
end

.false_value?(val) ⇒ Boolean

Check if a value is false Meant to be overriden / monkey patched to customize functionality

Parameters:

  • val (String)

    value to check if false

Returns:

  • (Boolean)

140
141
142
# File 'lib/env_helpers.rb', line 140

def false_value?(val)
  EnvHelpers::Utils.false_value?(val)
end

.int(var_name, default = nil) ⇒ Integer

Get an integer value (using to_i) from an ENV variable or return a default.

Parameters:

  • var_name (String)

    ENV variable name

  • default (Object) (defaults to: nil)

    retured when ENV is blank or doesn't exist

Returns:

  • (Integer)

    integer representing the ENV value


44
45
46
47
48
49
50
51
52
# File 'lib/env_helpers.rb', line 44

def int(var_name, default = nil)
  return default unless ENV.key?(var_name)

  n = ENV[var_name]

  return default if n == ''

  n.to_i
end

.json(var_name, default = nil) ⇒ Object

Get a JSON value from an ENV variable

Parameters:

  • var_name (String)

    ENV variable name

Returns:

  • (Object)

    array of strings representing the ENV value.

Raises:

  • (JSON::ParserError)

    if value is invalid JSON


71
72
73
74
75
76
77
78
79
# File 'lib/env_helpers.rb', line 71

def json(var_name, default = nil)
  return default unless ENV.key?(var_name)

  JSON.parse(ENV[var_name])
rescue JSON::ParserError => ex
  raise ex unless rescue_json?

  default
end

.num(var_name, default = nil) ⇒ Float

Get a number value (using to_f) from an ENV variable or return a default.

Parameters:

  • var_name (String)

    ENV variable name

  • default (Object) (defaults to: nil)

    retured when ENV is blank or doesn't exist

Returns:

  • (Float)

    float representing the ENV value


29
30
31
32
33
34
35
36
37
# File 'lib/env_helpers.rb', line 29

def num(var_name, default = nil)
  return default unless ENV.key?(var_name)

  n = ENV[var_name]

  return default if n == ''

  n.to_f
end

.rescue_json?Boolean

Returns:

  • (Boolean)

82
83
84
85
86
# File 'lib/env_helpers.rb', line 82

def rescue_json?
  return @rescue_json if defined? @rescue_json

  true
end

.true?(var_name) ⇒ Boolean

Check if an ENV variable represents `true`. ('true', 't', or '1'). Returns false if environment variable isn't defined.

Parameters:

  • var_name (String)

    ENV variable name

Returns:

  • (Boolean)

109
110
111
112
113
# File 'lib/env_helpers.rb', line 109

def true?(var_name)
  return false unless ENV.key?(var_name)

  true_value?(ENV[var_name])
end

.true_value?(val) ⇒ Boolean

Check if a value is true Meant to be overriden / monkey patched to customize functionality

Parameters:

  • val (String)

    value to check if true

Returns:

  • (Boolean)

131
132
133
# File 'lib/env_helpers.rb', line 131

def true_value?(val)
  EnvHelpers::Utils.true_value?(val)
end