Module: HatiConfig::Environment

Included in:
Setting, Setting
Defined in:
lib/hati_config/environment.rb

Overview

Environment module provides functionality for managing environment-specific configurations.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.current_environment ⇒ Symbol

Gets the current environment.

Returns:

  • (Symbol) —

    The current environment



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/hati_config/environment.rb', line 33

def self.current_environment
  @current_environment ||= begin
    env = if ENV['HATI_ENV']
            ENV['HATI_ENV']
          elsif ENV['RACK_ENV']
            ENV['RACK_ENV']
          elsif ENV['RAILS_ENV']
            ENV['RAILS_ENV']
          else
            'development'
          end
    env.to_sym
  end
end

.current_environment=(env) ⇒ Object

Sets the current environment.

Examples:

HatiConfig.environment = :production

Parameters:

  • env (Symbol) —

    The environment to set



26
27
28
# File 'lib/hati_config/environment.rb', line 26

def self.current_environment=(env)
  @current_environment = env&.to_sym
end

.with_environment(env) { ... } ⇒ Object

Temporarily changes the environment for a block of code.

Examples:

HatiConfig.with_environment(:staging) do
  # Configuration will use staging environment here
end

Parameters:

  • env (Symbol) —

    The environment to use

Yields:

  • The block to execute in the specified environment



63
64
65
66
67
68
69
# File 'lib/hati_config/environment.rb', line 63

def self.with_environment(env)
  original_env = current_environment
  self.current_environment = env
  yield
ensure
  self.current_environment = original_env
end

Instance Method Details

#current_environment ⇒ Symbol

Gets the current environment.

Returns:

  • (Symbol) —

    The current environment



51
52
53
# File 'lib/hati_config/environment.rb', line 51

def current_environment
  Environment.current_environment
end

#development? ⇒ Boolean

Checks if the current environment is development.

Returns:

  • (Boolean) —

    True if in development environment



82
83
84
# File 'lib/hati_config/environment.rb', line 82

def development?
  environment?(:development)
end

#environment(env_name) { ... } ⇒ Object

Defines environment-specific configuration overrides.

Examples:

environment :development do
  config api_url: 'http://localhost:3000'
  config debug: true
end

Parameters:

  • env_name (Symbol) —

    The name of the environment (e.g., :development, :staging, :production)

Yields:

  • The configuration block for the environment



15
16
17
18
19
# File 'lib/hati_config/environment.rb', line 15

def environment(env_name, &block)
  return unless current_environment == env_name

  instance_eval(&block)
end

#environment?(env) ⇒ Boolean

Checks if the current environment matches the given environment.

Parameters:

  • env (Symbol) —

    The environment to check

Returns:

  • (Boolean) —

    True if the current environment matches



75
76
77
# File 'lib/hati_config/environment.rb', line 75

def environment?(env)
  current_environment == env.to_sym
end

#production? ⇒ Boolean

Checks if the current environment is production.

Returns:

  • (Boolean) —

    True if in production environment



103
104
105
# File 'lib/hati_config/environment.rb', line 103

def production?
  environment?(:production)
end

#staging? ⇒ Boolean

Checks if the current environment is staging.

Returns:

  • (Boolean) —

    True if in staging environment



96
97
98
# File 'lib/hati_config/environment.rb', line 96

def staging?
  environment?(:staging)
end

#test? ⇒ Boolean

Checks if the current environment is test.

Returns:

  • (Boolean) —

    True if in test environment



89
90
91
# File 'lib/hati_config/environment.rb', line 89

def test?
  environment?(:test)
end