Class: Matching::HashIndex
- Inherits:
-
Object
- Object
- Matching::HashIndex
- Defined in:
- lib/matching/hash_index.rb
Instance Attribute Summary collapse
-
#hashes ⇒ Object
readonly
Returns the value of attribute hashes.
Instance Method Summary collapse
-
#get(attr, val) ⇒ Object
Return an array of object ids for a given attribute and value.
-
#initialize ⇒ HashIndex
constructor
A new instance of HashIndex.
-
#put(attr, val, id) ⇒ Object
Add a value to the index for a given attribute and object id.
Constructor Details
#initialize ⇒ HashIndex
Returns a new instance of HashIndex.
6 7 8 9 |
# File 'lib/matching/hash_index.rb', line 6 def initialize #one hash for each attribute @hashes = {} end |
Instance Attribute Details
#hashes ⇒ Object (readonly)
Returns the value of attribute hashes.
4 5 6 |
# File 'lib/matching/hash_index.rb', line 4 def hashes @hashes end |
Instance Method Details
#get(attr, val) ⇒ Object
Return an array of object ids for a given attribute and value
20 21 22 |
# File 'lib/matching/hash_index.rb', line 20 def get(attr, val) (@hashes[attr] ? @hashes[attr][val] : nil) end |
#put(attr, val, id) ⇒ Object
Add a value to the index for a given attribute and object id
12 13 14 15 16 17 |
# File 'lib/matching/hash_index.rb', line 12 def put(attr, val, id) unless val.nil? h = @hashes[attr] || (@hashes[attr] = {}) (h[val] ? h[val] << id : h[val] = [id]) end end |