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



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

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



73
74
75
76
77
78
# File 'lib/dagger/ox_extension.rb', line 73

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



8
9
10
# File 'lib/dagger/ox_extension.rb', line 8

def count
  raise "Please call #children.count"
end

#dig(*paths) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dagger/ox_extension.rb', line 43

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



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/dagger/ox_extension.rb', line 60

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

#slice(*arr) ⇒ Object



23
24
25
# File 'lib/dagger/ox_extension.rb', line 23

def slice(*arr)
  Array(arr).flatten.map { |key| self[key] }
end

#to_hashObject



12
13
14
# File 'lib/dagger/ox_extension.rb', line 12

def to_hash
  self # for backwards compat
end

#values(arr = nil, include_empty: false) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dagger/ox_extension.rb', line 27

def values(arr = nil, include_empty: false)
  if arr
    Array(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
  else
    children.each_with_object({}) do |child, memo| 
      if child.to_s || include_empty
        memo[child.name] = child.to_s
      end
    end
  end
end