Module: KVDAG::AttributeNode
Overview
Mixin with common methods for managing the attrs
of vertices and edges in a KVDAG.
Instance Attribute Summary collapse
-
#attrs ⇒ Object
readonly
Returns the value of attribute attrs.
Instance Method Summary collapse
-
#[](attr, options = {}) ⇒ Object
Return the value for an
attr
, ornil
if theattr
can’t be found. -
#[]=(attr, value) ⇒ Object
Set the
value
for anattr
in this attrnode. -
#fetch(attr, options = {}) ⇒ Object
Returns the value for an
attr
. -
#filter(*keys) ⇒ Object
Filter the key-value view of a vertex by a list of key prefixes, and return a hash_proxy containing only those trees.
-
#match?(filter = {}) ⇒ Boolean
:call-seq: match?() -> true match?(method: match, …) -> true or false match?(one?:{ matches }, …) -> true or false match?(any?:{ matches }, …) -> true or false match?(all?:{ matches }, …) -> true of false match?(none?:{ matches }, …) -> true or false.
- #merge!(other) ⇒ Object
- #to_hash ⇒ Object
Instance Attribute Details
#attrs ⇒ Object (readonly)
Returns the value of attribute attrs.
6 7 8 |
# File 'lib/kvdag/attrnode.rb', line 6 def attrs @attrs end |
Instance Method Details
#[](attr, options = {}) ⇒ Object
Return the value for an attr
, or nil
if the attr
can’t be found.
options
:
shallow
-
If true, lookup is limited to attrs defined in this attrnode. The default is lookup in the tree returned by
to_hash_proxy
.
34 35 36 37 38 |
# File 'lib/kvdag/attrnode.rb', line 34 def [](attr, = {}) fetch(attr, ) rescue nil end |
#[]=(attr, value) ⇒ Object
Set the value
for an attr
in this attrnode.
42 43 44 |
# File 'lib/kvdag/attrnode.rb', line 42 def []=(attr, value) @attrs[attr] = value end |
#fetch(attr, options = {}) ⇒ Object
Returns the value for an attr
. If the attr
can’t be found, it will raise a KeyError exception.
options
:
shallow
-
If true, lookup is limited to attrs defined in this attrnode. The default is lookup in the tree returned by
to_hash_proxy
.
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/kvdag/attrnode.rb', line 16 def fetch(attr, = {}) case when ([:shallow]) @attrs.fetch(attr) when self.respond_to?(:to_hash_proxy) to_hash_proxy.fetch(attr) else to_hash.fetch(attr) end end |
#filter(*keys) ⇒ Object
Filter the key-value view of a vertex by a list of key prefixes, and return a hash_proxy containing only those trees.
:call-seq:
filter("key.path1", ..., "key.pathN") -> hash_proxy
65 66 67 68 69 70 71 |
# File 'lib/kvdag/attrnode.rb', line 65 def filter(*keys) if self.respond_to?(:to_hash_proxy) then to_hash_proxy.filter(*keys) else raise NotImplementedError.new('not implemented for plain hash') end end |
#match?(filter = {}) ⇒ Boolean
:call-seq:
match?() -> true
match?(method: match, ...) -> true or false
match?(one?:{ matches }, ...) -> true or false
match?(any?:{ matches }, ...) -> true or false
match?(all?:{ matches }, ...) -> true of false
match?(none?:{ matches }, ...) -> true or false
Checks if the key-value view visible from a node matches all of set of filters. An empty filter set is considered a match.
Any method
given will be matched against its result:
match === self.send(method)
matches
should be a hash with ‘key.path’ strings at keys, and match
values to check for equality:
match === self[key]
Examples:
node.match?(class:KVDAG::Vertex)
node.match?(none?:{'key' => Integer})
node.match?(all?:{'key' => /this|that/})
node.match?(any?:{'key1' => 'this', 'key2' => 'that'})
node.match?(one?:{'key1' => 'this', 'key2' => 'that'})
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/kvdag/attrnode.rb', line 99 def match?(filter = {}) valid_enumerators = [:none?, :one?, :any?, :all?] filter.all? do |item| method, match = item if valid_enumerators.include?(method) match.send(method) do |match_item| key, value = match_item value === self[key] end else match === self.send(method) end end end |
#merge!(other) ⇒ Object
54 55 56 57 |
# File 'lib/kvdag/attrnode.rb', line 54 def merge!(other) @attrs.merge!(other.to_hash) self end |
#to_hash ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/kvdag/attrnode.rb', line 46 def to_hash if self.respond_to?(:to_hash_proxy) then to_hash_proxy.to_hash else @attrs end end |