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

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



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

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



25
26
27
28
29
30
# File 'lib/dagger/ox_extension.rb', line 25

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

#first(key) ⇒ Object

returns first matching node



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/dagger/ox_extension.rb', line 12

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