Class: Ace::Tmux::Molecules::PresetLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/tmux/molecules/preset_loader.rb

Overview

Finds and loads YAML presets across the ACE config cascade

Uses VirtualConfigResolver to discover presets from:

1. Project .ace/tmux/ (highest priority)
2. User ~/.ace/tmux/
3. Gem .ace-defaults/tmux/ (lowest priority)

Constant Summary collapse

PRESET_TYPES =
%w[sessions windows panes].freeze

Instance Method Summary collapse

Constructor Details

#initialize(gem_root:, start_path: nil) ⇒ PresetLoader

Returns a new instance of PresetLoader.

Parameters:

  • gem_root (String)

    Gem root directory for defaults

  • start_path (String, nil) (defaults to: nil)

    Starting path for cascade traversal



20
21
22
23
24
25
26
27
# File 'lib/ace/tmux/molecules/preset_loader.rb', line 20

def initialize(gem_root:, start_path: nil)
  @resolver = Ace::Support::Config.virtual_resolver(
    config_dir: ".ace",
    defaults_dir: ".ace-defaults",
    start_path: start_path,
    gem_path: gem_root
  )
end

Instance Method Details

#list(type) ⇒ Array<String>

List available presets for a type

Parameters:

  • type (String)

    Preset type: “sessions”, “windows”, or “panes”

Returns:

  • (Array<String>)

    Preset names (without .yml extension)



46
47
48
49
50
51
# File 'lib/ace/tmux/molecules/preset_loader.rb', line 46

def list(type)
  pattern = "tmux/#{type}/*.yml"
  @resolver.glob(pattern).keys.map do |relative_path|
    File.basename(relative_path, ".yml")
  end.sort.uniq
end

#list_allHash<String, Array<String>>

List all preset types and their presets

Returns:

  • (Hash<String, Array<String>>)

    Map of type => preset names



56
57
58
59
60
61
# File 'lib/ace/tmux/molecules/preset_loader.rb', line 56

def list_all
  PRESET_TYPES.each_with_object({}) do |type, result|
    presets = list(type)
    result[type] = presets unless presets.empty?
  end
end

#load(type, name) ⇒ Hash?

Load a preset by type and name

Parameters:

  • type (String)

    Preset type: “sessions”, “windows”, or “panes”

  • name (String)

    Preset name (without .yml extension)

Returns:

  • (Hash, nil)

    Parsed YAML hash, or nil if not found



34
35
36
37
38
39
40
# File 'lib/ace/tmux/molecules/preset_loader.rb', line 34

def load(type, name)
  relative_path = "tmux/#{type}/#{name}.yml"
  absolute_path = @resolver.resolve_path(relative_path)
  return nil unless absolute_path && File.exist?(absolute_path)

  YAML.safe_load_file(absolute_path, permitted_classes: [Date], aliases: true) || {}
end

#to_lookup(type) ⇒ Proc

Create a lookup proc for PresetResolver

Parameters:

  • type (String)

    Preset type to look up

Returns:

  • (Proc)

    Proc that takes a name and returns a preset hash



67
68
69
# File 'lib/ace/tmux/molecules/preset_loader.rb', line 67

def to_lookup(type)
  ->(name) { load(type, name) }
end