Class: Sxn::Config::TemplatesConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/sxn/config/templates_config.rb

Overview

Handles loading and saving session templates from .sxn/templates.yml

Templates define collections of projects (worktree configurations) that can be applied when creating a session.

Constant Summary collapse

TEMPLATES_FILE =
"templates.yml"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sxn_path) ⇒ TemplatesConfig

Returns a new instance of TemplatesConfig.



17
18
19
20
# File 'lib/sxn/config/templates_config.rb', line 17

def initialize(sxn_path)
  @sxn_path = Pathname.new(sxn_path)
  @templates_file_path = @sxn_path / TEMPLATES_FILE
end

Instance Attribute Details

#sxn_pathObject (readonly)

Returns the value of attribute sxn_path.



15
16
17
# File 'lib/sxn/config/templates_config.rb', line 15

def sxn_path
  @sxn_path
end

#templates_file_pathObject (readonly)

Returns the value of attribute templates_file_path.



15
16
17
# File 'lib/sxn/config/templates_config.rb', line 15

def templates_file_path
  @templates_file_path
end

Instance Method Details

#exists?Boolean

Check if templates file exists

Returns:



50
51
52
# File 'lib/sxn/config/templates_config.rb', line 50

def exists?
  templates_file_path.exist?
end

#get_template(name) ⇒ Hash?

Get a specific template by name

Parameters:

  • Template name

Returns:

  • Template configuration or nil if not found



57
58
59
60
61
62
63
64
65
66
# File 'lib/sxn/config/templates_config.rb', line 57

def get_template(name)
  config = load
  templates = config["templates"] || {}
  template = templates[name]

  return nil unless template

  # Normalize project entries to hashes
  normalize_template(name, template)
end

#list_template_namesArray<String>

List all template names

Returns:

  • Template names



70
71
72
73
# File 'lib/sxn/config/templates_config.rb', line 70

def list_template_names
  config = load
  (config["templates"] || {}).keys
end

#loadHash

Load templates configuration from file

Returns:

  • Templates configuration



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sxn/config/templates_config.rb', line 24

def load
  return default_config unless templates_file_path.exist?

  content = File.read(templates_file_path)
  config = YAML.safe_load(content, permitted_classes: [], permitted_symbols: [], aliases: false) || {}
  normalize_config(config)
rescue Psych::SyntaxError => e
  raise ConfigurationError, "Invalid YAML in #{templates_file_path}: #{e.message}"
rescue StandardError => e
  raise ConfigurationError, "Failed to load templates file #{templates_file_path}: #{e.message}"
end

#remove_template(name) ⇒ Boolean

Remove a template

Parameters:

  • Template name

Returns:

  • True if template was removed



88
89
90
91
92
93
94
95
96
97
# File 'lib/sxn/config/templates_config.rb', line 88

def remove_template(name)
  config = load
  templates = config["templates"] || {}

  return false unless templates.key?(name)

  templates.delete(name)
  save(config)
  true
end

#save(config) ⇒ Object

Save templates configuration to file

Parameters:

  • Templates configuration



38
39
40
41
42
43
44
45
46
# File 'lib/sxn/config/templates_config.rb', line 38

def save(config)
  ensure_directory_exists!

  # Ensure version is set
  config["version"] ||= 1
  config["templates"] ||= {}

  File.write(templates_file_path, YAML.dump(stringify_keys(config)))
end

#set_template(name, template) ⇒ Object

Add or update a template

Parameters:

  • Template name

  • Template configuration



78
79
80
81
82
83
# File 'lib/sxn/config/templates_config.rb', line 78

def set_template(name, template)
  config = load
  config["templates"] ||= {}
  config["templates"][name] = template
  save(config)
end