Module: TUI::Settings

Defined in:
lib/tui/settings.rb

Overview

TUI-specific configuration backed by ~/.anima/tui.toml.

Zero Rails dependency — the TUI is a standalone client process.

Accessors are generated automatically from the template TOML file. Convention: method name = section_key (e.g. [hud] min_widthhud_min_width). To add a setting, add the key to tui.toml — the accessor appears automatically.

Settings are loaded once at startup. Restart the TUI to pick up changes — it’s a thin client, the brain won’t notice.

Examples:

Reading a setting

TUI::Settings.connection_default_host  #=> "localhost:42134"
TUI::Settings.hud_min_width            #=> 24

See Also:

Defined Under Namespace

Classes: MissingConfigError, MissingSettingError

Constant Summary collapse

DEFAULT_PATH =
File.expand_path("~/.anima/tui.toml")
TEMPLATE_PATH =
File.expand_path("../../../templates/tui.toml", __FILE__)
TEMPLATE =
TomlRB.load_file(TEMPLATE_PATH)

Class Method Summary collapse

Class Method Details

.config_pathString

Returns active config file path.

Returns:

  • (String)

    active config file path



48
49
50
# File 'lib/tui/settings.rb', line 48

def config_path
  @config_path || DEFAULT_PATH
end

.config_path=(path) ⇒ Object

Override config file path (for testing). Triggers a load so the new config takes effect immediately.

Parameters:

  • path (String, nil)

    custom path, or nil to restore default



42
43
44
45
# File 'lib/tui/settings.rb', line 42

def config_path=(path)
  @config_path = path
  load! if path
end

.load!Object

Parses the config file and populates all setting ivars.

Raises:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/tui/settings.rb', line 65

def load!
  path = config_path
  unless File.exist?(path)
    raise MissingConfigError,
      "TUI config file not found: #{path}. Run `anima install` to create it."
  end

  parsed = TomlRB.load_file(path)
  TEMPLATE.each do |section, keys|
    keys.each_key do |key|
      value = parsed.dig(section, key)
      if value.nil?
        raise MissingSettingError,
          "[#{section}] #{key} is not set in #{path}. Run `anima update` to add missing settings."
      end
      instance_variable_set(:"@#{section}_#{key}", value)
    end
  end
end

.reset!Object

Clears all loaded settings and resets to default path. Useful in test teardown.



54
55
56
57
58
59
# File 'lib/tui/settings.rb', line 54

def reset!
  @config_path = nil
  TEMPLATE.each do |section, keys|
    keys.each_key { |key| instance_variable_set(:"@#{section}_#{key}", nil) }
  end
end