Class: PacerXml::GraphVisitor
- Inherits:
-
Object
- Object
- PacerXml::GraphVisitor
- Defined in:
- lib/pacer-xml/build_graph.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#depth ⇒ Object
Returns the value of attribute depth.
-
#documents ⇒ Object
Returns the value of attribute documents.
-
#graph ⇒ Object
readonly
Returns the value of attribute graph.
-
#html ⇒ Object
readonly
Returns the value of attribute html.
-
#rename ⇒ Object
readonly
Returns the value of attribute rename.
-
#skip ⇒ Object
readonly
Returns the value of attribute skip.
-
#with_body ⇒ Object
readonly
Returns the value of attribute with_body.
Class Method Summary collapse
Instance Method Summary collapse
- #build(doc) ⇒ Object
-
#initialize(graph, opts = {}) ⇒ GraphVisitor
constructor
A new instance of GraphVisitor.
- #level ⇒ Object
- #skip?(e) ⇒ Boolean
- #tell(x) ⇒ Object
- #visit_edge_fields(e) ⇒ Object
- #visit_vertex_fields(e) ⇒ Object
- #with_body?(e) ⇒ Boolean
Constructor Details
#initialize(graph, opts = {}) ⇒ GraphVisitor
Returns a new instance of GraphVisitor.
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/pacer-xml/build_graph.rb', line 18 def initialize(graph, opts = {}) @documents = 0 @graph = graph # treat tag as a property containing html @html = (opts[:html] || []).map(&:to_s).to_set # capture the body into a body property in addition to any tags it contains. @with_body = (opts[:with_body] || []).map(&:to_s).to_set # skip property or tag @skip = (opts[:skip] || []).map(&:to_s).to_set # rename type or property @rename = self.class.build_rename(opts[:rename]) end |
Instance Attribute Details
#depth ⇒ Object
Returns the value of attribute depth.
15 16 17 |
# File 'lib/pacer-xml/build_graph.rb', line 15 def depth @depth end |
#documents ⇒ Object
Returns the value of attribute documents.
15 16 17 |
# File 'lib/pacer-xml/build_graph.rb', line 15 def documents @documents end |
#graph ⇒ Object (readonly)
Returns the value of attribute graph.
14 15 16 |
# File 'lib/pacer-xml/build_graph.rb', line 14 def graph @graph end |
#html ⇒ Object (readonly)
Returns the value of attribute html.
16 17 18 |
# File 'lib/pacer-xml/build_graph.rb', line 16 def html @html end |
#rename ⇒ Object (readonly)
Returns the value of attribute rename.
16 17 18 |
# File 'lib/pacer-xml/build_graph.rb', line 16 def rename @rename end |
#skip ⇒ Object (readonly)
Returns the value of attribute skip.
16 17 18 |
# File 'lib/pacer-xml/build_graph.rb', line 16 def skip @skip end |
#with_body ⇒ Object (readonly)
Returns the value of attribute with_body.
16 17 18 |
# File 'lib/pacer-xml/build_graph.rb', line 16 def with_body @with_body end |
Class Method Details
.build_rename(custom = {}) ⇒ Object
6 7 8 9 10 11 |
# File 'lib/pacer-xml/build_graph.rb', line 6 def build_rename(custom = {}) h = Hash.new { |h, k| h[k] = k.to_s } h['id'] = 'identifier' h.merge! custom if custom h end |
Instance Method Details
#build(doc) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/pacer-xml/build_graph.rb', line 32 def build(doc) self.documents += 1 self.depth = 0 if doc.is_a? Nokogiri::XML::Document visit_element doc.first_element_child elsif doc.element? visit_element doc elsif doc.is_a? Enumerable doc.select(&:element?).each { |e| visit_element e } else fail "Don't know what you want to do" end end |
#level ⇒ Object
89 90 91 92 93 94 |
# File 'lib/pacer-xml/build_graph.rb', line 89 def level self.depth += 1 yield ensure self.depth -= 1 end |
#skip?(e) ⇒ Boolean
81 82 83 |
# File 'lib/pacer-xml/build_graph.rb', line 81 def skip?(e) skip.include? e.name or html.include? e.name end |
#tell(x) ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/pacer-xml/build_graph.rb', line 72 def tell(x) print(' ' * depth) if depth if x.is_a? Hash or x.is_a? Array p x else puts x end end |
#visit_edge_fields(e) ⇒ Object
66 67 68 69 70 |
# File 'lib/pacer-xml/build_graph.rb', line 66 def visit_edge_fields(e) h = visit_vertex_fields(e) h.delete 'type' h end |
#visit_vertex_fields(e) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/pacer-xml/build_graph.rb', line 46 def visit_vertex_fields(e) h = e.fields h['body'] = e.inner_html if with_body? e h['type'] = rename[h['type']] rename.each do |from, to| if h.key? from h[to] = h.delete from end end html.each do |name| name = rename[name] child = e.at_xpath(name) h[name] = child.inner_html if child end skip.each do |name| h.delete name end h end |
#with_body?(e) ⇒ Boolean
85 86 87 |
# File 'lib/pacer-xml/build_graph.rb', line 85 def with_body?(e) with_body.include? e.name end |