Class: Envalit::Loader Private
- Inherits:
-
Object
- Object
- Envalit::Loader
- Defined in:
- lib/envalit/loader.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
The Loader class handles environment variable registration, validation, and type checking.
This class is responsible for:
-
Managing the schema of environment variables
-
Loading variables from .env files
-
Validating variable presence and types
-
Handling default values
-
Providing warnings or errors for missing variables
Constant Summary collapse
- VALID_TYPES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Valid types for environment variables
%i[string integer boolean float].freeze
- ERROR_COLOR =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
ANSI color codes for error formatting
"\e[1;91m"- RESET_COLOR =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
bright red and bold
"\e[0m"- HIGHLIGHT_COLOR =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
bold
"\e[1m"
Instance Method Summary collapse
-
#initialize(app_root = Dir.pwd) ⇒ Loader
constructor
private
Initializes a new Loader instance.
-
#load ⇒ void
private
Loads environment variables from a .env file.
-
#register(key, options = {}) ⇒ void
private
Registers an environment variable with validation options.
-
#validate(strict: false) ⇒ void
private
Validates all registered environment variables.
-
#validate! ⇒ void
private
Validates all registered environment variables in strict mode.
Constructor Details
#initialize(app_root = Dir.pwd) ⇒ Loader
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initializes a new Loader instance.
43 44 45 46 47 |
# File 'lib/envalit/loader.rb', line 43 def initialize(app_root = Dir.pwd) @app_root = app_root @schema = {} @dotenv_loaded = false end |
Instance Method Details
#load ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Loads environment variables from a .env file.
98 99 100 |
# File 'lib/envalit/loader.rb', line 98 def load load_dotenv end |
#register(key, options = {}) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Registers an environment variable with validation options.
61 62 63 64 65 |
# File 'lib/envalit/loader.rb', line 61 def register(key, = {}) () set_default_value(key, [:default]) if .key?(:default) @schema[key] = end |
#validate(strict: false) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Validates all registered environment variables.
This method:
-
Loads variables from .env file if not already loaded
-
Checks for missing required variables
-
Validates variable types
-
Raises errors for strict violations
-
Warns about non-strict violations
80 81 82 83 84 |
# File 'lib/envalit/loader.rb', line 80 def validate(strict: false) load_dotenv unless @dotenv_loaded check_missing_variables(strict) check_invalid_types end |
#validate! ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Validates all registered environment variables in strict mode. All missing required variables will raise an error, regardless of their strict setting.
91 92 93 |
# File 'lib/envalit/loader.rb', line 91 def validate! validate(strict: true) end |