Class: KVDAG::KeyPathHashProxy
- Inherits:
-
Hash
- Object
- Hash
- KVDAG::KeyPathHashProxy
- Defined in:
- lib/kvdag/keypathhash.rb
Defined Under Namespace
Classes: KeyPath
Instance Method Summary collapse
-
#[](keypath) ⇒ Object
:call-seq: [](“key.path”) -> value or nil []([“key”, “path”]) -> value or nil.
- #[]=(keypath, value) ⇒ Object
-
#fetch(keypath) ⇒ Object
:call-seq: fetch(“key.path”) -> value or KeyError fetch([“key”, “path”]) -> value or KeyError.
-
#filter(*keypaths) ⇒ Object
:call-seq: filter(“key.path1”, …, “key.pathN”) -> KeyPathHashProxy.
-
#initialize(hash = {}) ⇒ KeyPathHashProxy
constructor
A new instance of KeyPathHashProxy.
- #merge(other, &block) ⇒ Object
- #merge!(other, &block) ⇒ Object
Constructor Details
#initialize(hash = {}) ⇒ KeyPathHashProxy
Returns a new instance of KeyPathHashProxy.
15 16 17 18 19 |
# File 'lib/kvdag/keypathhash.rb', line 15 def initialize(hash = {}) raise TypeError.new('Must be initialized with a `hash`') unless hash.is_a?(Hash) @hash = hash.deep_stringify_keys super(@hash) end |
Instance Method Details
#[](keypath) ⇒ Object
:call-seq:
[]("key.path") -> value or nil
[](["key", "path"]) -> value or nil
Return the value at a specified keypath. If the keypath does not specify a terminal value, return the remaining subtree instead.
Returns nil if the keypath is not found.
60 61 62 63 64 |
# File 'lib/kvdag/keypathhash.rb', line 60 def [](keypath) fetch(keypath) rescue nil end |
#[]=(keypath, value) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/kvdag/keypathhash.rb', line 66 def []=(keypath, value) *keypath, key = KeyPath.new(keypath) if keypath.empty? then hash = @hash else if not hash = self[keypath] then self[keypath] = hash = Hash.new end end hash[key] = value end |
#fetch(keypath) ⇒ Object
:call-seq:
fetch("key.path") -> value or KeyError
fetch(["key", "path"]) -> value or KeyError
Return the value at a specified keypath. If the keypath does not specify a terminal value, return the remaining subtree instead.
Raises a KeyError exception if the keypath is not found.
40 41 42 43 44 45 46 47 48 |
# File 'lib/kvdag/keypathhash.rb', line 40 def fetch(keypath) *keysubpath, key = KeyPath.new(keypath) hash = @hash keysubpath.each { |key| hash = hash.fetch(key) } hash.fetch(key) rescue KeyError raise KeyError.new("keypath not found: #{keypath.inspect}") end |
#filter(*keypaths) ⇒ Object
:call-seq:
filter("key.path1", ..., "key.pathN") -> KeyPathHashProxy
Filter a keypathhash tree by a list of keypath prefixes, and return a new keypathhash containing only those trees.
Raises a KeyError exception if any of the specified keypaths cannot be found.
89 90 91 92 93 94 95 |
# File 'lib/kvdag/keypathhash.rb', line 89 def filter(*keypaths) result = self.class.new keypaths.each do |keypath| result[keypath] = self.fetch(keypath) end result end |
#merge(other, &block) ⇒ Object
21 22 23 |
# File 'lib/kvdag/keypathhash.rb', line 21 def merge(other, &block) self.class.new(@hash.deep_merge(other.deep_stringify_keys, &block)) end |
#merge!(other, &block) ⇒ Object
25 26 27 28 |
# File 'lib/kvdag/keypathhash.rb', line 25 def merge!(other, &block) @hash.deep_merge!(other.deep_stringify_keys, &block) self end |