kind_dom 0.9.2 → 0.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +5 -26
- data/lib/kind_dom.rb +18 -19
- metadata +2 -2
data/README
CHANGED
@@ -1,32 +1,11 @@
|
|
1
|
-
KindDom provides
|
2
|
-
|
3
|
-
|
4
|
-
- #first_of to select one node
|
5
|
-
- #content_for to get node content.
|
6
|
-
|
7
|
-
The original libxml behavior of the parsed document is preserved.
|
8
|
-
|
9
|
-
As a contrived example, in the controller:
|
10
|
-
@results = KindDom.new(xml_data)
|
11
|
-
|
12
|
-
In the view:
|
13
|
-
<% @results.first_of('//item') do |item| -%>
|
14
|
-
<!-- this block is only executed if `item` is found -->
|
15
|
-
|
16
|
-
<% item.content_for('title') do |title| -%>
|
17
|
-
<!-- this block is only executed if `title` has content -->
|
18
|
-
<p><%=h title %></p>
|
19
|
-
<% end -%>
|
20
|
-
|
21
|
-
<% item.content_for('description') do |description| -%>
|
22
|
-
<!-- this block is only executed if `description` has content -->
|
23
|
-
<p><%=h description %></p>
|
24
|
-
<% end -%>
|
25
|
-
<% end -%>
|
1
|
+
KindDom provides graceful access to the the DOM of an XML document, while preserving the original libxml behavior of the parsed document.
|
2
|
+
|
3
|
+
See the source code comments, rdoc or ri for usage details; the tests also demonstrate use cases.
|
26
4
|
|
27
5
|
Project Home http://code.google.com/p/ruby-kind-dom/
|
28
6
|
|
29
|
-
|
7
|
+
|
8
|
+
Licensed under The MIT License.
|
30
9
|
|
31
10
|
Copyright (c) 2008 Scout Labs Inc. http://scoutlabs.com
|
32
11
|
|
data/lib/kind_dom.rb
CHANGED
@@ -3,11 +3,11 @@ require 'active_support'
|
|
3
3
|
require 'xml/libxml'
|
4
4
|
|
5
5
|
|
6
|
-
# KindDom provides
|
7
|
-
# three methods:
|
8
|
-
# -
|
9
|
-
# -
|
10
|
-
# -
|
6
|
+
# KindDom provides graceful access to the the DOM of an XML document using
|
7
|
+
# three methods of Kindness:
|
8
|
+
# - <tt>#collection_of</tt> to select a collection of nodes
|
9
|
+
# - <tt>#first_of</tt> to select one node
|
10
|
+
# - <tt>#content_for</tt> to get node content.
|
11
11
|
#
|
12
12
|
# The original libxml behavior of the parsed document is preserved.
|
13
13
|
#
|
@@ -15,17 +15,18 @@ require 'xml/libxml'
|
|
15
15
|
# @results = KindDom.new(xml_data)
|
16
16
|
#
|
17
17
|
# In the view:
|
18
|
+
#
|
18
19
|
# <% @results.first_of('//item') do |item| -%>
|
19
|
-
#
|
20
|
-
#
|
20
|
+
# <p>(This block is only executed if `item` is found.)</p>
|
21
|
+
#
|
21
22
|
# <% item.content_for('title') do |title| -%>
|
22
|
-
#
|
23
|
-
# <p
|
23
|
+
# <h2><%=h title %></h2>
|
24
|
+
# <p>(This block is only executed if `title` has content.)</p>
|
24
25
|
# <% end -%>
|
25
|
-
#
|
26
|
+
#
|
26
27
|
# <% item.content_for('description') do |description| -%>
|
27
|
-
# <!-- this block is only executed if `description` has content -->
|
28
28
|
# <p><%=h description %></p>
|
29
|
+
# <p>(This block is only executed if `description` has content.)</p>
|
29
30
|
# <% end -%>
|
30
31
|
# <% end -%>
|
31
32
|
#
|
@@ -50,12 +51,10 @@ class KindDom < ObjectProxy
|
|
50
51
|
#
|
51
52
|
# Optional second argument is the default value to return if the DOM find fails.
|
52
53
|
#
|
53
|
-
# When a block is provided, it will be
|
54
|
+
# When a block is provided, it will be called with the found content
|
54
55
|
# (or default) before returning.
|
55
56
|
#
|
56
|
-
|
57
|
-
#
|
58
|
-
def content_for(xpath, default=nil)
|
57
|
+
def content_for(xpath, default=nil) # :yields: found_content
|
59
58
|
node = case self.class.to_s
|
60
59
|
when 'XML::Document' then
|
61
60
|
root.find_first(xpath)
|
@@ -82,10 +81,10 @@ class KindDom < ObjectProxy
|
|
82
81
|
#
|
83
82
|
# Each node is extended with Kindness to support #content_for, #collection_of & #first_of.
|
84
83
|
#
|
85
|
-
# When a block is provided, it will be
|
84
|
+
# When a block is provided, it will be called with the found collection
|
86
85
|
# (or default) before returning.
|
87
86
|
#
|
88
|
-
def collection_of(xpath, default=nil)
|
87
|
+
def collection_of(xpath, default=nil) # :yields: found_collection
|
89
88
|
c = find(xpath).collect {|n| n.extend Kindness }
|
90
89
|
rescue NoMethodError
|
91
90
|
ensure
|
@@ -101,10 +100,10 @@ class KindDom < ObjectProxy
|
|
101
100
|
#
|
102
101
|
# The node is extended with Kindness to support #content_for, #collection_of & #first_of.
|
103
102
|
#
|
104
|
-
# When a block is provided, it will be
|
103
|
+
# When a block is provided, it will be called with the found node
|
105
104
|
# (or default) before returning.
|
106
105
|
#
|
107
|
-
def first_of(xpath, default=nil)
|
106
|
+
def first_of(xpath, default=nil) # :yields: found_node
|
108
107
|
n = case self.class.to_s
|
109
108
|
when 'XML::Document' then
|
110
109
|
root.find_first(xpath)
|
metadata
CHANGED