Class: Nokogiri::XML::Node

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

Instance Method Summary collapse

Instance Method Details

#attrsObject



105
106
107
108
109
110
111
# File 'lib/pacer-xml/nokogiri_node.rb', line 105

def attrs
  if respond_to? :attributes
    attributes
  else
    {}
  end
end

#contained_relsObject



129
130
131
132
133
134
135
136
# File 'lib/pacer-xml/nokogiri_node.rb', line 129

def contained_rels
  if container?
    elements.select(&:vertex?) +
      elements.select(&:container?).flat_map(&:contained_rels)
  else
    []
  end
end

#container?Boolean

Returns:

  • (Boolean)


91
92
93
94
95
# File 'lib/pacer-xml/nokogiri_node.rb', line 91

def container?
  not property? and
    elements.map(&:name).uniq.length == 1 and
    elements.all? { |e| e.vertex? or e.container? }
end

#descriptionObject



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pacer-xml/nokogiri_node.rb', line 74

def description
  s = if property?
    "property"
  elsif container?
    'container'
  elsif vertex?
    'vertex'
  else
    'other'
  end
  "#{ s } #{ name }"
end

#fieldsObject



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/pacer-xml/nokogiri_node.rb', line 113

def fields
  result = {}
  attrs.each do |name, attr|
    result[name] = attr.value
  end
  properties.each do |e|
    result[e.name] = e.text
  end
  result['type'] = name
  result
end

#inspectObject



66
67
68
69
70
71
72
# File 'lib/pacer-xml/nokogiri_node.rb', line 66

def inspect
  if children.all? &:text?
    "#<Property #{ name }>"
  else
    "#<Element #{ name } [#{ elements.map(&:name).uniq.join(', ') }]>"
  end
end

#many_relsObject



138
139
140
# File 'lib/pacer-xml/nokogiri_node.rb', line 138

def many_rels
  elements.select &:container?
end

#one_relsObject



125
126
127
# File 'lib/pacer-xml/nokogiri_node.rb', line 125

def one_rels
  elements.select &:vertex?
end

#propertiesObject



101
102
103
# File 'lib/pacer-xml/nokogiri_node.rb', line 101

def properties
  elements.select(&:property?)
end

#property?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/pacer-xml/nokogiri_node.rb', line 87

def property?
  children.all? &:text?
end

#rels_hashObject



142
143
144
145
146
147
# File 'lib/pacer-xml/nokogiri_node.rb', line 142

def rels_hash
  result = Hash.new { |h, k| h[k] = [] }
  one_rels.each  { |e| result[e.name] << e }
  many_rels.each { |e| result[e.name] += e.contained_rels }
  result
end

#tree(key_map = {}) ⇒ Object



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
# File 'lib/pacer-xml/nokogiri_node.rb', line 17

def tree(key_map = {})
  c = elements.map { |x| x.tree(key_map) }.compact
  if c.empty?
    key_map.fetch(name, name)
  else
    ct = {}
    texts = []
    attrs = {}
    if respond_to? :attributes
      attrs = Hash[attributes.map { |k, a|
        k = key_map.fetch(k, k)
        [k, a.value] if k
      }.compact]
    end
    c.each do |h|
      if h.is_a? String
        texts << h
        next
      end
      h.each do |name, value|
        if ct.key? name
          if ct[name].is_a? Array
            ct[name] << value unless ct[name].include? value
          elsif ct[name] != value
            ct[name] = [ct[name], value]
          end
        else
          ct[name] = value
        end
      end
    end
    ct.merge! attrs
    key = key_map.fetch(name, name)
    if key
      if ct.empty?
        if texts.count < 2
          { key => texts.first }
        else
          { key => texts.uniq }
        end
      elsif texts.any?
        { key => ct }
      else
        { key => ct }
      end
    end
  end
end

#vertex?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/pacer-xml/nokogiri_node.rb', line 97

def vertex?
  not property? and not container?
end