Class: Nokogiri::XML::Node
- Inherits:
-
Object
- Object
- Nokogiri::XML::Node
- Defined in:
- lib/pacer-xml/nokogiri_node.rb
Instance Method Summary collapse
- #attrs ⇒ Object
- #contained_rels ⇒ Object
- #container? ⇒ Boolean
- #description ⇒ Object
- #fields ⇒ Object
- #inspect ⇒ Object
- #many_rels ⇒ Object
- #one_rels ⇒ Object
- #properties ⇒ Object
- #property? ⇒ Boolean
- #rels_hash ⇒ Object
- #tree(key_map = {}) ⇒ Object
- #vertex? ⇒ Boolean
Instance Method Details
#attrs ⇒ Object
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_rels ⇒ Object
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
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 |
#description ⇒ Object
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 |
#fields ⇒ Object
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 |
#inspect ⇒ Object
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_rels ⇒ Object
138 139 140 |
# File 'lib/pacer-xml/nokogiri_node.rb', line 138 def many_rels elements.select &:container? end |
#one_rels ⇒ Object
125 126 127 |
# File 'lib/pacer-xml/nokogiri_node.rb', line 125 def one_rels elements.select &:vertex? end |
#properties ⇒ Object
101 102 103 |
# File 'lib/pacer-xml/nokogiri_node.rb', line 101 def properties elements.select(&:property?) end |
#property? ⇒ Boolean
87 88 89 |
# File 'lib/pacer-xml/nokogiri_node.rb', line 87 def property? children.all? &:text? end |
#rels_hash ⇒ Object
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
97 98 99 |
# File 'lib/pacer-xml/nokogiri_node.rb', line 97 def vertex? not property? and not container? end |