Class: KindDom

Inherits:
ObjectProxy
  • Object
show all
Defined in:
lib/kind_dom.rb

Overview

KindDom provides fail-safe access to the the DOM of an XML document using three methods:

  • #collection_of to select a collection of nodes

  • #first_of to select one node

  • #content_for to get node content.

The original libxml behavior of the parsed document is preserved.

As a contrived example, in the controller:

@results = KindDom.new(xml_data)

In the view:

<% @results.first_of('//item') do |item| -%>
  <% item.content_for('description') do |description| -%>
    <!-- this block is only executed if `item` & `description` is found -->
    <p><%=h description %></p>
  <% end -%>
<% end -%>

Defined Under Namespace

Modules: Kindness

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_xml = nil) ⇒ KindDom

Returns a new instance of KindDom.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/kind_dom.rb', line 29

def initialize(raw_xml=nil)
  unless raw_xml.nil?
    parser = XML::Parser.new
    parser.string = raw_xml
    @dom = parser.parse
  end
rescue XML::Parser::ParseError
ensure
  @dom.extend Kindness
  super(@dom) # Proxy all method calls to @dom. 
end

Instance Attribute Details

#domObject

Returns the value of attribute dom.



27
28
29
# File 'lib/kind_dom.rb', line 27

def dom
  @dom
end