Class: Fasti::Config

Inherits:
Object
  • Object
show all
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.

Examples:

Basic usage

Fasti.configure do |config|
  config.format = :quarter
  config.country = :jp
end

With styling

Fasti.configure do |config|
  config.style = {
    sunday: { foreground: :red, bold: true },
    holiday: { background: :yellow }
  }
end

Defined Under Namespace

Modules: Schema, Types

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load_from_file(file_path) ⇒ Hash

Load configuration from a Ruby file

Parameters:

  • file_path (String)

    Path to configuration file

Returns:

  • (Hash)

    Configuration hash loaded from file

Raises:



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.message}"
  rescue => e
    raise ConfigError, "Error loading configuration from #{file_path}: #{e.message}"
  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

Parameters:

  • style_hash (Hash)

    Raw style configuration

Returns:

  • (Hash<Symbol, Hash>)

    Validated and normalized style hash

Raises:

  • (ArgumentError)

    If validation fails



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, messages|
        "#{key}: #{Array(messages).join(", ")}"
      }.join("; ")
      raise ArgumentError, "Invalid style attributes for #{target}: #{errors}"
    end
  end

  validated_style
end

Instance Method Details

#styleHash<Symbol, Hash>

Style configuration Accepts a hash mapping style targets to their attributes

Parameters:

  • value (Hash<Symbol|String, Hash>)

    Style configuration hash

Returns:

  • (Hash<Symbol, Hash>)

    Validated and normalized style hash



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