Class: TermUtils::PropertyTreeNode

Inherits:
Object
  • Object
show all
Defined in:
lib/term_utils/property_tree_node.rb

Overview

Represents a general-purpose tree node that holds a key-value pair.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, &block) ⇒ PropertyTreeNode

Creates a new PropertyTreeNode. option opts [Object] :key option opts [Object] :value

Parameters:

  • opts (Hash) (defaults to: {})


35
36
37
38
39
40
41
# File 'lib/term_utils/property_tree_node.rb', line 35

def initialize(opts = {}, &block)
  @parent_node = nil
  @child_nodes = nil
  @key = opts.fetch(:key, nil)
  @value = opts.fetch(:value, nil)
  block.call(self) if block
end

Instance Attribute Details

#child_nodesArray<PropertyTreeNode>

Returns:



25
26
27
# File 'lib/term_utils/property_tree_node.rb', line 25

def child_nodes
  @child_nodes
end

#keyObject

Returns:

  • (Object)


27
28
29
# File 'lib/term_utils/property_tree_node.rb', line 27

def key
  @key
end

#parent_nodePropertyTreeNode

Returns:



23
24
25
# File 'lib/term_utils/property_tree_node.rb', line 23

def parent_node
  @parent_node
end

#valueObject

Returns:

  • (Object)


29
30
31
# File 'lib/term_utils/property_tree_node.rb', line 29

def value
  @value
end

Instance Method Details

#child_node(key) ⇒ PropertyTreeNode?

Returns the child node identified by a given key.

Parameters:

  • key (Object)

Returns:



72
73
74
75
76
# File 'lib/term_utils/property_tree_node.rb', line 72

def child_node(key)
  if @child_nodes
    @child_nodes.find { |n| n.key == key }
  end
end

#collect_nodes(opts = {}) ⇒ Array<PropertyTreeNode>

Collects nodes.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :path (Array)
  • :leaf_only (Boolean)

Returns:



155
156
157
158
159
160
161
# File 'lib/term_utils/property_tree_node.rb', line 155

def collect_nodes(opts = {})
  nodes = []
  each_node(opts) do |n|
    nodes << n
  end
  nodes
end

#collect_paths(opts = {}) ⇒ Array<Array<Object>>

Collects node paths.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :path (Array)
  • :leaf_only (Boolean)

Returns:

  • (Array<Array<Object>>)


168
169
170
171
172
173
174
# File 'lib/term_utils/property_tree_node.rb', line 168

def collect_paths(opts = {})
  paths = []
  each_node(opts) do |n|
    paths << n.path
  end
  paths
end

#collect_values(opts = {}) ⇒ Array<Object>

Collect node values.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :path (Array)
  • :leaf_only (Boolean)

Returns:

  • (Array<Object>)


181
182
183
184
185
186
187
# File 'lib/term_utils/property_tree_node.rb', line 181

def collect_values(opts = {})
  vals = []
  each_node(opts) do |n|
    vals << n.value if n.value
  end
  vals
end

#define_node(opts = {}, &block) ⇒ PropertyTreeNode

Creates a new node and adds it as a child.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :key (Object)
  • :value (Object)

Returns:



83
84
85
86
87
88
89
90
# File 'lib/term_utils/property_tree_node.rb', line 83

def define_node(opts = {}, &block)
  new_node = TermUtils::PropertyTreeNode.new(opts)
  new_node.parent_node = self
  @child_nodes = [] unless @child_nodes
  @child_nodes << new_node
  block.call(new_node) if block
  new_node
end

#each_node(opts = {}, &block) ⇒ nil

Iterates over every node.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :path (Array)
  • :leaf_only (Boolean)

Returns:

  • (nil)


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/term_utils/property_tree_node.rb', line 105

def each_node(opts = {}, &block)
  rpath = nil
  if opts.key? :path
    rpath = opts[:path].dup
  end
  dive = true
  if @key
    hide = false
    if rpath
      if rpath.shift == @key
        unless rpath.empty?
          hide = true
        end
      else
        dive = false
        hide = true
      end
    end
    unless hide || (opts[:leaf_only] && @child_nodes)
      if opts.key? :block
        opts[:block].call(self)
      elsif block
        block.call(self)
      end
    end
  end # if @key
  if dive && @child_nodes
    ropts = opts.dup
    if rpath
      if rpath.empty?
        ropts.delete :path
      else
        ropts[:path] = rpath
      end
    end
    if block
      ropts[:block] = block
    end
    @child_nodes.each do |n|
      n.each_node(ropts)
    end
  end
  nil
end

#eval_child_count(path) ⇒ Integer

Evaluates the total number of nodes in the tree represented by this one.

Parameters:

  • path (Array<Object>)

Returns:

  • (Integer)


211
212
213
214
215
216
# File 'lib/term_utils/property_tree_node.rb', line 211

def eval_child_count(path)
  node = find_node(path)
  if node
    node.child_nodes ? node.child_nodes.length : 0
  end
end

#find_node(path) ⇒ PropertyTreeNode

Finds the node identified by a given path of keys.

Parameters:

  • path (Array<Object>)

Returns:



192
193
194
195
196
197
198
199
# File 'lib/term_utils/property_tree_node.rb', line 192

def find_node(path)
  catch :found do
    each_node(path: path) do |n| # rubocop:disable Lint/UnreachableLoop
      throw :found, n
    end
    nil
  end
end

#find_node_value(path) ⇒ Object

Finds the node identified by a given path of keys and returns its value.

Parameters:

  • path (Array<Object>)

Returns:

  • (Object)


221
222
223
224
225
226
# File 'lib/term_utils/property_tree_node.rb', line 221

def find_node_value(path)
  node = find_node(path)
  if node
    node.value
  end
end

#head?Boolean

Tests whether this one is the head of the tree (i.e. has no parent).

Returns:

  • (Boolean)


59
60
61
# File 'lib/term_utils/property_tree_node.rb', line 59

def head?
  @parent_node == nil
end

#initialize_dup(other) ⇒ Object

For dup method.



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/term_utils/property_tree_node.rb', line 44

def initialize_dup(other)
  @parent_node = nil
  if other.child_nodes
    @child_nodes = []
    other.child_nodes.each do |n|
      new_node = n.dup
      new_node.parent_node = self
      @child_nodes << new_node
    end
  end
  super
end

#leaf?Boolean

Tests whether this one is a leaf of the tree (i.e. has no child).

Returns:

  • (Boolean)


65
66
67
# File 'lib/term_utils/property_tree_node.rb', line 65

def leaf?
  @child_nodes == nil
end

#node_exists?(path) ⇒ Boolean

Tests whether the node identified by a given path of keys exists.

Parameters:

  • path (Array<Object>)

Returns:

  • (Boolean)


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

def node_exists?(path)
  find_node(path) != nil
end

#pathArray<Object>

Builds the path of keys.

Returns:

  • (Array<Object>)


94
95
96
97
98
# File 'lib/term_utils/property_tree_node.rb', line 94

def path
  p = @parent_node ? @parent_node.path : []
  p << @key if @key
  p
end