Class: Rulezilla::Tree
- Inherits:
-
Object
- Object
- Rulezilla::Tree
- Defined in:
- lib/rulezilla/tree.rb
Instance Attribute Summary collapse
-
#current_node ⇒ Object
readonly
Returns the value of attribute current_node.
-
#root_node ⇒ Object
readonly
Returns the value of attribute root_node.
Instance Method Summary collapse
- #all_results(record, node = @root_node, results = []) ⇒ Object
- #clone_and_append_children(children, node = @current_node) ⇒ Object
- #create_and_move_to_child(name = nil) ⇒ Object
-
#find_all(record, node = @root_node) ⇒ Object
Returns all the result outcomes of all the matching nodes.
- #go_up ⇒ Object
-
#initialize(node) ⇒ Tree
constructor
A new instance of Tree.
- #trace(record, node = @root_node) ⇒ Object
Constructor Details
#initialize(node) ⇒ Tree
7 8 9 10 11 |
# File 'lib/rulezilla/tree.rb', line 7 def initialize(node) @root_node = node @root_node.name = :root @current_node = node end |
Instance Attribute Details
#current_node ⇒ Object (readonly)
Returns the value of attribute current_node.
5 6 7 |
# File 'lib/rulezilla/tree.rb', line 5 def current_node @current_node end |
#root_node ⇒ Object (readonly)
Returns the value of attribute root_node.
5 6 7 |
# File 'lib/rulezilla/tree.rb', line 5 def root_node @root_node end |
Instance Method Details
#all_results(record, node = @root_node, results = []) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# 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 |
#clone_and_append_children(children, node = @current_node) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 |
# 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 |
#create_and_move_to_child(name = nil) ⇒ Object
58 59 60 61 62 63 64 |
# 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 |
#find_all(record, node = @root_node) ⇒ Object
Returns all the result outcomes of all the matching nodes.
19 20 21 22 23 24 25 26 27 28 29 |
# 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 |
#go_up ⇒ Object
13 14 15 |
# File 'lib/rulezilla/tree.rb', line 13 def go_up @current_node = is_root? ? @root_node : @current_node.parent end |
#trace(record, node = @root_node) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# 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 |