Class: Jsus::Util::Tree::Node
- Inherits:
-
Object
- Object
- Jsus::Util::Tree::Node
- Defined in:
- lib/jsus/util/tree.rb
Overview
Jsus::Tree node class. Most of the time you only need to extract #value from the node, although sometimes you might need to refer to #parent node and #full_path
Instance Attribute Summary collapse
-
#full_path ⇒ Object
Contains full path to the node, such as '/hello/world'.
-
#name ⇒ Object
readonly
Contains node basename, such as 'world' for '/hello/world'.
-
#parent ⇒ Object
Contains reference to parent node, nil for root node.
-
#path_components ⇒ Object
Contains array with path components.
-
#value ⇒ Object
Contains assigned value.
Instance Method Summary collapse
-
#children ⇒ Array
Node's direct descendants.
-
#create_child(name, value = nil) ⇒ Jsus::Util::Tree::Node
Creates a child with given name and value.
-
#find_child(name) ⇒ Jsus::Util::Tree::Node
Direct node child with given basename.
-
#find_children_matching(pathspec) ⇒ Array
Finds children matching the given pathspec Pathspec format: '*' -- this node and all the children nodes that have children 'smth' -- nodes beginning with smth 'smth*else' -- nodes beginning with smth and ending with else
-- plain node lookup by name. -
#find_or_create_child(name, value = nil) ⇒ Object
Finds a child with given name or creates a child with given name and value.
-
#has_children? ⇒ Boolean
Whether this node has children.
-
#initialize(full_path, value = nil) ⇒ Node
constructor
Initializes full path and value for the node.
Constructor Details
#initialize(full_path, value = nil) ⇒ Node
Initializes full path and value for the node
63 64 65 66 |
# File 'lib/jsus/util/tree.rb', line 63 def initialize(full_path, value = nil) self.full_path = full_path self.value = value end |
Instance Attribute Details
#full_path ⇒ Object
Contains full path to the node, such as '/hello/world'
69 70 71 |
# File 'lib/jsus/util/tree.rb', line 69 def full_path @full_path end |
#name ⇒ Object (readonly)
Contains node basename, such as 'world' for '/hello/world'
71 72 73 |
# File 'lib/jsus/util/tree.rb', line 71 def name @name end |
#parent ⇒ Object
Contains reference to parent node, nil for root node
55 56 57 |
# File 'lib/jsus/util/tree.rb', line 55 def parent @parent end |
#path_components ⇒ Object
Contains array with path components
57 58 59 |
# File 'lib/jsus/util/tree.rb', line 57 def path_components @path_components end |
#value ⇒ Object
Contains assigned value
53 54 55 |
# File 'lib/jsus/util/tree.rb', line 53 def value @value end |
Instance Method Details
#children ⇒ Array
Returns node's direct descendants.
83 84 85 |
# File 'lib/jsus/util/tree.rb', line 83 def children @children ||= [] end |
#create_child(name, value = nil) ⇒ Jsus::Util::Tree::Node
Creates a child with given name and value
99 100 101 102 103 104 105 |
# File 'lib/jsus/util/tree.rb', line 99 def create_child(name, value = nil) full_path = Tree.path_from_components(path_components + [name]) node = Node.new(full_path, value) children << node node.parent = self node end |
#find_child(name) ⇒ Jsus::Util::Tree::Node
Returns direct node child with given basename.
90 91 92 |
# File 'lib/jsus/util/tree.rb', line 90 def find_child(name) children.detect {|child| child.name == name } end |
#find_children_matching(pathspec) ⇒ Array
Finds children matching the given pathspec
Pathspec format:
'*' -- this node and all the children nodes that have children
'smth' -- nodes beginning with smth
'smth*else' -- nodes beginning with smth and ending with else
127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/jsus/util/tree.rb', line 127 def find_children_matching(pathspec) case pathspec when "**" [self] + children.select {|child| child.has_children? } when /\*/ regexp = Regexp.new("^" + Regexp.escape(pathspec).gsub("\\*", ".*") + "$") children.select {|child| !child.has_children? && child.name =~ regexp } else [find_child(pathspec)].compact end end |
#find_or_create_child(name, value = nil) ⇒ Object
Finds a child with given name or creates a child with given name and value.
113 114 115 |
# File 'lib/jsus/util/tree.rb', line 113 def find_or_create_child(name, value = nil) find_child(name) || create_child(name, value) end |
#has_children? ⇒ Boolean
Returns whether this node has children.
141 142 143 |
# File 'lib/jsus/util/tree.rb', line 141 def has_children? !children.empty? end |