Class: Rhales::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/rhales/configuration.rb

Overview

Configuration management for Rhales library

Provides a clean, testable alternative to global configuration access. Supports block-based configuration typical of Ruby gems and dependency injection.

Usage:

Rhales.configure do |config|
  config.default_locale = 'en'
  config.template_paths = ['app/templates', 'lib/templates']
  config.features = { account_creation: true }
end

Defined Under Namespace

Classes: ConfigurationError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rhales/configuration.rb', line 34

def initialize
  # Set sensible defaults
  @default_locale         = 'en'
  @app_environment        = 'development'
  @development_enabled    = false
  @template_paths         = []
  @cache_templates        = true
  @csrf_token_name        = 'csrf_token'
  @nonce_header_name      = 'nonce'
  @csp_enabled            = true
  @auto_nonce             = true
  @csp_policy             = default_csp_policy
  @features               = {}
  @site_ssl_enabled       = false
  @cache_parsed_templates = true
  @cache_ttl              = 3600 # 1 hour
end

Instance Attribute Details

#api_base_urlObject

Build API base URL from site configuration



29
30
31
# File 'lib/rhales/configuration.rb', line 29

def api_base_url
  @api_base_url
end

#app_environmentObject

Core application settings



17
18
19
# File 'lib/rhales/configuration.rb', line 17

def app_environment
  @app_environment
end

#auto_nonceObject

Security settings



23
24
25
# File 'lib/rhales/configuration.rb', line 23

def auto_nonce
  @auto_nonce
end

#cache_parsed_templatesObject

Performance settings



32
33
34
# File 'lib/rhales/configuration.rb', line 32

def cache_parsed_templates
  @cache_parsed_templates
end

#cache_templatesObject

Template settings



20
21
22
# File 'lib/rhales/configuration.rb', line 20

def cache_templates
  @cache_templates
end

#cache_ttlObject

Performance settings



32
33
34
# File 'lib/rhales/configuration.rb', line 32

def cache_ttl
  @cache_ttl
end

#csp_enabledObject

Security settings



23
24
25
# File 'lib/rhales/configuration.rb', line 23

def csp_enabled
  @csp_enabled
end

#csp_policyObject

Security settings



23
24
25
# File 'lib/rhales/configuration.rb', line 23

def csp_policy
  @csp_policy
end

#csrf_token_nameObject

Security settings



23
24
25
# File 'lib/rhales/configuration.rb', line 23

def csrf_token_name
  @csrf_token_name
end

#default_localeObject

Core application settings



17
18
19
# File 'lib/rhales/configuration.rb', line 17

def default_locale
  @default_locale
end

#development_enabledObject

Core application settings



17
18
19
# File 'lib/rhales/configuration.rb', line 17

def development_enabled
  @development_enabled
end

#featuresObject

Feature flags



26
27
28
# File 'lib/rhales/configuration.rb', line 26

def features
  @features
end

#nonce_header_nameObject

Security settings



23
24
25
# File 'lib/rhales/configuration.rb', line 23

def nonce_header_name
  @nonce_header_name
end

#site_hostObject

Site configuration



29
30
31
# File 'lib/rhales/configuration.rb', line 29

def site_host
  @site_host
end

#site_ssl_enabledObject

Site configuration



29
30
31
# File 'lib/rhales/configuration.rb', line 29

def site_ssl_enabled
  @site_ssl_enabled
end

#template_pathsObject

Template settings



20
21
22
# File 'lib/rhales/configuration.rb', line 20

def template_paths
  @template_paths
end

#template_rootObject

Template settings



20
21
22
# File 'lib/rhales/configuration.rb', line 20

def template_root
  @template_root
end

Instance Method Details

#default_csp_policyObject

Default CSP policy with secure defaults



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rhales/configuration.rb', line 73

def default_csp_policy
  {
    'default-src' => ["'self'"],
    'script-src' => ["'self'", "'nonce-{{nonce}}'"],
    'style-src' => ["'self'", "'nonce-{{nonce}}'", "'unsafe-hashes'"],
    'img-src' => ["'self'", 'data:'],
    'font-src' => ["'self'"],
    'connect-src' => ["'self'"],
    'base-uri' => ["'self'"],
    'form-action' => ["'self'"],
    'frame-ancestors' => ["'none'"],
    'object-src' => ["'none'"],
    'media-src' => ["'self'"],
    'worker-src' => ["'self'"],
    'manifest-src' => ["'self'"],
    'prefetch-src' => ["'self'"],
    'upgrade-insecure-requests' => []
  }.freeze
end

#development?Boolean

Check if development mode is enabled

Returns:

  • (Boolean)


63
64
65
# File 'lib/rhales/configuration.rb', line 63

def development?
  @development_enabled || @app_environment == 'development'
end

#feature_enabled?(feature_name) ⇒ Boolean

Get feature flag value

Returns:

  • (Boolean)


94
95
96
# File 'lib/rhales/configuration.rb', line 94

def feature_enabled?(feature_name)
  @features[feature_name] || @features[feature_name.to_s] || false
end

#freeze!Object

Deep freeze configuration to prevent modification after setup



123
124
125
126
127
# File 'lib/rhales/configuration.rb', line 123

def freeze!
  @features.freeze
  @template_paths.freeze
  freeze
end

#production?Boolean

Check if production mode

Returns:

  • (Boolean)


68
69
70
# File 'lib/rhales/configuration.rb', line 68

def production?
  @app_environment == 'production'
end

#validate!Object

Validate configuration

Raises:



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/rhales/configuration.rb', line 99

def validate!
  errors = []

  # Validate locale
  if @default_locale.nil? || @default_locale.empty?
    errors << 'default_locale cannot be empty'
  end

  # Validate template paths exist if specified
  @template_paths.each do |path|
    unless Dir.exist?(path)
      errors << "Template path does not exist: #{path}"
    end
  end

  # Validate cache TTL
  if @cache_ttl && @cache_ttl <= 0
    errors << 'cache_ttl must be positive'
  end

  raise ConfigurationError, "Configuration errors: #{errors.join(', ')}" unless errors.empty?
end