Class: Fasti::Config
- Inherits:
-
Object
- Object
- Fasti::Config
- Extended by:
- Dry::Configurable
- Defined in:
- lib/fasti/config.rb,
lib/fasti/config/types.rb,
lib/fasti/config/schema.rb
Overview
Configuration management using dry-configurable
Provides a clean interface for managing fasti configuration with type safety and validation.
Defined Under Namespace
Class Method Summary collapse
-
.load_from_file(file_path) ⇒ Hash
Load configuration from a Ruby file.
-
.reset! ⇒ Object
Reset configuration to defaults.
-
.validate_and_normalize_style(style_hash) ⇒ Hash<Symbol, Hash>
Validates and normalizes a style configuration hash.
Instance Method Summary collapse
-
#style ⇒ Hash<Symbol, Hash>
Style configuration Accepts a hash mapping style targets to their attributes.
Class Method Details
.load_from_file(file_path) ⇒ Hash
Load configuration from a Ruby file
98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/fasti/config.rb', line 98 def self.load_from_file(file_path) return {} unless File.exist?(file_path) begin # Execute the configuration file in the context of Fasti instance_eval(File.read(file_path), file_path) config.to_h rescue SyntaxError => e raise ConfigError, "Invalid Ruby syntax in #{file_path}: #{e.}" rescue => e raise ConfigError, "Error loading configuration from #{file_path}: #{e.}" end end |
.reset! ⇒ Object
Reset configuration to defaults
84 85 86 87 88 89 90 91 |
# File 'lib/fasti/config.rb', line 84 def self.reset! configure do |config| config.format = :month config.start_of_week = :sunday config.country = :us config.style = nil end end |
.validate_and_normalize_style(style_hash) ⇒ Hash<Symbol, Hash>
Validates and normalizes a style configuration hash
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/fasti/config.rb', line 56 def self.validate_and_normalize_style(style_hash) validated_style = {} style_hash.each do |target, attributes| # Validate and convert target target_sym = Types::StyleTarget.call(target) # Validate attributes structure unless attributes.is_a?(Hash) raise ArgumentError, "Style attributes for #{target} must be a Hash, got #{attributes.class}" end # Validate individual attributes using schema result = Schema::StyleAttribute.call(attributes) if result.success? validated_style[target_sym] = TIntMe[**result.to_h] else errors = result.errors.to_h.map {|key, | "#{key}: #{Array().join(", ")}" }.join("; ") raise ArgumentError, "Invalid style attributes for #{target}: #{errors}" end end validated_style end |
Instance Method Details
#style ⇒ Hash<Symbol, Hash>
Style configuration Accepts a hash mapping style targets to their attributes
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/fasti/config.rb', line 40 setting :style, default: nil, constructor: ->(value) do case value when nil nil when Hash validate_and_normalize_style(value) else raise ArgumentError, "Style must be nil or Hash, got #{value.class}" end end |