class Rulezilla::Tree
Attributes
Public Class Methods
Source
# File lib/rulezilla/tree.rb, line 7 def initialize(node) @root_node = node @root_node.name = :root @current_node = node end
Public Instance Methods
Source
# File lib/rulezilla/tree.rb, line 42 def all_results(record, node = @root_node, results = []) if node.has_result? begin results << node.result(record) rescue StandardError NoMethodError end end node.children.each do |child_node| all_results(record, child_node, results) end results end
Source
# File lib/rulezilla/tree.rb, line 66 def clone_and_append_children(children, node = @current_node) children.each do |child_node| child_node = child_node.dup node.add_child(child_node) next unless child_node.has_children? children_nodes = child_node.children child_node.children = [] clone_and_append_children(children_nodes, child_node) end end
Source
# File lib/rulezilla/tree.rb, line 58 def create_and_move_to_child(name = nil) node = Node.new node.name = name @current_node.add_child(node) @current_node = node node end
Source
# File lib/rulezilla/tree.rb, line 19 def find_all(record, node = @root_node) array = [] if node.applies?(record) node.children.each do |child_node| array += find_all(record, child_node) end return node.has_result? ? array + [node] : array end array end
Returns all the result outcomes of all the matching nodes.
Source
# File lib/rulezilla/tree.rb, line 13 def go_up @current_node = is_root? ? @root_node : @current_node.parent end
Source
# File lib/rulezilla/tree.rb, line 31 def trace(record, node = @root_node) if node.applies?(record) node.children.each do |child_node| array = trace(record, child_node) return [node] + array unless array.empty? end return node.has_result? ? [node] : [] end [] end
Private Instance Methods
Source
# File lib/rulezilla/tree.rb, line 81 def is_root? @current_node == @root_node end