Module: Envalit

Defined in:
lib/envalit.rb,
lib/envalit/loader.rb,
lib/envalit/railtie.rb,
lib/envalit/version.rb,
lib/envalit/generators/install_generator.rb

Overview

Envalit is a Ruby gem for managing and validating environment variables.

It provides a simple and flexible way to:

  • Register environment variables with validation rules

  • Set default values for optional variables

  • Validate variable types (string, integer, boolean, float)

  • Control validation behavior (warnings vs. strict errors)

  • Load variables from .env files

Examples:

Basic usage

Envalit.configure do |config|
  config.register "DATABASE_URL",
                 required: true,
                 strict: true

  config.register "PORT",
                 type: :integer,
                 default: 3000
end

Envalit.validate # Validates all registered variables

Type validation

Envalit.configure do |config|
  config.register "DEBUG_MODE",
                 type: :boolean,
                 default: false

  config.register "API_TIMEOUT",
                 type: :float,
                 required: true
end

See Also:

Defined Under Namespace

Modules: Generators Classes: Loader, Railtie, ValidationError

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.configure {|self| ... } ⇒ void

This method returns an undefined value.

Configures Envalit with the given block.

Yields:

  • (self)

    Yields self for configuration



47
48
49
50
# File 'lib/envalit.rb', line 47

def configure
  @loader ||= Loader.new(Dir.pwd)
  yield(self)
end

.register(key, options = {}) ⇒ void

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



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

def register(key, options = {})
  @loader.register(key, options)
end

.validatevoid

This method returns an undefined value.

Validates all registered environment variables. Missing required variables will trigger warnings unless they are marked as strict.

Raises:



71
72
73
# File 'lib/envalit.rb', line 71

def validate
  @loader.validate
end

.validate!void

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:



80
81
82
# File 'lib/envalit.rb', line 80

def validate!
  @loader.validate!
end