Class: WritersRoom::AliasResolver
- Inherits:
-
Object
- Object
- WritersRoom::AliasResolver
- Defined in:
- lib/writers_room/alias_resolver.rb
Overview
Cross-collection alias resolution. Searches multiple ElementCollections to resolve a name or alias.
Instance Method Summary collapse
-
#add_collection(collection) ⇒ Object
Add a collection to the resolver.
-
#find_all(query) ⇒ Object
Find all elements matching the query across all collections.
-
#initialize(collections) ⇒ AliasResolver
constructor
A new instance of AliasResolver.
-
#resolvable?(query) ⇒ Boolean
Check whether a query resolves unambiguously.
-
#resolve(query) ⇒ Object
Resolve a name/alias to a single Element.
Constructor Details
#initialize(collections) ⇒ AliasResolver
Returns a new instance of AliasResolver.
7 8 9 |
# File 'lib/writers_room/alias_resolver.rb', line 7 def initialize(collections) @collections = Array(collections) end |
Instance Method Details
#add_collection(collection) ⇒ Object
Add a collection to the resolver.
37 38 39 |
# File 'lib/writers_room/alias_resolver.rb', line 37 def add_collection(collection) @collections << collection end |
#find_all(query) ⇒ Object
Find all elements matching the query across all collections.
27 28 29 |
# File 'lib/writers_room/alias_resolver.rb', line 27 def find_all(query) @collections.flat_map { |col| col.search(query) }.uniq { |el| el.path } end |
#resolvable?(query) ⇒ Boolean
Check whether a query resolves unambiguously.
32 33 34 |
# File 'lib/writers_room/alias_resolver.rb', line 32 def resolvable?(query) find_all(query).length == 1 end |
#resolve(query) ⇒ Object
Resolve a name/alias to a single Element. Returns the element if unambiguous, raises if ambiguous or not found.
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/writers_room/alias_resolver.rb', line 13 def resolve(query) candidates = find_all(query) case candidates.length when 0 nil when 1 candidates.first else raise Error, "Ambiguous name '#{query}'. Matches: #{candidates.map(&:name).join(', ')}" end end |