class Forester::TreeNode

Public Instance Methods

get(field, default = :raise) { |field, self| ... } click to toggle source
# 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) click to toggle source
# File lib/forester/tree_node.rb, line 88
def has_field?(field)
  content.key?(field)
end
leaf?() click to toggle source
# File lib/forester/tree_node.rb, line 32
def leaf?
  is_leaf?
end
leaves() click to toggle source
# File lib/forester/tree_node.rb, line 36
def leaves
  each_leaf
end
leaves_when_pruned_to_depth(d) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/forester/tree_node.rb, line 8
def node_level
  node_depth + 1
end
nodes_of_depth(d) click to toggle source
# File lib/forester/tree_node.rb, line 12
def nodes_of_depth(d) # relative to this node
  d.between?(0, node_height) ? each_level.take(d + 1).last : []
end
nodes_of_level(l) click to toggle source
# File lib/forester/tree_node.rb, line 16
def nodes_of_level(l)
  nodes_of_depth(l - 1)
end
path_from_root() click to toggle source
# File lib/forester/tree_node.rb, line 20
def path_from_root
  (parentage || []).reverse + [self]
end
paths_of_length(l) click to toggle source
# File lib/forester/tree_node.rb, line 40
def paths_of_length(l)
  paths_to(nodes_of_depth(l))
end
paths_to(descendants) click to toggle source
# 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() click to toggle source
# File lib/forester/tree_node.rb, line 24
def paths_to_leaves
  paths_to(leaves)
end
paths_to_leaves_when_pruned_to_depth(d) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

as_array(object) click to toggle source
# File lib/forester/tree_node.rb, line 94
def as_array(object)
  [object].flatten(1)
end