Class: Typelizer::Configuration

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

Overview

Central registry for Typelizer multi-writer configuration

Responsibilities:

  • Holds immutable Config per writer name (always includes :default)

  • Maintain flat DSL setters for :default (e.g., config.output_dir = …)

  • Allows defining/updating named writers via writer(:name) { |cfg| … }

  • Check unique output_dir across writers to avoid file conflicts

Config priorities:

  • WriterContext merges in order: library defaults < global_settings < writer < DSL inheritance

  • global_settings are only updated by flat setters, not by writer(:default) blocks

Constant Summary collapse

DEFAULT_WRITER_NAME =
:default

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/typelizer/configuration.rb', line 28

def initialize
  @dirs = []
  @listen = nil

  default = Config.build

  @writers = {DEFAULT_WRITER_NAME => default.freeze}
  @global_settings = {}

  @writer_output_dirs = {DEFAULT_WRITER_NAME => normalize_path(default.output_dir)}
  @used_output_dirs = Set.new(@writer_output_dirs.values.compact)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)

Setters and readers to Writer(:default) config Keep the “flat” setters for the :default writer, for example:

config.output_dir = ...
config.prefer_double_quotes = true


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/typelizer/configuration.rb', line 91

def method_missing(name, *args, &block)
  name = name.to_s
  config_key = normalize_method_name(name)

  # Setters
  if name.end_with?("=") && args.length.positive?
    return super unless config_attribute?(config_key)

    val = args.first
    new_default = @writers[DEFAULT_WRITER_NAME].with_overrides(config_key => val)

    register_output_dir!(DEFAULT_WRITER_NAME, new_default.output_dir) if config_key == :output_dir

    @writers[DEFAULT_WRITER_NAME] = new_default.freeze

    return @global_settings[config_key] = val
  end

  # Readers
  return @writers[DEFAULT_WRITER_NAME].public_send(config_key) if args.empty? && config_attribute?(config_key)

  super
end

Instance Attribute Details

#dirsObject

Returns the value of attribute dirs.



21
22
23
# File 'lib/typelizer/configuration.rb', line 21

def dirs
  @dirs
end

#global_settingsObject (readonly)

Returns the value of attribute global_settings.



22
23
24
# File 'lib/typelizer/configuration.rb', line 22

def global_settings
  @global_settings
end

#listenObject

Returns the value of attribute listen.



21
22
23
# File 'lib/typelizer/configuration.rb', line 21

def listen
  @listen
end

#writersObject (readonly)

Returns the value of attribute writers.



22
23
24
# File 'lib/typelizer/configuration.rb', line 22

def writers
  @writers
end

Instance Method Details

#reset_writers!Object

Reset writers and keep only default writer



75
76
77
78
79
80
81
82
83
# File 'lib/typelizer/configuration.rb', line 75

def reset_writers!
  @writers.keep_if { |key, _| key == DEFAULT_WRITER_NAME }

  @writer_output_dirs = {
    DEFAULT_WRITER_NAME => normalize_path(@writers[DEFAULT_WRITER_NAME].output_dir)
  }

  @used_output_dirs = Set.new(@writer_output_dirs.values.compact)
end

#routesObject



24
25
26
# File 'lib/typelizer/configuration.rb', line 24

def routes
  @routes ||= RouteConfig.build
end

#writer(name = DEFAULT_WRITER_NAME, from: nil, &block) ⇒ Object

Defines or updates a writer configuration.

Inherits from the existing writer config (or global_settigns if absent), yields a mutable copy, then freezes and stores it. output_dir is unique and mandatory Also accepts “from” argument, which allows us to inherit configuration from any writer



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/typelizer/configuration.rb', line 46

def writer(name = DEFAULT_WRITER_NAME, from: nil, &block)
  writer_name = normalize_writer_name(name)

  # Inherit from existing writer config or from "from" attribute or global (flatt) config
  base_config =
    if @writers.key?(writer_name)
      @writers[writer_name]
    elsif from && @writers.key?(from.to_sym)
      @writers[from.to_sym]
    else
      Config.build(**@global_settings)
    end

  mutable_config = base_config.with_overrides

  block&.call(mutable_config)

  # Register output directory for uniqueness checking
  register_output_dir!(writer_name, mutable_config.output_dir)

  # Store and return frozen configuration
  @writers[writer_name] = mutable_config.freeze
end

#writer_config(name = DEFAULT_WRITER_NAME) ⇒ Object



70
71
72
# File 'lib/typelizer/configuration.rb', line 70

def writer_config(name = DEFAULT_WRITER_NAME)
  @writers.fetch((name || DEFAULT_WRITER_NAME).to_sym)
end