Class: Rhales::Configuration
- Inherits:
-
Object
- Object
- Rhales::Configuration
- 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
-
#api_base_url ⇒ Object
Build API base URL from site configuration.
-
#app_environment ⇒ Object
Core application settings.
-
#auto_nonce ⇒ Object
Security settings.
-
#cache_parsed_templates ⇒ Object
Performance settings.
-
#cache_templates ⇒ Object
Template settings.
-
#cache_ttl ⇒ Object
Performance settings.
-
#csp_enabled ⇒ Object
Security settings.
-
#csp_policy ⇒ Object
Security settings.
-
#csrf_token_name ⇒ Object
Security settings.
-
#default_locale ⇒ Object
Core application settings.
-
#development_enabled ⇒ Object
Core application settings.
-
#enable_json_responder ⇒ Object
JSON response settings.
-
#enable_schema_validation ⇒ Object
Schema validation settings.
-
#fail_on_validation_error ⇒ Object
Schema validation settings.
-
#features ⇒ Object
Feature flags.
-
#hydration ⇒ Object
Hydration settings.
-
#json_responder_include_metadata ⇒ Object
JSON response settings.
-
#nonce_header_name ⇒ Object
Security settings.
-
#schemas_dir ⇒ Object
Schema validation settings.
-
#site_host ⇒ Object
Site configuration.
-
#site_ssl_enabled ⇒ Object
Site configuration.
-
#template_paths ⇒ Object
Template settings.
-
#template_root ⇒ Object
Template settings.
Instance Method Summary collapse
-
#default_csp_policy ⇒ Object
Default CSP policy with secure defaults.
-
#development? ⇒ Boolean
Check if development mode is enabled.
-
#feature_enabled?(feature_name) ⇒ Boolean
Get feature flag value.
-
#freeze! ⇒ Object
Deep freeze configuration to prevent modification after setup.
-
#initialize {|_self| ... } ⇒ Configuration
constructor
A new instance of Configuration.
-
#production? ⇒ Boolean
Check if production mode.
-
#validate! ⇒ Object
Validate configuration.
Constructor Details
#initialize {|_self| ... } ⇒ Configuration
Returns a new instance of Configuration.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/rhales/configuration.rb', line 152 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 @hydration = HydrationConfiguration.new # Schema validation defaults @enable_schema_validation = true @fail_on_validation_error = false # Set by environment in middleware @schemas_dir = './public/schemas' # Default to implementing project's public directory # JSON responder defaults @enable_json_responder = true @json_responder_include_metadata = false # Yield to block for configuration if provided yield(self) if block_given? end |
Instance Attribute Details
#api_base_url ⇒ Object
Build API base URL from site configuration
138 139 140 |
# File 'lib/rhales/configuration.rb', line 138 def api_base_url @api_base_url end |
#app_environment ⇒ Object
Core application settings
126 127 128 |
# File 'lib/rhales/configuration.rb', line 126 def app_environment @app_environment end |
#auto_nonce ⇒ Object
Security settings
132 133 134 |
# File 'lib/rhales/configuration.rb', line 132 def auto_nonce @auto_nonce end |
#cache_parsed_templates ⇒ Object
Performance settings
141 142 143 |
# File 'lib/rhales/configuration.rb', line 141 def cache_parsed_templates @cache_parsed_templates end |
#cache_templates ⇒ Object
Template settings
129 130 131 |
# File 'lib/rhales/configuration.rb', line 129 def cache_templates @cache_templates end |
#cache_ttl ⇒ Object
Performance settings
141 142 143 |
# File 'lib/rhales/configuration.rb', line 141 def cache_ttl @cache_ttl end |
#csp_enabled ⇒ Object
Security settings
132 133 134 |
# File 'lib/rhales/configuration.rb', line 132 def csp_enabled @csp_enabled end |
#csp_policy ⇒ Object
Security settings
132 133 134 |
# File 'lib/rhales/configuration.rb', line 132 def csp_policy @csp_policy end |
#csrf_token_name ⇒ Object
Security settings
132 133 134 |
# File 'lib/rhales/configuration.rb', line 132 def csrf_token_name @csrf_token_name end |
#default_locale ⇒ Object
Core application settings
126 127 128 |
# File 'lib/rhales/configuration.rb', line 126 def default_locale @default_locale end |
#development_enabled ⇒ Object
Core application settings
126 127 128 |
# File 'lib/rhales/configuration.rb', line 126 def development_enabled @development_enabled end |
#enable_json_responder ⇒ Object
JSON response settings
150 151 152 |
# File 'lib/rhales/configuration.rb', line 150 def enable_json_responder @enable_json_responder end |
#enable_schema_validation ⇒ Object
Schema validation settings
147 148 149 |
# File 'lib/rhales/configuration.rb', line 147 def enable_schema_validation @enable_schema_validation end |
#fail_on_validation_error ⇒ Object
Schema validation settings
147 148 149 |
# File 'lib/rhales/configuration.rb', line 147 def fail_on_validation_error @fail_on_validation_error end |
#features ⇒ Object
Feature flags
135 136 137 |
# File 'lib/rhales/configuration.rb', line 135 def features @features end |
#hydration ⇒ Object
Hydration settings
144 145 146 |
# File 'lib/rhales/configuration.rb', line 144 def hydration @hydration end |
#json_responder_include_metadata ⇒ Object
JSON response settings
150 151 152 |
# File 'lib/rhales/configuration.rb', line 150 def @json_responder_include_metadata end |
#nonce_header_name ⇒ Object
Security settings
132 133 134 |
# File 'lib/rhales/configuration.rb', line 132 def nonce_header_name @nonce_header_name end |
#schemas_dir ⇒ Object
Schema validation settings
147 148 149 |
# File 'lib/rhales/configuration.rb', line 147 def schemas_dir @schemas_dir end |
#site_host ⇒ Object
Site configuration
138 139 140 |
# File 'lib/rhales/configuration.rb', line 138 def site_host @site_host end |
#site_ssl_enabled ⇒ Object
Site configuration
138 139 140 |
# File 'lib/rhales/configuration.rb', line 138 def site_ssl_enabled @site_ssl_enabled end |
#template_paths ⇒ Object
Template settings
129 130 131 |
# File 'lib/rhales/configuration.rb', line 129 def template_paths @template_paths end |
#template_root ⇒ Object
Template settings
129 130 131 |
# File 'lib/rhales/configuration.rb', line 129 def template_root @template_root end |
Instance Method Details
#default_csp_policy ⇒ Object
Default CSP policy with secure defaults
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/rhales/configuration.rb', line 204 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
194 195 196 |
# File 'lib/rhales/configuration.rb', line 194 def development? @development_enabled || @app_environment == 'development' end |
#feature_enabled?(feature_name) ⇒ Boolean
Get feature flag value
225 226 227 |
# File 'lib/rhales/configuration.rb', line 225 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
254 255 256 257 258 |
# File 'lib/rhales/configuration.rb', line 254 def freeze! @features.freeze @template_paths.freeze freeze end |
#production? ⇒ Boolean
Check if production mode
199 200 201 |
# File 'lib/rhales/configuration.rb', line 199 def production? @app_environment == 'production' end |
#validate! ⇒ Object
Validate configuration
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/rhales/configuration.rb', line 230 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 |