Class: Ergane::PathRegistry

Inherits:
Object show all
Defined in:
lib/ergane/path_registry.rb

Overview

An ordered registry of path-prefix → label substitutions, used by Command#abbreviate_path. Ships with $HOME → “~”; consumers register their own, e.g.:

Ergane.paths.register("~/Workspace", "@ws")

When abbreviating, the longest matching prefix wins, and a prefix only matches at a path boundary so “/home/user” never clips “/home/username”.

Defined Under Namespace

Classes: Substitution

Instance Method Summary collapse

Constructor Details

#initializePathRegistry

Returns a new instance of PathRegistry.



15
16
17
# File 'lib/ergane/path_registry.rb', line 15

def initialize
  @substitutions = []
end

Instance Method Details

#abbreviate(path) ⇒ Object

Collapse the longest matching prefix in path to its label, returning the path unchanged when nothing matches.



37
38
39
40
41
42
43
44
45
# File 'lib/ergane/path_registry.rb', line 37

def abbreviate(path)
  str = path.to_s
  best = @substitutions
    .select { |sub| str == sub.prefix || str.start_with?("#{sub.prefix}/") }
    .max_by { |sub| sub.prefix.length }
  return str unless best

  "#{best.label}#{str[best.prefix.length..]}"
end

#clearObject

Remove every registered substitution. Returns self for chaining.



30
31
32
33
# File 'lib/ergane/path_registry.rb', line 30

def clear
  @substitutions.clear
  self
end

#register(prefix, label) ⇒ Object

Register a prefix to collapse to label. The prefix is expanded (so “~” and relative paths resolve), and re-registering a prefix replaces its previous label. Returns self for chaining.



22
23
24
25
26
27
# File 'lib/ergane/path_registry.rb', line 22

def register(prefix, label)
  expanded = File.expand_path(prefix.to_s)
  @substitutions.reject! { |sub| sub.prefix == expanded }
  @substitutions << Substitution.new(expanded, label.to_s)
  self
end