Class: TaxGenerator::TaxonomyTree
- Inherits:
-
Object
- Object
- TaxGenerator::TaxonomyTree
- Includes:
- ApplicationHelper
- Defined in:
- lib/tax_generator/classes/taxonomy_tree.rb
Overview
class used to create the Taxonomy Tree
Instance Attribute Summary collapse
-
#document ⇒ Nokogiri::XML
The xml document used to build the tree.
-
#root_node ⇒ Tree::TreeNode
The root node of the tree.
-
#taxonomies ⇒ Object
readonly
Returns the value of attribute taxonomies.
Instance Method Summary collapse
-
#add_node(taxonomy_node, node, options = {}) ⇒ void
checks to see if the nokogiri element has any childrens, if it has , will add it to the tree and iterates over the children and adds them as child to the newly added node.
-
#add_taxonomy_node(taxonomy_node, node) ⇒ void
gets the atlas_id from the nokogiri element and then searches first child whose name is ‘node_name’ and uses this to insert the node.
-
#find_by_name(node_id, node = @root_node, list = []) ⇒ void
finds a node by the name in the tree list.
-
#find_taxonomies ⇒ void
searches all the taxonomy elements in the document and adds them as top level nodes and then calls method add_node to search inside childrens.
-
#initialize(file_path) ⇒ void
constructor
receives a file path that will be parsed and used to build the tree.
-
#insert_node(atlas_node_id, node_name, node) ⇒ void
inserts a new node in the tree by checking first if atlas_id and node_name are present and then adds the node as child to the node passed as third argument.
-
#method_missing(name, *args, &block) ⇒ void
receives a file path that will be parsed and used to build the tree.
Methods included from ApplicationHelper
create_directories, elements_with_content, erb_template, execute_with_rescue, format_error, log_error, log_message, nokogiri_xml, rescue_interrupt, root
Constructor Details
#initialize(file_path) ⇒ void
receives a file path that will be parsed and used to build the tree
23 24 25 26 27 28 |
# File 'lib/tax_generator/classes/taxonomy_tree.rb', line 23 def initialize(file_path) @document = nokogiri_xml(file_path) @root_node = TaxGenerator::TaxonomyNode.new('ROOT', 'ROOT') @taxonomies = [] find_taxonomies end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ void
This method returns an undefined value.
receives a file path that will be parsed and used to build the tree
127 128 129 |
# File 'lib/tax_generator/classes/taxonomy_tree.rb', line 127 def method_missing(name, *args, &block) @root_node.send name, *args, &block end |
Instance Attribute Details
#document ⇒ Nokogiri::XML
Returns the xml document used to build the tree.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/tax_generator/classes/taxonomy_tree.rb', line 10 class TaxonomyTree include TaxGenerator::ApplicationHelper attr_reader :root_node, :document, :taxonomies # receives a file path that will be parsed and used to build the tree # @see Tree::TreeNode#new # @see #find_taxonomies # # @param [String] file_path the path to the xml file that will be parsed and used to build the tree # # @return [void] # # @api public def initialize(file_path) @document = nokogiri_xml(file_path) @root_node = TaxGenerator::TaxonomyNode.new('ROOT', 'ROOT') @taxonomies = [] find_taxonomies end # searches all the taxonomy elements in the document and adds them as top level nodes # and then calls method add_node to search inside childrens # @see #insert_node # @see #add_node # # @return [void] # # @api public def find_taxonomies @document.xpath('.//taxonomy').each do |taxonomy_node| taxonomy_name = taxonomy_node.at_xpath('.//taxonomy_name') tax_node = insert_node(SecureRandom.uuid, taxonomy_name.content, @root_node) @taxonomies << tax_node add_node(taxonomy_node, tax_node, skip_add: true) end end # finds a node by the name in the tree list # # @param [String] node_id the name of the node that needs to be found # @param [Tree::TreeNode] node the node that will be used to search in # @param [Array] list the list that holds the nodes # # @return [void] # # @api public def find_by_name(node_id, node = @root_node, list = []) if node.name.to_s == node_id.to_s list << node else node.children.each { |child| find_by_name(node_id, child, list) } end list.compact end # gets the atlas_id from the nokogiri element and then searches first child whose name is 'node_name' # and uses this to insert the node # @see #insert_node # # @param [Nokogiri::Element] taxonomy_node the nokogiri element that wants to be added to the tree # @param [Tree::TreeNode] node the parent node to which the element needs to be added # # @return [void] # # @api public def add_taxonomy_node(taxonomy_node, node) atlas_node_id = taxonomy_node.attributes['atlas_node_id'] node_name = taxonomy_node.children.find { |child| child.name == 'node_name' } return if atlas_node_id.blank? || node_name.blank? insert_node(atlas_node_id.value, node_name.content, node) end # inserts a new node in the tree by checking first if atlas_id and node_name are present # and then adds the node as child to the node passed as third argument # @see Tree::TreeNode#new # # @param [Nokogiri::Element] atlas_node_id the element that holds the value of the atlas_id attribute # @param [Nokogiri::Element] node_name the the element that holds the node name of the element # @param [Tree::TreeNode] node the parent node to which the element needs to be added # # @return [void] # # @api public def insert_node(atlas_node_id, node_name, node) return if atlas_node_id.blank? || node_name.blank? current_node = TaxGenerator::TaxonomyNode.new(atlas_node_id, node_name) node << current_node current_node end # checks to see if the nokogiri element has any childrens, if it has , will add it to the tree and iterates over the # children and adds them as child to the newly added node # @see #add_taxonomy_node # # @param [Nokogiri::Element] taxonomy_node the nokogiri element that wants to be added to the tree # @param [Tree::TreeNode] node the parent node to which the element needs to be added # # @return [void] # # @api public def add_node(taxonomy_node, node, = {}) tax_node = [:skip_add].present? ? node : add_taxonomy_node(taxonomy_node, node) return unless taxonomy_node.children.any? taxonomy_node.xpath('./node').each do |child_node| add_node(child_node, tax_node) if tax_node.present? end end # receives a file path that will be parsed and used to build the tree # # @param [String] name the name of the method that is invoked against the tree # @param [Array] args the arguments to the method # @param [Proc] block the block that will be passed to the method # # @return [void] # # @api public def method_missing(name, *args, &block) @root_node.send name, *args, &block end end |
#root_node ⇒ Tree::TreeNode
Returns the root node of the tree.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/tax_generator/classes/taxonomy_tree.rb', line 10 class TaxonomyTree include TaxGenerator::ApplicationHelper attr_reader :root_node, :document, :taxonomies # receives a file path that will be parsed and used to build the tree # @see Tree::TreeNode#new # @see #find_taxonomies # # @param [String] file_path the path to the xml file that will be parsed and used to build the tree # # @return [void] # # @api public def initialize(file_path) @document = nokogiri_xml(file_path) @root_node = TaxGenerator::TaxonomyNode.new('ROOT', 'ROOT') @taxonomies = [] find_taxonomies end # searches all the taxonomy elements in the document and adds them as top level nodes # and then calls method add_node to search inside childrens # @see #insert_node # @see #add_node # # @return [void] # # @api public def find_taxonomies @document.xpath('.//taxonomy').each do |taxonomy_node| taxonomy_name = taxonomy_node.at_xpath('.//taxonomy_name') tax_node = insert_node(SecureRandom.uuid, taxonomy_name.content, @root_node) @taxonomies << tax_node add_node(taxonomy_node, tax_node, skip_add: true) end end # finds a node by the name in the tree list # # @param [String] node_id the name of the node that needs to be found # @param [Tree::TreeNode] node the node that will be used to search in # @param [Array] list the list that holds the nodes # # @return [void] # # @api public def find_by_name(node_id, node = @root_node, list = []) if node.name.to_s == node_id.to_s list << node else node.children.each { |child| find_by_name(node_id, child, list) } end list.compact end # gets the atlas_id from the nokogiri element and then searches first child whose name is 'node_name' # and uses this to insert the node # @see #insert_node # # @param [Nokogiri::Element] taxonomy_node the nokogiri element that wants to be added to the tree # @param [Tree::TreeNode] node the parent node to which the element needs to be added # # @return [void] # # @api public def add_taxonomy_node(taxonomy_node, node) atlas_node_id = taxonomy_node.attributes['atlas_node_id'] node_name = taxonomy_node.children.find { |child| child.name == 'node_name' } return if atlas_node_id.blank? || node_name.blank? insert_node(atlas_node_id.value, node_name.content, node) end # inserts a new node in the tree by checking first if atlas_id and node_name are present # and then adds the node as child to the node passed as third argument # @see Tree::TreeNode#new # # @param [Nokogiri::Element] atlas_node_id the element that holds the value of the atlas_id attribute # @param [Nokogiri::Element] node_name the the element that holds the node name of the element # @param [Tree::TreeNode] node the parent node to which the element needs to be added # # @return [void] # # @api public def insert_node(atlas_node_id, node_name, node) return if atlas_node_id.blank? || node_name.blank? current_node = TaxGenerator::TaxonomyNode.new(atlas_node_id, node_name) node << current_node current_node end # checks to see if the nokogiri element has any childrens, if it has , will add it to the tree and iterates over the # children and adds them as child to the newly added node # @see #add_taxonomy_node # # @param [Nokogiri::Element] taxonomy_node the nokogiri element that wants to be added to the tree # @param [Tree::TreeNode] node the parent node to which the element needs to be added # # @return [void] # # @api public def add_node(taxonomy_node, node, = {}) tax_node = [:skip_add].present? ? node : add_taxonomy_node(taxonomy_node, node) return unless taxonomy_node.children.any? taxonomy_node.xpath('./node').each do |child_node| add_node(child_node, tax_node) if tax_node.present? end end # receives a file path that will be parsed and used to build the tree # # @param [String] name the name of the method that is invoked against the tree # @param [Array] args the arguments to the method # @param [Proc] block the block that will be passed to the method # # @return [void] # # @api public def method_missing(name, *args, &block) @root_node.send name, *args, &block end end |
#taxonomies ⇒ Object (readonly)
Returns the value of attribute taxonomies.
12 13 14 |
# File 'lib/tax_generator/classes/taxonomy_tree.rb', line 12 def taxonomies @taxonomies end |
Instance Method Details
#add_node(taxonomy_node, node, options = {}) ⇒ void
This method returns an undefined value.
checks to see if the nokogiri element has any childrens, if it has , will add it to the tree and iterates over the children and adds them as child to the newly added node
110 111 112 113 114 115 116 |
# File 'lib/tax_generator/classes/taxonomy_tree.rb', line 110 def add_node(taxonomy_node, node, = {}) tax_node = [:skip_add].present? ? node : add_taxonomy_node(taxonomy_node, node) return unless taxonomy_node.children.any? taxonomy_node.xpath('./node').each do |child_node| add_node(child_node, tax_node) if tax_node.present? end end |
#add_taxonomy_node(taxonomy_node, node) ⇒ void
This method returns an undefined value.
gets the atlas_id from the nokogiri element and then searches first child whose name is ‘node_name’ and uses this to insert the node
75 76 77 78 79 80 |
# File 'lib/tax_generator/classes/taxonomy_tree.rb', line 75 def add_taxonomy_node(taxonomy_node, node) atlas_node_id = taxonomy_node.attributes['atlas_node_id'] node_name = taxonomy_node.children.find { |child| child.name == 'node_name' } return if atlas_node_id.blank? || node_name.blank? insert_node(atlas_node_id.value, node_name.content, node) end |
#find_by_name(node_id, node = @root_node, list = []) ⇒ void
This method returns an undefined value.
finds a node by the name in the tree list
56 57 58 59 60 61 62 63 |
# File 'lib/tax_generator/classes/taxonomy_tree.rb', line 56 def find_by_name(node_id, node = @root_node, list = []) if node.name.to_s == node_id.to_s list << node else node.children.each { |child| find_by_name(node_id, child, list) } end list.compact end |
#find_taxonomies ⇒ void
This method returns an undefined value.
searches all the taxonomy elements in the document and adds them as top level nodes and then calls method add_node to search inside childrens
38 39 40 41 42 43 44 45 |
# File 'lib/tax_generator/classes/taxonomy_tree.rb', line 38 def find_taxonomies @document.xpath('.//taxonomy').each do |taxonomy_node| taxonomy_name = taxonomy_node.at_xpath('.//taxonomy_name') tax_node = insert_node(SecureRandom.uuid, taxonomy_name.content, @root_node) @taxonomies << tax_node add_node(taxonomy_node, tax_node, skip_add: true) end end |
#insert_node(atlas_node_id, node_name, node) ⇒ void
This method returns an undefined value.
inserts a new node in the tree by checking first if atlas_id and node_name are present and then adds the node as child to the node passed as third argument
93 94 95 96 97 98 |
# File 'lib/tax_generator/classes/taxonomy_tree.rb', line 93 def insert_node(atlas_node_id, node_name, node) return if atlas_node_id.blank? || node_name.blank? current_node = TaxGenerator::TaxonomyNode.new(atlas_node_id, node_name) node << current_node current_node end |