Class: HashTree
- Inherits:
-
Object
- Object
- HashTree
- Defined in:
- lib/hashtree.rb,
lib/hashtree/version.rb
Constant Summary collapse
- VERSION =
"0.0.9"
Instance Attribute Summary collapse
-
#branches ⇒ Object
readonly
Returns the value of attribute branches.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#reading ⇒ Object
readonly
Returns the value of attribute reading.
-
#settings ⇒ Object
readonly
Returns the value of attribute settings.
-
#writing ⇒ Object
readonly
Returns the value of attribute writing.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #branch(key) ⇒ Object
- #clear ⇒ Object
- #count ⇒ Object
- #delete(key) ⇒ Object
- #has_key?(key) ⇒ Boolean
-
#initialize(settings = {}) ⇒ HashTree
constructor
A new instance of HashTree.
- #select(&block) ⇒ Object
Constructor Details
#initialize(settings = {}) ⇒ HashTree
Returns a new instance of HashTree.
11 12 13 14 15 16 17 18 |
# File 'lib/hashtree.rb', line 11 def initialize(settings = {}) @settings = { max_branch_size: 16 }.merge(settings) @root = {} @branches = {} @free = {} end |
Instance Attribute Details
#branches ⇒ Object (readonly)
Returns the value of attribute branches.
9 10 11 |
# File 'lib/hashtree.rb', line 9 def branches @branches end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
9 10 11 |
# File 'lib/hashtree.rb', line 9 def index @index end |
#reading ⇒ Object (readonly)
Returns the value of attribute reading.
9 10 11 |
# File 'lib/hashtree.rb', line 9 def reading @reading end |
#settings ⇒ Object (readonly)
Returns the value of attribute settings.
9 10 11 |
# File 'lib/hashtree.rb', line 9 def settings @settings end |
#writing ⇒ Object (readonly)
Returns the value of attribute writing.
9 10 11 |
# File 'lib/hashtree.rb', line 9 def writing @writing end |
Instance Method Details
#[](key) ⇒ Object
20 21 22 23 |
# File 'lib/hashtree.rb', line 20 def [](key) id = @root[key] id ? read(id) : nil end |
#[]=(key, value) ⇒ Object
25 26 27 28 |
# File 'lib/hashtree.rb', line 25 def []=(key, value) id = @root[key] id ? update(id, value) : create(key, value) end |
#branch(key) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/hashtree.rb', line 61 def branch(key) id = @root[key] if id branch_id = id[0..31] @branches[branch_id] else nil end end |
#clear ⇒ Object
48 49 50 51 |
# File 'lib/hashtree.rb', line 48 def clear @root = {} @branches = {} end |
#count ⇒ Object
53 54 55 |
# File 'lib/hashtree.rb', line 53 def count @root.count end |
#delete(key) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/hashtree.rb', line 30 def delete(key) id = @root[key] if id doc = read(id) destroy(id) @root.delete(key) return doc else nil end end |
#has_key?(key) ⇒ Boolean
57 58 59 |
# File 'lib/hashtree.rb', line 57 def has_key?(key) @root.has_key?(key) end |
#select(&block) ⇒ Object
42 43 44 45 46 |
# File 'lib/hashtree.rb', line 42 def select(&block) @root.select do |key, id| yield(key, read(id)) end end |