Class: Jsus::Util::Tree::Node

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

Instance Method Summary collapse

Constructor Details

#initialize(full_path, value = nil) ⇒ Node

Initializes full path and value for the node

Parameters:

  • full_path (String)

    full path to node

  • value (Object) (defaults to: nil)


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_pathObject

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

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

#parentObject

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_componentsObject

Contains array with path components



57
58
59
# File 'lib/jsus/util/tree.rb', line 57

def path_components
  @path_components
end

#valueObject

Contains assigned value



53
54
55
# File 'lib/jsus/util/tree.rb', line 53

def value
  @value
end

Instance Method Details

#childrenArray

Returns node's direct descendants.

Returns:

  • (Array)

    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

Parameters:

  • name (String)

    node name

  • value (Object) (defaults to: nil)

Returns:



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.

Parameters:

  • name (String)

    basename

Returns:



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 -- plain node lookup by name

Parameters:

  • pathspec (String)

Returns:

  • (Array)

    array with search results



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.

Parameters:

  • name (String)
  • value (Object) (defaults to: nil)


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.

Returns:

  • (Boolean)

    whether this node has children



141
142
143
# File 'lib/jsus/util/tree.rb', line 141

def has_children?
  !children.empty?
end