Module: Tina4::Env
- Defined in:
- lib/tina4/env.rb
Constant Summary collapse
- DEFAULT_ENV =
{ "PROJECT_NAME" => "Tina4 Ruby Project", "VERSION" => "1.0.0", "TINA4_LOCALE" => "en", "TINA4_DEBUG" => "true", "TINA4_LOG_LEVEL" => "[TINA4_LOG_ALL]", "SECRET" => "tina4-secret-change-me" }.freeze
Class Method Summary collapse
-
.all_env ⇒ Object
Return all current ENV vars as a hash.
-
.get_env(key, default = nil) ⇒ Object
Get an env var value, with optional default.
-
.has_env?(key) ⇒ Boolean
Check if an env var exists.
-
.is_truthy(val) ⇒ Object
Check if a value is truthy for env boolean checks.
- .load_env(root_dir = Dir.pwd) ⇒ Object
-
.require_env!(*keys) ⇒ Object
Raise if any of the given keys are missing from ENV.
-
.reset_env ⇒ Object
Reset: clear all env vars that were loaded (restore to process defaults).
Class Method Details
.all_env ⇒ Object
Return all current ENV vars as a hash
43 44 45 |
# File 'lib/tina4/env.rb', line 43 def all_env ENV.to_h end |
.get_env(key, default = nil) ⇒ Object
Get an env var value, with optional default
33 34 35 |
# File 'lib/tina4/env.rb', line 33 def get_env(key, default = nil) ENV[key.to_s] || default end |
.has_env?(key) ⇒ Boolean
Check if an env var exists
38 39 40 |
# File 'lib/tina4/env.rb', line 38 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).
19 20 21 |
# File 'lib/tina4/env.rb', line 19 def self.is_truthy(val) %w[true 1 yes on].include?(val.to_s.strip.downcase) end |
.load_env(root_dir = Dir.pwd) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/tina4/env.rb', line 24 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
48 49 50 51 52 53 |
# File 'lib/tina4/env.rb', line 48 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_env ⇒ Object
Reset: clear all env vars that were loaded (restore to process defaults)
56 57 58 59 |
# File 'lib/tina4/env.rb', line 56 def reset_env @loaded_keys&.each { |k| ENV.delete(k) } @loaded_keys = [] end |