Class: KeyValueTree::Hash

Inherits:
Object
  • Object
show all
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

Operations collapse

Helpers collapse

Instance Method Summary collapse

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.

Parameters:

  • method (Symbol)

    The called method

  • args (Array)

    Provided arguments (if any)



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

#storeObject (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

Parameters:

  • key (String)

    The key to fetch

Returns:



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.

Parameters:

  • key (String)

    The key to set

  • value (String)

    The value to set

Returns:

  • (String)

    The value



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

Parameters:

  • key (String)

    The key to delete

Returns:



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

Note:

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

Parameters:

  • key (String)

    The key to delete

Returns:

  • (nil)

    Undefined



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.

Parameters:

  • object (Hash)

    The object to import

Returns:

  • (self)


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

Parameters:

  • key (String, nil) (defaults to: nil)

    The subkey (if any)

Returns:

  • (Array<String>)

    An array of keys (starting with the root key)



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

Parameters:

  • key (String, nil) (defaults to: nil)

    The subkey (if any)

Returns:

  • (String)

    A dot separated String of keys (starting with the root key)



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

#keysArray<String>

Note:

This method might raise an exception if the store does not support the operation.

Return all keys.

Returns:

  • (Array<String>)

    An Array of 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)

Returns:

  • (Boolean)


88
89
90
# File 'lib/keyvaluetree/hash.rb', line 88

def root?
  @parent.nil?
end