Class: PacerXml::GraphVisitor

Inherits:
Object
  • Object
show all
Defined in:
lib/pacer-xml/build_graph.rb

Direct Known Subclasses

BuildGraph

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graph, opts = {}) ⇒ GraphVisitor

Returns a new instance of GraphVisitor.



18
19
20
21
22
23
24
25
26
27
# 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
  # 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

#depthObject

Returns the value of attribute depth.



15
16
17
# File 'lib/pacer-xml/build_graph.rb', line 15

def depth
  @depth
end

#documentsObject

Returns the value of attribute documents.



15
16
17
# File 'lib/pacer-xml/build_graph.rb', line 15

def documents
  @documents
end

#graphObject (readonly)

Returns the value of attribute graph.



14
15
16
# File 'lib/pacer-xml/build_graph.rb', line 14

def graph
  @graph
end

#htmlObject (readonly)

Returns the value of attribute html.



16
17
18
# File 'lib/pacer-xml/build_graph.rb', line 16

def html
  @html
end

#renameObject (readonly)

Returns the value of attribute rename.



16
17
18
# File 'lib/pacer-xml/build_graph.rb', line 16

def rename
  @rename
end

#skipObject (readonly)

Returns the value of attribute skip.



16
17
18
# File 'lib/pacer-xml/build_graph.rb', line 16

def skip
  @skip
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



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pacer-xml/build_graph.rb', line 29

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

#levelObject



81
82
83
84
85
86
# File 'lib/pacer-xml/build_graph.rb', line 81

def level
  self.depth += 1
  yield
ensure
  self.depth -= 1
end

#skip?(e) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/pacer-xml/build_graph.rb', line 77

def skip?(e)
  skip.include? e.name or html.include? e.name
end

#tell(x) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/pacer-xml/build_graph.rb', line 68

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



62
63
64
65
66
# File 'lib/pacer-xml/build_graph.rb', line 62

def visit_edge_fields(e)
  h = visit_vertex_fields(e)
  h.delete 'type'
  h
end

#visit_vertex_fields(e) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pacer-xml/build_graph.rb', line 43

def visit_vertex_fields(e)
  h = e.fields
  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