class KeyValueTree::Hash

Hash implements an hierachical Hash (i.e. Hashes in Hashes) backed by a flat-hash Store.

Attributes

store[R]

Public Class Methods

new(store = KeyValueTree::MemoryStore.new(), key=nil, parent = nil) click to toggle source
# 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

Public Instance Methods

[](key) click to toggle source

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 @param [String]key The key to fetch @return [String,KeyValueTree::Hash] The result

# 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) click to toggle source

Set a value for the given key. @param [String]key The key to set @param [String]value The value to set @return [String] The value

# 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) click to toggle source

Delete the value for the given key @param [String]key The key to delete @return [String,KeyValueTree::Hash] The value of the deleted key

# File lib/keyvaluetree/hash.rb, line 60
def delete(key)
  @store.delete(key_path_string(key.to_s))
end
delete_all(key) click to toggle source

Delete the value for the given key and it's sub-keys @note This method might raise an exception if the store does not support the operation. @param [String]key The key to delete @return [nil] Undefined

# File lib/keyvaluetree/hash.rb, line 68
def delete_all(key)
  @store.delete_all(key_path_string(key.to_s))
end
import(object) click to toggle source

Import the given ::Hash into the Hash. @param [Hash]object The object to import @return [self]

# File lib/keyvaluetree/hash.rb, line 75
def import(object)
  self[nil] = object
end
key_path(key = nil) click to toggle source

Return the keypath to self as Array of keys. Append key is given @param [String,nil]key The subkey (if any) @return [Array<String>] An array of keys (starting with the root key)

# 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) click to toggle source

Return the keypath to self as dot separated String of keys. Append key is given @param [String,nil]key The subkey (if any) @return [String] A dot separated String of keys (starting with the root key)

# 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() click to toggle source

Return all keys. @note This method might raise an exception if the store does not support the operation. @return [Array<String>] An Array of keys

# File lib/keyvaluetree/hash.rb, line 82
def keys
  @store.keys_starting_with(key_path_string()).map { |each| each.split(".").first }.uniq
end
method_missing(method, *args) click to toggle source

Handle method dispatch for missing methods. Provides the functionality to access a value using hash.key or set it via hash.key = value. @param [Symbol]method The called method @param [Array]args Provided arguments (if any)

# 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
root?() click to toggle source

Return true if the Hash is the root Hash (i.e. has no parent) @return [Boolean]

# File lib/keyvaluetree/hash.rb, line 88
def root?
  @parent.nil?
end