Class: KeyValueTree::Hash
- Inherits:
-
Object
- Object
- KeyValueTree::Hash
- Defined in:
- lib/keyvaluetree/hash.rb
Overview
Hash implements an hierachical Hash (i.e. Hashes in Hashes) backed by a flat-hash Store.
Instance Attribute Summary collapse
-
#store ⇒ Object
readonly
Returns the value of attribute store.
Operations collapse
-
#[](key) ⇒ String, KeyValueTree::Hash
Return the value for the given key.
-
#[]=(key, value) ⇒ String
Set a value for the given key.
-
#delete(key) ⇒ String, KeyValueTree::Hash
Delete the value for the given key.
-
#delete_all(key) ⇒ nil
Delete the value for the given key and it’s sub-keys.
-
#import(object) ⇒ self
Import the given ::Hash into the Hash.
-
#keys ⇒ Array<String>
Return all keys.
-
#root? ⇒ Boolean
Return true if the Hash is the root Hash (i.e. has no parent).
Helpers collapse
-
#key_path(key = nil) ⇒ Array<String>
Return the keypath to self as Array of keys.
-
#key_path_string(key = nil) ⇒ String
Return the keypath to self as dot separated String of keys.
-
#method_missing(method, *args) ⇒ Object
Handle method dispatch for missing methods.
Instance Method Summary collapse
-
#initialize(store = KeyValueTree::MemoryStore.new(), key = nil, parent = nil) ⇒ Hash
constructor
A new instance of Hash.
Constructor Details
#initialize(store = KeyValueTree::MemoryStore.new(), key = nil, parent = nil) ⇒ Hash
Returns a new instance of Hash.
8 9 10 11 12 |
# File 'lib/keyvaluetree/hash.rb', line 8 def initialize(store = KeyValueTree::MemoryStore.new(), key=nil, parent = nil) @key = key.to_s @parent = parent @store = store end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
Handle method dispatch for missing methods. Provides the functionality to access a value using hash.key or set it via hash.key = value.
99 100 101 102 103 104 105 106 |
# File 'lib/keyvaluetree/hash.rb', line 99 def method_missing(method, *args) property = method.to_s if property =~ /=$/ return self[property.chop] = args[0] else return self[property] end end |
Instance Attribute Details
#store ⇒ Object (readonly)
Returns the value of attribute store.
6 7 8 |
# File 'lib/keyvaluetree/hash.rb', line 6 def store @store end |
Instance Method Details
#[](key) ⇒ String, KeyValueTree::Hash
Return the value for the given key. If the the key is nil return self. If the value is nil (empty) return a new instance of Hash for the key
19 20 21 22 23 24 |
# File 'lib/keyvaluetree/hash.rb', line 19 def [] (key) return self if key.nil? value = @store.key(key_path_string(key)) return value unless value.nil? return KeyValueTree::Hash.new(@store, key, self) end |
#[]=(key, value) ⇒ String
Set a value for the given key.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/keyvaluetree/hash.rb', line 30 def []= (key, value) if value.is_a?(::Hash) value.each do |hash_key, hash_value| self[key][hash_key] = hash_value end return end #if value.is_a?(Array) # value.each_with_index do |value, index| # self[key][index] = value # end # return #end @store.store(key_path_string(key), value) return value end |
#delete(key) ⇒ String, KeyValueTree::Hash
Delete the value for the given key
60 61 62 |
# File 'lib/keyvaluetree/hash.rb', line 60 def delete(key) @store.delete(key_path_string(key.to_s)) end |
#delete_all(key) ⇒ nil
This method might raise an exception if the store does not support the operation.
Delete the value for the given key and it’s sub-keys
68 69 70 |
# File 'lib/keyvaluetree/hash.rb', line 68 def delete_all(key) @store.delete_all(key_path_string(key.to_s)) end |
#import(object) ⇒ self
Import the given ::Hash into the Hash.
75 76 77 |
# File 'lib/keyvaluetree/hash.rb', line 75 def import(object) self[nil] = object end |
#key_path(key = nil) ⇒ Array<String>
Return the keypath to self as Array of keys. Append key is given
111 112 113 114 115 116 117 118 119 |
# File 'lib/keyvaluetree/hash.rb', line 111 def key_path(key = nil) if root? return [] if key.nil? return [key.to_s] else return (@parent.key_path + [@key]).compact if key.nil? return (@parent.key_path + [@key, key.to_s]).compact end end |
#key_path_string(key = nil) ⇒ String
Return the keypath to self as dot separated String of keys. Append key is given
124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/keyvaluetree/hash.rb', line 124 def key_path_string(key = nil) result = '' key_path(key).each_with_index do |value, index| # if value.is_a?(Integer) # result = result + "[#{value}]" # else result = result + '.' unless index == 0 result = result + value # end end return result end |
#keys ⇒ Array<String>
This method might raise an exception if the store does not support the operation.
Return all keys.
82 83 84 |
# File 'lib/keyvaluetree/hash.rb', line 82 def keys @store.keys_starting_with(key_path_string()).map { |each| each.split(".").first }.uniq end |
#root? ⇒ Boolean
Return true if the Hash is the root Hash (i.e. has no parent)
88 89 90 |
# File 'lib/keyvaluetree/hash.rb', line 88 def root? @parent.nil? end |