Class: XMLNode

Inherits:
Struct
  • Object
show all
Defined in:
lib/dagger/ox_extension.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes

Returns:

  • (Object)

    the current value of attributes



3
4
5
# File 'lib/dagger/ox_extension.rb', line 3

def attributes
  @attributes
end

#childrenObject

Returns the value of attribute children

Returns:

  • (Object)

    the current value of children



3
4
5
# File 'lib/dagger/ox_extension.rb', line 3

def children
  @children
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



3
4
5
# File 'lib/dagger/ox_extension.rb', line 3

def name
  @name
end

#textObject Also known as: to_s, value

Returns the value of attribute text

Returns:

  • (Object)

    the current value of 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

#countObject



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

Returns:

  • (Boolean)


20
21
22
# File 'lib/dagger/ox_extension.rb', line 20

def is_array?
  keys.count != keys.uniq.count
end

#keysObject



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_nodeObject



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