Class: Coradoc::Reference::Catalog::MemoryIndex
- Inherits:
-
Object
- Object
- Coradoc::Reference::Catalog::MemoryIndex
- Includes:
- Enumerable
- Defined in:
- lib/coradoc/reference/catalog/memory_index.rb
Overview
Shared in-memory index. Built-in catalogs compose this rather than reimplementing the storage layer (DRY). The index is immutable from the outside — entries are added by the catalog during construction, then read-only.
Instance Method Summary collapse
- #add(address, content) ⇒ Object
- #ambiguous?(address) ⇒ Boolean
- #each_pair ⇒ Object
-
#initialize ⇒ MemoryIndex
constructor
A new instance of MemoryIndex.
- #lookup(address) ⇒ Object
- #recognizes_scheme?(scheme) ⇒ Boolean
- #size ⇒ Object
Constructor Details
#initialize ⇒ MemoryIndex
Returns a new instance of MemoryIndex.
13 14 15 16 |
# File 'lib/coradoc/reference/catalog/memory_index.rb', line 13 def initialize @by_address = {} @schemes = Set.new end |
Instance Method Details
#add(address, content) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/coradoc/reference/catalog/memory_index.rb', line 18 def add(address, content) raise ArgumentError, 'address required' unless address.is_a?(Coradoc::Reference::Address) raise ArgumentError, 'content required' unless content @by_address[address] = Array(@by_address[address]) << content @schemes << address.scheme.to_sym self end |
#ambiguous?(address) ⇒ Boolean
36 37 38 39 |
# File 'lib/coradoc/reference/catalog/memory_index.rb', line 36 def ambiguous?(address) entries = @by_address[address] entries && entries.size > 1 end |
#each_pair ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/coradoc/reference/catalog/memory_index.rb', line 41 def each_pair return to_enum(:each_pair) unless block_given? @by_address.each do |address, contents| contents.each { |c| yield address, c } end end |
#lookup(address) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/coradoc/reference/catalog/memory_index.rb', line 27 def lookup(address) entries = @by_address[address] return nil unless entries return entries.first if entries.size == 1 # Copy: callers must not be able to corrupt the index. entries.dup end |
#recognizes_scheme?(scheme) ⇒ Boolean
49 50 51 |
# File 'lib/coradoc/reference/catalog/memory_index.rb', line 49 def recognizes_scheme?(scheme) @schemes.include?(scheme.to_sym) end |
#size ⇒ Object
53 54 55 |
# File 'lib/coradoc/reference/catalog/memory_index.rb', line 53 def size @by_address.size end |