Class: Envalit::Loader Private

Inherits:
Object
  • Object
show all
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

Examples:

Basic usage with type validation

loader = Loader.new(Rails.root)
loader.register("PORT", type: :integer, default: 3000)
loader.register("DEBUG", type: :boolean, default: false)
loader.validate

Strict validation with required variables

loader = Loader.new(Dir.pwd)
loader.register("API_KEY", required: true, strict: true)
loader.register("API_URL", required: true)
# Will raise ValidationError if API_KEY is missing
# Will warn if API_URL is missing
loader.validate

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

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.

Parameters:

  • app_root (String) (defaults to: Dir.pwd)

    The root directory path where .env files are located

  • app_root (String, nil) (defaults to: Dir.pwd)

    The root directory path where .env files are located. Defaults to Dir.pwd



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

#loadvoid

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.

Parameters:

  • key (String)

    The environment variable name

  • options (Hash) (defaults to: {})

    The validation options

Options Hash (options):

  • :required (Boolean)

    Whether the variable is required

  • :strict (Boolean)

    Raise error instead of warning for missing required variables

  • :type (Symbol)

    The variable type (:string, :integer, :boolean, :float)

  • :default (Object)

    The default value if variable is not set

  • :description (String)

    Description of the variable’s purpose

Raises:

  • (ArgumentError)

    If an invalid type is specified



61
62
63
64
65
# File 'lib/envalit/loader.rb', line 61

def register(key, options = {})
  validate_options(options)
  set_default_value(key, options[:default]) if options.key?(:default)
  @schema[key] = options
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

Parameters:

  • strict (Boolean) (defaults to: false)

    Whether to enforce strict mode for all required variables

Raises:



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.

Raises:



91
92
93
# File 'lib/envalit/loader.rb', line 91

def validate!
  validate(strict: true)
end