Class: Ergane::PathRegistry
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
-
#abbreviate(path) ⇒ Object
Collapse the longest matching prefix in
pathto its label, returning the path unchanged when nothing matches. -
#clear ⇒ Object
Remove every registered substitution.
-
#initialize ⇒ PathRegistry
constructor
A new instance of PathRegistry.
-
#register(prefix, label) ⇒ Object
Register a
prefixto collapse tolabel.
Constructor Details
#initialize ⇒ PathRegistry
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. The input is expanded before matching (mirroring how prefixes are stored on #register), so matching is consistent across platforms and “~”-relative input is accepted.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ergane/path_registry.rb', line 39 def abbreviate(path) original = path.to_s = File.(original) best = @substitutions .select { |sub| == sub.prefix || .start_with?("#{sub.prefix}/") } .max_by { |sub| sub.prefix.length } return original unless best "#{best.label}#{[best.prefix.length..]}" end |
#clear ⇒ Object
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) = File.(prefix.to_s) @substitutions.reject! { |sub| sub.prefix == } @substitutions << Substitution.new(, label.to_s) self end |