Class: XMLNode
- Inherits:
-
Struct
- Object
- Struct
- XMLNode
- Defined in:
- lib/dagger/ox_extension.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#children ⇒ Object
Returns the value of attribute children.
-
#name ⇒ Object
Returns the value of attribute name.
-
#text ⇒ Object
(also: #to_s, #value)
Returns the value of attribute text.
Instance Method Summary collapse
-
#[](key) ⇒ Object
this lets us traverse an parsed object like this: doc[:grandchild].value.
-
#all(key) ⇒ Object
returns all matching nodes.
- #count ⇒ Object
- #dig(*paths) ⇒ Object
-
#first(key) ⇒ Object
returns first matching node.
- #is_array? ⇒ Boolean
- #keys ⇒ Object
-
#slice(*arr) ⇒ Object
returns list of XMLNodes with matching names.
- #to_node ⇒ Object
- #values(keys_arr = nil, include_empty: false) ⇒ Object (also: #to_hash, #to_h)
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes
3 4 5 |
# File 'lib/dagger/ox_extension.rb', line 3 def attributes @attributes end |
#children ⇒ Object
Returns the value of attribute children
3 4 5 |
# File 'lib/dagger/ox_extension.rb', line 3 def children @children end |
#name ⇒ Object
Returns the value of attribute name
3 4 5 |
# File 'lib/dagger/ox_extension.rb', line 3 def name @name end |
#text ⇒ Object Also known as: to_s, value
Returns the value of attribute text
3 4 5 |
# File 'lib/dagger/ox_extension.rb', line 3 def text @text end |
Instance Method Details
#[](key) ⇒ Object
this lets us traverse an parsed object like this: doc[:grandchild].value
26 27 28 29 |
# File 'lib/dagger/ox_extension.rb', line 26 def [](key) found = children.select { |node| node.name.to_s == key.to_s } found.empty? ? nil : found.size == 1 ? found.first : found end |
#all(key) ⇒ Object
returns all matching nodes
84 85 86 87 88 89 |
# File 'lib/dagger/ox_extension.rb', line 84 def all(key) found = self[key] direct = found.is_a?(XMLNode) ? [found] : found || [] indirect = children.map { |ch| ch.all(key) }.flatten.compact direct + indirect end |
#count ⇒ Object
12 13 14 |
# File 'lib/dagger/ox_extension.rb', line 12 def count raise "Please call #children.count" end |
#dig(*paths) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/dagger/ox_extension.rb', line 55 def dig(*paths) list = Array(paths).flatten res = list.reduce([self]) do |parents, key| if parents found = parents.map do |parent| parent.children.select { |node| node.name.to_s == key.to_s } end.flatten found.any? ? found : nil end end res.nil? || res.empty? ? nil : res.size == 1 ? res.first : res end |
#first(key) ⇒ Object
returns first matching node
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/dagger/ox_extension.rb', line 71 def first(key) if found = self[key] found.is_a?(XMLNode) ? found : found.first else children.find do |ch| if res = ch.first(key) return res end end end end |
#is_array? ⇒ Boolean
20 21 22 |
# File 'lib/dagger/ox_extension.rb', line 20 def is_array? keys.count != keys.uniq.count end |
#keys ⇒ Object
16 17 18 |
# File 'lib/dagger/ox_extension.rb', line 16 def keys @keys ||= children.collect(&:name) end |
#slice(*arr) ⇒ Object
returns list of XMLNodes with matching names
32 33 34 |
# File 'lib/dagger/ox_extension.rb', line 32 def slice(*arr) Array(arr).flatten.map { |key| self[key] } end |
#to_node ⇒ Object
8 9 10 |
# File 'lib/dagger/ox_extension.rb', line 8 def to_node self end |
#values(keys_arr = nil, include_empty: false) ⇒ Object Also known as: to_hash, to_h
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/dagger/ox_extension.rb', line 36 def values(keys_arr = nil, include_empty: false) if keys_arr Array(keys_arr).flatten.each_with_object({}) do |key, memo| if found = self[key] and (found.to_s || include_empty) memo[key] = found.to_s end end elsif is_array? children.map(&:values) else children.each_with_object({}) do |child, memo| memo[child.name] = child.children.any? ? child.values : child.text end end end |