Class: HashTree::Set

Inherits:
Node
  • Object
show all
Defined in:
lib/hash_tree.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#children, #parent

Instance Method Summary collapse

Methods inherited from Node

#[], #aggregate, #ancestors, #detach, #dot, #dot?, #each, #key?, #keys, #parents, #postorder, #preorder, #root, #traverse, #values

Constructor Details

#initialize(parent, key, attach: true) ⇒ Set

Returns a new instance of Set.



206
207
208
# File 'lib/hash_tree.rb', line 206

def initialize(parent, key, attach: true)
  super(parent, @key = key, attach: attach)
end

Instance Attribute Details

#keyObject (readonly)

Key of this node. TODO: Make it possible/required to alias this method to provide an internal key



204
205
206
# File 'lib/hash_tree.rb', line 204

def key
  @key
end

Instance Method Details

#attach(child) ⇒ Object



210
# File 'lib/hash_tree.rb', line 210

def attach(child) do_attach(child.key, child) end

#node_attachObject



200
# File 'lib/hash_tree.rb', line 200

alias node_attach attach

#pathObject

Unique dot-separated list of keys leading from the root object to self. Note that the root object is not included in the path so that

obj.parent.nil? || obj.root.dot(obj.path) == obj

is always true

Note that for this to work, keys may not contain a dots (‘.’)



225
# File 'lib/hash_tree.rb', line 225

def path() @path ||= ancestors(true)[1..-1].map(&:key).join(".") end

#retach(node) ⇒ Object



212
213
214
215
# File 'lib/hash_tree.rb', line 212

def retach(node)
  node.parent.detach(node.key)
  attach(node)
end

#to_sObject

A set node is rendered as its key



228
# File 'lib/hash_tree.rb', line 228

def to_s() key.to_s end