Class: TaxGenerator::TaxonomyTree

Inherits:
Object
  • Object
show all
Includes:
ApplicationHelper
Defined in:
lib/tax_generator/classes/taxonomy_tree.rb

Overview

class used to create the Taxonomy Tree

Instance Attribute Summary collapse

Instance Method Summary collapse

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, set_celluloid_exception_handling

Constructor Details

#initialize(file_path) ⇒ void

receives a file path that will be parsed and used to build the tree

Parameters:

  • file_path (String)

    the path to the xml file that will be parsed and used to build the tree

See Also:



23
24
25
26
27
28
29
30
# File 'lib/tax_generator/classes/taxonomy_tree.rb', line 23

def initialize(file_path)
  @document = nokogiri_xml(file_path)
  taxonomy_root = @document.at_xpath('//taxonomy_name')
  @root_node = Tree::TreeNode.new(taxonomy_root.content, nil)
  @document.xpath('//node').pmap do |taxonomy_node|
    add_node(taxonomy_node, @root_node)
  end
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

Parameters:

  • name (String)

    the name of the method that is invoked against the tree

  • args (Array)

    the arguments to the method

  • block (Proc)

    the block that will be passed to the method



93
94
95
# File 'lib/tax_generator/classes/taxonomy_tree.rb', line 93

def method_missing(name, *args, &block)
  @root_node.send name, *args, &block
end

Instance Attribute Details

#documentNokogiri::XML

Returns the xml document used to build the tree.

Returns:

  • (Nokogiri::XML)

    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
# File 'lib/tax_generator/classes/taxonomy_tree.rb', line 10

class TaxonomyTree
  include TaxGenerator::ApplicationHelper
  attr_reader :root_node, :document

  #  receives a file path that will be parsed and used to build the tree
  # @see Tree::TreeNode#new
  # @see #add_node
  #
  # @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)
    taxonomy_root = @document.at_xpath('//taxonomy_name')
    @root_node = Tree::TreeNode.new(taxonomy_root.content, nil)
    @document.xpath('//node').pmap do |taxonomy_node|
      add_node(taxonomy_node, @root_node)
    end
  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' }
    insert_node(atlas_node_id, node_name, 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 = Tree::TreeNode.new(atlas_node_id.value, node_name.content)
    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)
    return unless taxonomy_node.children.any?
    tax_node = add_taxonomy_node(taxonomy_node, node)
    taxonomy_node.xpath('./node').pmap do |child_node|
      add_taxonomy_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_nodeTree::TreeNode

Returns the root node of the tree.

Returns:

  • (Tree::TreeNode)

    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
# File 'lib/tax_generator/classes/taxonomy_tree.rb', line 10

class TaxonomyTree
  include TaxGenerator::ApplicationHelper
  attr_reader :root_node, :document

  #  receives a file path that will be parsed and used to build the tree
  # @see Tree::TreeNode#new
  # @see #add_node
  #
  # @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)
    taxonomy_root = @document.at_xpath('//taxonomy_name')
    @root_node = Tree::TreeNode.new(taxonomy_root.content, nil)
    @document.xpath('//node').pmap do |taxonomy_node|
      add_node(taxonomy_node, @root_node)
    end
  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' }
    insert_node(atlas_node_id, node_name, 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 = Tree::TreeNode.new(atlas_node_id.value, node_name.content)
    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)
    return unless taxonomy_node.children.any?
    tax_node = add_taxonomy_node(taxonomy_node, node)
    taxonomy_node.xpath('./node').pmap do |child_node|
      add_taxonomy_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

Instance Method Details

#add_node(taxonomy_node, node) ⇒ 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

Parameters:

  • taxonomy_node (Nokogiri::Element)

    the nokogiri element that wants to be added to the tree

  • node (Tree::TreeNode)

    the parent node to which the element needs to be added

See Also:



76
77
78
79
80
81
82
# File 'lib/tax_generator/classes/taxonomy_tree.rb', line 76

def add_node(taxonomy_node, node)
  return unless taxonomy_node.children.any?
  tax_node = add_taxonomy_node(taxonomy_node, node)
  taxonomy_node.xpath('./node').pmap do |child_node|
    add_taxonomy_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

Parameters:

  • taxonomy_node (Nokogiri::Element)

    the nokogiri element that wants to be added to the tree

  • node (Tree::TreeNode)

    the parent node to which the element needs to be added

See Also:



42
43
44
45
46
# File 'lib/tax_generator/classes/taxonomy_tree.rb', line 42

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' }
  insert_node(atlas_node_id, node_name, node)
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

Parameters:

  • atlas_node_id (Nokogiri::Element)

    the element that holds the value of the atlas_id attribute

  • node_name (Nokogiri::Element)

    the the element that holds the node name of the element

  • node (Tree::TreeNode)

    the parent node to which the element needs to be added

See Also:

  • Tree::TreeNode#new


59
60
61
62
63
64
# File 'lib/tax_generator/classes/taxonomy_tree.rb', line 59

def insert_node(atlas_node_id, node_name, node)
  return if atlas_node_id.blank? || node_name.blank?
  current_node = Tree::TreeNode.new(atlas_node_id.value, node_name.content)
  node << current_node
  current_node
end