Class: Forester::TreeNode
Constant Summary
Constants included
from Iterators
Iterators::TRAVERSAL_MODES
Instance Method Summary
collapse
#as_root_hash
Methods included from Validators
#validate_uniqueness_of_field, #validate_uniqueness_of_fields, #validate_uniqueness_of_fields_combination
Methods included from Mutators
#add_child_content, #add_field_in_node, #add_field_in_subtree, #add_fields_in_node, #add_fields_in_subtree, #change_parent_to, #delete_values_in_node, #delete_values_in_subtree, #remove_levels_past
Methods included from Iterators
#each_content, #each_level, #each_node
Instance Method Details
#get(field, default = :raise) ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/forester/tree_node.rb', line 67
def get(field, default = :raise)
if has_field?(field)
content[field]
elsif block_given?
yield(field, self)
elsif default != :raise
default
else
missing_key =
if field.is_a?(Symbol)
":#{field}"
elsif field.is_a?(String)
"'#{field}'"
else
field
end
error_message = "key not found: #{missing_key} in node content \"#{content}\""
raise KeyError, error_message
end
end
|
#has_field?(field) ⇒ Boolean
88
89
90
|
# File 'lib/forester/tree_node.rb', line 88
def has_field?(field)
content.key?(field)
end
|
#leaf? ⇒ Boolean
32
33
34
|
# File 'lib/forester/tree_node.rb', line 32
def leaf?
is_leaf?
end
|
#leaves ⇒ Object
36
37
38
|
# File 'lib/forester/tree_node.rb', line 36
def leaves
each_leaf
end
|
#leaves_when_pruned_to_depth(d) ⇒ Object
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/forester/tree_node.rb', line 44
def leaves_when_pruned_to_depth(d)
ret = []
each_node(traversal: :breadth_first) do |node|
relative_depth_of_descendant = node.node_depth - node_depth
break if relative_depth_of_descendant > d
ret.push(node) if node.leaf? || (relative_depth_of_descendant == d)
end
ret
end
|
#leaves_when_pruned_to_level(l) ⇒ Object
55
56
57
|
# File 'lib/forester/tree_node.rb', line 55
def leaves_when_pruned_to_level(l)
leaves_when_pruned_to_depth(l - 1)
end
|
#node_level ⇒ Object
8
9
10
|
# File 'lib/forester/tree_node.rb', line 8
def node_level
node_depth + 1
end
|
#nodes_of_depth(d) ⇒ Object
12
13
14
|
# File 'lib/forester/tree_node.rb', line 12
def nodes_of_depth(d) d.between?(0, node_height) ? each_level.take(d + 1).last : []
end
|
#nodes_of_level(l) ⇒ Object
16
17
18
|
# File 'lib/forester/tree_node.rb', line 16
def nodes_of_level(l)
nodes_of_depth(l - 1)
end
|
#path_from_root ⇒ Object
20
21
22
|
# File 'lib/forester/tree_node.rb', line 20
def path_from_root
(parentage || []).reverse + [self]
end
|
#paths_of_length(l) ⇒ Object
40
41
42
|
# File 'lib/forester/tree_node.rb', line 40
def paths_of_length(l)
paths_to(nodes_of_depth(l))
end
|
#paths_to(descendants) ⇒ Object
28
29
30
|
# File 'lib/forester/tree_node.rb', line 28
def paths_to(descendants)
descendants.map { |node| node.path_from_root.drop(node_depth) }
end
|
#paths_to_leaves ⇒ Object
24
25
26
|
# File 'lib/forester/tree_node.rb', line 24
def paths_to_leaves
paths_to(leaves)
end
|
#paths_to_leaves_when_pruned_to_depth(d) ⇒ Object
59
60
61
|
# File 'lib/forester/tree_node.rb', line 59
def paths_to_leaves_when_pruned_to_depth(d)
paths_to(leaves_when_pruned_to_depth(d))
end
|
#paths_to_leaves_when_pruned_to_level(l) ⇒ Object
63
64
65
|
# File 'lib/forester/tree_node.rb', line 63
def paths_to_leaves_when_pruned_to_level(l)
paths_to(leaves_when_pruned_to_level(l))
end
|