Class: Ace::Support::Items::Molecules::ShortcutResolver
- Inherits:
-
Object
- Object
- Ace::Support::Items::Molecules::ShortcutResolver
- Defined in:
- lib/ace/support/items/molecules/shortcut_resolver.rb
Overview
Resolves shortcut references to item ScanResult objects.
Shortcuts are the last N characters of an item ID. For ideas (6-char IDs): “8ppq7w” => shortcut “q7w” For tasks (9-char formatted IDs): “8pp.t.q7w” => shortcut “q7w” Full IDs are also accepted directly.
Warns (via callback or STDERR) when multiple matches are found.
Instance Method Summary collapse
-
#all_matches(ref) ⇒ Array<ScanResult>
Return all matches for a reference (useful for listing ambiguous matches).
-
#ambiguous?(ref) ⇒ Boolean
Check if a reference would be ambiguous.
-
#initialize(scan_results, full_id_length: 6) ⇒ ShortcutResolver
constructor
A new instance of ShortcutResolver.
-
#resolve(ref, on_ambiguity: nil) ⇒ ScanResult?
Resolve a reference to a single ScanResult.
Constructor Details
#initialize(scan_results, full_id_length: 6) ⇒ ShortcutResolver
Returns a new instance of ShortcutResolver.
18 19 20 21 |
# File 'lib/ace/support/items/molecules/shortcut_resolver.rb', line 18 def initialize(scan_results, full_id_length: 6) @scan_results = scan_results @full_id_length = full_id_length end |
Instance Method Details
#all_matches(ref) ⇒ Array<ScanResult>
Return all matches for a reference (useful for listing ambiguous matches)
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/ace/support/items/molecules/shortcut_resolver.rb', line 72 def all_matches(ref) return [] if ref.nil? || ref.empty? ref = ref.strip.downcase if ref.length == @full_id_length @scan_results.select { |r| r.id == ref } else @scan_results.select { |r| r.id.end_with?(ref) } end end |
#ambiguous?(ref) ⇒ Boolean
Check if a reference would be ambiguous
61 62 63 64 65 66 67 |
# File 'lib/ace/support/items/molecules/shortcut_resolver.rb', line 61 def ambiguous?(ref) return false if ref.nil? || ref.length == @full_id_length ref = ref.strip.downcase matches = @scan_results.select { |r| r.id.end_with?(ref) } matches.size > 1 end |
#resolve(ref, on_ambiguity: nil) ⇒ ScanResult?
Resolve a reference to a single ScanResult
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ace/support/items/molecules/shortcut_resolver.rb', line 27 def resolve(ref, on_ambiguity: nil) return nil if ref.nil? || ref.empty? ref = ref.strip.downcase if ref.length == @full_id_length # Full ID match exact = @scan_results.find { |r| r.id == ref } return exact end # Suffix match (last N characters) matches = @scan_results.select { |r| r.id.end_with?(ref) } if matches.empty? nil elsif matches.size == 1 matches.first else # Ambiguity: multiple matches if on_ambiguity on_ambiguity.call(matches) else warn "Warning: Ambiguous shortcut '#{ref}' matches #{matches.size} items: " \ "#{matches.map(&:id).join(", ")}. Using most recent." end # Return most recent (last by sorted ID = chronologically latest) matches.last end end |