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.



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/rhales/configuration.rb', line 163

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

  # Logging defaults
  @allowed_unescaped_variables      = []

  # Hydration mismatch reporting defaults
  @hydration_mismatch_format        = :compact  # :compact, :multiline, :sidebyside, :json
  @hydration_authority              = :schema   # :schema or :data

  # External schema defaults
  @schema_search_paths              = []        # Additional paths to search for external schemas
  @schema_tsconfig_path             = nil       # Path to tsconfig.json for tsx import execution
  @schema_use_tsx_import            = false     # Use tsx import (runs through project tsconfig)

  # Yield to block for configuration if provided
  yield(self) if block_given?
end

Instance Attribute Details

#allowed_unescaped_variablesObject

Logging settings



155
156
157
# File 'lib/rhales/configuration.rb', line 155

def allowed_unescaped_variables
  @allowed_unescaped_variables
end

#api_base_urlObject

Build API base URL from site configuration



140
141
142
# File 'lib/rhales/configuration.rb', line 140

def api_base_url
  @api_base_url
end

#app_environmentObject

Core application settings



128
129
130
# File 'lib/rhales/configuration.rb', line 128

def app_environment
  @app_environment
end

#auto_nonceObject

Security settings



134
135
136
# File 'lib/rhales/configuration.rb', line 134

def auto_nonce
  @auto_nonce
end

#cache_parsed_templatesObject

Performance settings



143
144
145
# File 'lib/rhales/configuration.rb', line 143

def cache_parsed_templates
  @cache_parsed_templates
end

#cache_templatesObject

Template settings



131
132
133
# File 'lib/rhales/configuration.rb', line 131

def cache_templates
  @cache_templates
end

#cache_ttlObject

Performance settings



143
144
145
# File 'lib/rhales/configuration.rb', line 143

def cache_ttl
  @cache_ttl
end

#csp_enabledObject

Security settings



134
135
136
# File 'lib/rhales/configuration.rb', line 134

def csp_enabled
  @csp_enabled
end

#csp_policyObject

Security settings



134
135
136
# File 'lib/rhales/configuration.rb', line 134

def csp_policy
  @csp_policy
end

#csrf_token_nameObject

Security settings



134
135
136
# File 'lib/rhales/configuration.rb', line 134

def csrf_token_name
  @csrf_token_name
end

#default_localeObject

Core application settings



128
129
130
# File 'lib/rhales/configuration.rb', line 128

def default_locale
  @default_locale
end

#development_enabledObject

Core application settings



128
129
130
# File 'lib/rhales/configuration.rb', line 128

def development_enabled
  @development_enabled
end

#enable_json_responderObject

JSON response settings



152
153
154
# File 'lib/rhales/configuration.rb', line 152

def enable_json_responder
  @enable_json_responder
end

#enable_schema_validationObject

Schema validation settings



149
150
151
# File 'lib/rhales/configuration.rb', line 149

def enable_schema_validation
  @enable_schema_validation
end

#fail_on_validation_errorObject

Schema validation settings



149
150
151
# File 'lib/rhales/configuration.rb', line 149

def fail_on_validation_error
  @fail_on_validation_error
end

#featuresObject

Feature flags



137
138
139
# File 'lib/rhales/configuration.rb', line 137

def features
  @features
end

#hydrationObject

Hydration settings



146
147
148
# File 'lib/rhales/configuration.rb', line 146

def hydration
  @hydration
end

#hydration_authorityObject

Hydration mismatch reporting settings



158
159
160
# File 'lib/rhales/configuration.rb', line 158

def hydration_authority
  @hydration_authority
end

#hydration_mismatch_formatObject

Hydration mismatch reporting settings



158
159
160
# File 'lib/rhales/configuration.rb', line 158

def hydration_mismatch_format
  @hydration_mismatch_format
end

#json_responder_include_metadataObject

JSON response settings



152
153
154
# File 'lib/rhales/configuration.rb', line 152

def 
  @json_responder_include_metadata
end

#nonce_header_nameObject

Security settings



134
135
136
# File 'lib/rhales/configuration.rb', line 134

def nonce_header_name
  @nonce_header_name
end

#schema_search_pathsObject

External schema settings



161
162
163
# File 'lib/rhales/configuration.rb', line 161

def schema_search_paths
  @schema_search_paths
end

#schema_tsconfig_pathObject

External schema settings



161
162
163
# File 'lib/rhales/configuration.rb', line 161

def schema_tsconfig_path
  @schema_tsconfig_path
end

#schema_use_tsx_importObject

External schema settings



161
162
163
# File 'lib/rhales/configuration.rb', line 161

def schema_use_tsx_import
  @schema_use_tsx_import
end

#schemas_dirObject

Schema validation settings



149
150
151
# File 'lib/rhales/configuration.rb', line 149

def schemas_dir
  @schemas_dir
end

#site_hostObject

Site configuration



140
141
142
# File 'lib/rhales/configuration.rb', line 140

def site_host
  @site_host
end

#site_ssl_enabledObject

Site configuration



140
141
142
# File 'lib/rhales/configuration.rb', line 140

def site_ssl_enabled
  @site_ssl_enabled
end

#template_pathsObject

Template settings



131
132
133
# File 'lib/rhales/configuration.rb', line 131

def template_paths
  @template_paths
end

#template_rootObject

Template settings



131
132
133
# File 'lib/rhales/configuration.rb', line 131

def template_root
  @template_root
end

Instance Method Details

#default_csp_policyObject

Default CSP policy with secure defaults



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/rhales/configuration.rb', line 227

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)


217
218
219
# File 'lib/rhales/configuration.rb', line 217

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

#feature_enabled?(feature_name) ⇒ Boolean

Get feature flag value

Returns:

  • (Boolean)


248
249
250
# File 'lib/rhales/configuration.rb', line 248

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



284
285
286
287
288
289
# File 'lib/rhales/configuration.rb', line 284

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

#production?Boolean

Check if production mode

Returns:

  • (Boolean)


222
223
224
# File 'lib/rhales/configuration.rb', line 222

def production?
  @app_environment == 'production'
end

#validate!Object

Validate configuration

Raises:



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/rhales/configuration.rb', line 253

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 schema search paths exist if specified
  @schema_search_paths.each do |path|
    unless Dir.exist?(path)
      errors << "Schema search 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