Class: Sxn::Config::TemplatesConfig
- Inherits:
-
Object
- Object
- Sxn::Config::TemplatesConfig
- 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
-
#sxn_path ⇒ Object
readonly
Returns the value of attribute sxn_path.
-
#templates_file_path ⇒ Object
readonly
Returns the value of attribute templates_file_path.
Instance Method Summary collapse
-
#exists? ⇒ Boolean
Check if templates file exists.
-
#get_template(name) ⇒ Hash?
Get a specific template by name.
-
#initialize(sxn_path) ⇒ TemplatesConfig
constructor
A new instance of TemplatesConfig.
-
#list_template_names ⇒ Array<String>
List all template names.
-
#load ⇒ Hash
Load templates configuration from file.
-
#remove_template(name) ⇒ Boolean
Remove a template.
-
#save(config) ⇒ Object
Save templates configuration to file.
-
#set_template(name, template) ⇒ Object
Add or update a template.
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_path ⇒ Object (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_path ⇒ Object (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
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
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_names ⇒ Array<String>
List all 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 |
#load ⇒ Hash
Load templates configuration from file
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
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
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
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 |