Class: Forester::TreeNode
Instance Method Summary
collapse
Methods included from Views
#as_root_hash
Methods included from Mutators
#add_field!, #add_fields!, #add_fields_to_root!, #delete_values!, #delete_values_from_root!, #percolate_values!, #remove_levels_past!
Methods included from Validators
#validate_uniqueness_of_field, #validate_uniqueness_of_fields
#group_by_sibling_subtrees, #nodes_with, #own_and_descendants, #search, #with_ancestry
Instance Method Details
#contents ⇒ Object
49
50
51
|
# File 'lib/forester/tree_node.rb', line 49
def contents
each_node.map(&:content)
end
|
#each_level ⇒ Object
19
20
21
22
23
24
25
26
27
|
# File 'lib/forester/tree_node.rb', line 19
def each_level
Enumerator.new do |yielder|
level = [self]
until level.empty?
yielder << level
level = level.flat_map(&:children)
end
end
end
|
#get(field, options = {}, &if_missing) ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/forester/tree_node.rb', line 29
def get(field, options = {}, &if_missing)
default_options = {
default: :raise,
subtree: false
}
options = default_options.merge(options)
return own_and_descendants(field, &if_missing) if options[:subtree]
if has?(field)
content.get(field)
elsif block_given?
yield self
elsif options[:default] != :raise
options[:default]
else
raise ArgumentError.new("the node \"#{name}\" does not have \"#{field}\"")
end
end
|
#nodes_of_level(l) ⇒ Object
15
16
17
|
# File 'lib/forester/tree_node.rb', line 15
def nodes_of_level(l)
l.between?(0, max_level) ? each_level.take(l + 1).last : []
end
|
#same_as?(other) ⇒ Boolean
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/forester/tree_node.rb', line 53
def same_as?(other)
return false unless content == other.content
return false unless size == other.size
nodes_of_other = other.each_node.to_a
each_node.with_index do |n, i|
next if i == 0
return false unless n.same_as?(nodes_of_other[i])
end
true
end
|