Class: LibraryThing::NodeWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/librarything/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ NodeWrapper

Returns a new instance of NodeWrapper.



9
10
11
# File 'lib/librarything/resource.rb', line 9

def initialize(node)
  @node = node
end

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



7
8
9
# File 'lib/librarything/resource.rb', line 7

def node
  @node
end

Instance Method Details

#[](name) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/librarything/resource.rb', line 17

def [](name)
  # Check for attributes first. Why? It works for the LT xml (for now).
  if @node.attributes.has_key?(name)
    return @node.attributes[name].value
  end

  # Find child elements with that name
  children = @node.xpath("lt:#{name}")
  if children.size == 1
    child = children.first
  
    # LT-specific hack to simplify access to lists. Ill-advised?
    if child.name =~ /^[\w]*List$/
      return wrap_children(child.children)
    else
      return NodeWrapper.new(child)
    end
  elsif children.size > 1
    return wrap_children(children)
  else
    return nil
  end
end

#contentObject



13
14
15
# File 'lib/librarything/resource.rb', line 13

def content
  @node.content
end