class Savvy::EnvironmentReader

Constants

NONE

Public Class Methods

new(env = ENV) click to toggle source

@param [#[]] env

# File lib/savvy/environment_reader.rb, line 8
def initialize(env = ENV)
  @env = env
end

Public Instance Methods

[](*names, presence: true, raise_error: false, **options) click to toggle source

Like {#fetch}, but with ‘raise_error` set to false and `presence` set to true.

@param [<String>] names @param [Boolean] presence

# File lib/savvy/environment_reader.rb, line 16
def [](*names, presence: true, raise_error: false, **options)
  options[:raise_error] = raise_error
  options[:presence] = presence

  fetch(*names, **options)
end
fetch(*names, fallback: NONE, raise_error: true, presence: false) click to toggle source

@param [<String>] names @param [Object] fallback @param []

# File lib/savvy/environment_reader.rb, line 26
def fetch(*names, fallback: NONE, raise_error: true, presence: false)
  names.flatten!

  names.each do |name|
    if ( presence && has_value?(name) ) || set?(name)
      return @env[name]
    end
  end

  if fallback != NONE
    return fallback
  elsif raise_error
    raise ArgumentError, "No #{'present ' if presence}env value found for #{names.join(', ')}"
  end
end
has_value?(var) click to toggle source

Check if the environment has a present value set.

@param [String] var

# File lib/savvy/environment_reader.rb, line 46
def has_value?(var)
  set?(var) && Dux.presentish?(@env[var])
end
set?(var) click to toggle source

Checks only if the variable is set, not necessarily that it is non-empty.

@param [String] var

# File lib/savvy/environment_reader.rb, line 54
def set?(var)
  @env.key?(var)
end