Module: Tina4::Env

Defined in:
lib/tina4/env.rb

Constant Summary collapse

DEFAULT_ENV =
{
  "PROJECT_NAME" => "Tina4 Ruby Project",
  "TINA4_SWAGGER_VERSION" => "1.0.0",
  "TINA4_LOCALE" => "en",
  "TINA4_DEBUG" => "true",
  "TINA4_LOG_LEVEL" => "[TINA4_LOG_ALL]",
  "TINA4_SECRET" => "tina4-secret-change-me"
}.freeze

Class Method Summary collapse

Class Method Details

.all_envObject

Return all current ENV vars as a hash



114
115
116
# File 'lib/tina4/env.rb', line 114

def all_env
  ENV.to_h
end

.get_env(key, default = nil) ⇒ Object

Get an env var value, with optional default



104
105
106
# File 'lib/tina4/env.rb', line 104

def get_env(key, default = nil)
  ENV[key.to_s] || default
end

.has_env?(key) ⇒ Boolean

Check if an env var exists

Returns:

  • (Boolean)


109
110
111
# File 'lib/tina4/env.rb', line 109

def has_env?(key)
  ENV.key?(key.to_s)
end

.is_truthy(val) ⇒ Object

Check if a value is truthy for env boolean checks.

Accepts: “true”, “True”, “TRUE”, “1”, “yes”, “Yes”, “YES”, “on”, “On”, “ON”. Everything else is falsy (including empty string, nil, not set).



90
91
92
# File 'lib/tina4/env.rb', line 90

def self.is_truthy(val)
  %w[true 1 yes on].include?(val.to_s.strip.downcase)
end

.load_env(root_dir = Dir.pwd) ⇒ Object



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

def load_env(root_dir = Dir.pwd)
  env_file = resolve_env_file(root_dir)
  unless File.exist?(env_file)
    create_default_env(env_file)
  end
  parse_env_file(env_file)
end

.require_env!(*keys) ⇒ Object

Raise if any of the given keys are missing from ENV



119
120
121
122
123
124
# File 'lib/tina4/env.rb', line 119

def require_env!(*keys)
  missing = keys.map(&:to_s).reject { |k| ENV.key?(k) }
  unless missing.empty?
    raise KeyError, "Missing required env vars: #{missing.join(', ')}"
  end
end

.reset_envObject

Reset: clear all env vars that were loaded (restore to process defaults)



127
128
129
130
# File 'lib/tina4/env.rb', line 127

def reset_env
  @loaded_keys&.each { |k| ENV.delete(k) }
  @loaded_keys = []
end