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:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/tui/settings.rb', line 72

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

.load_defaults!Object

Populates ivars from the cached TEMPLATE hash without touching disk. Intended for test suites that want template defaults without paying the TOML parse cost for every example.



62
63
64
65
66
# File 'lib/tui/settings.rb', line 62

def load_defaults!
  TEMPLATE.each do |section, keys|
    keys.each { |key, value| instance_variable_set(:"@#{section}_#{key}", value) }
  end
end

.reset!Object

Restores template defaults and clears the path override. Useful in test teardown.



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

def reset!
  @config_path = nil
  load_defaults!
end