Class: OsCtl::Lib::Index
- Inherits:
-
Object
- Object
- OsCtl::Lib::Index
- Defined in:
- lib/libosctl/index.rb
Overview
Hash-based cache for collection lookups by key
Index is not thread-safe.
Instance Method Summary collapse
-
#<<(obj) ⇒ Object
Add object to the index.
-
#[](key) ⇒ Object
Lookup using string key.
-
#delete(obj) ⇒ Object
Delete object from the index.
-
#delete_key(key) ⇒ Object
Delete object from the index by key.
- #empty? ⇒ Boolean
-
#initialize {|obj| ... } ⇒ Index
constructor
A new instance of Index.
- #key(obj) ⇒ Object protected
Constructor Details
#initialize {|obj| ... } ⇒ Index
Returns a new instance of Index.
8 9 10 11 |
# File 'lib/libosctl/index.rb', line 8 def initialize(&block) @block = block @index = {} end |
Instance Method Details
#<<(obj) ⇒ Object
Add object to the index
15 16 17 18 |
# File 'lib/libosctl/index.rb', line 15 def <<(obj) @index[key(obj)] = obj self end |
#[](key) ⇒ Object
Lookup using string key
22 23 24 |
# File 'lib/libosctl/index.rb', line 22 def [](key) @index[key] end |
#delete(obj) ⇒ Object
Delete object from the index
28 29 30 |
# File 'lib/libosctl/index.rb', line 28 def delete(obj) @index.delete(key(obj)) end |
#delete_key(key) ⇒ Object
Delete object from the index by key
34 35 36 |
# File 'lib/libosctl/index.rb', line 34 def delete_key(key) @index.delete(key) end |
#empty? ⇒ Boolean
38 39 40 |
# File 'lib/libosctl/index.rb', line 38 def empty? @index.empty? end |
#key(obj) ⇒ Object (protected)
44 45 46 |
# File 'lib/libosctl/index.rb', line 44 def key(obj) @block.call(obj) end |