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 node. option opts [Object] :key option opts [Object] :value

Parameters:

  • opts (Hash<Symbol, Object>) (defaults to: {})


31
32
33
34
35
36
37
# File 'lib/term_utils/property_tree_node.rb', line 31

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<TermUtils::PropertyTreeNode>

Returns:



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

def child_nodes
  @child_nodes
end

#keyObject

Returns:

  • (Object)


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

def key
  @key
end

#parent_nodeTermUtils::PropertyTreeNode



20
21
22
# File 'lib/term_utils/property_tree_node.rb', line 20

def parent_node
  @parent_node
end

#valueObject

Returns:

  • (Object)


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

def value
  @value
end

Instance Method Details

#child_node(key) ⇒ TermUtils::PropertyTreeNode?

Returns the child node identified by a given key.

Parameters:

  • key (Object)

Returns:



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

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

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

Collects nodes.

Parameters:

  • opts (Hash<Symbol,Object>) (defaults to: {})

    `:path`, `:leaf_only`

Returns:



137
138
139
140
141
142
143
# File 'lib/term_utils/property_tree_node.rb', line 137

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<Symbol,Object>) (defaults to: {})

    `:path`, `:leaf_only`

Returns:

  • (Array<Array<Object>>)


147
148
149
150
151
152
153
# File 'lib/term_utils/property_tree_node.rb', line 147

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<Symbol,Object>) (defaults to: {})

    `:path`, `:leaf_only`

Returns:

  • (Array<Object>)


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

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

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

Creates a new node and adds it as a child.

Parameters:

  • opts (Hash<Symbol,Object>) (defaults to: {})

    key value

Returns:



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

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<Symbol,Object>) (defaults to: {})

    `:path`, `:leaf_only`

Returns:

  • (nil)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
# File 'lib/term_utils/property_tree_node.rb', line 90

def each_node(opts = {}, &block)
  rpath = nil
  if opts.has_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 or (opts[:leaf_only] and @child_nodes)
      if opts.has_key? :block
        opts[:block].call(self)
      elsif block
        block.call(self)
      end
    end
  end # if @key
  if dive and @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)


184
185
186
187
188
189
# File 'lib/term_utils/property_tree_node.rb', line 184

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

#find_node(path) ⇒ TermUtils::PropertyTreeNode

Finds the node identified by a given path of keys.

Parameters:

  • path (Array<Object>)

Returns:



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

def find_node(path)
  catch :found do
    each_node(:path => path) do |n|
      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)


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

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)


53
54
55
# File 'lib/term_utils/property_tree_node.rb', line 53

def head?
  @parent_node == nil
end

#initialize_dup(other) ⇒ Object

For dup method.



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/term_utils/property_tree_node.rb', line 39

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)


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

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)


178
179
180
# File 'lib/term_utils/property_tree_node.rb', line 178

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

#pathArray<Object>

Builds the path of keys.

Returns:

  • (Array<Object>)


82
83
84
85
86
# File 'lib/term_utils/property_tree_node.rb', line 82

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