rasantiago-tiny_xpath_helper 0.1.1 → 0.1.2
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.rdoc +5 -0
- data/lib/tiny_xpath_helper.rb +8 -4
- data/tiny_xpath_helper.gemspec +1 -1
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -40,6 +40,11 @@ The output from :format => :xml is suitable for passing to another TinyXPathHelp
|
|
40
40
|
>> deeper_xpath = TinyXPathHelper.new( node_tree )
|
41
41
|
>> deeper_xpath[ 'b/@beta' ]
|
42
42
|
=> ["true"]
|
43
|
+
|
44
|
+
If you want empty documents to not raise an error
|
45
|
+
>> empty_xpath = TinyXPathHelper.new( String.new, :allow_empty_document => true )
|
46
|
+
>> empty_xpath[ 'b/@beta' ]
|
47
|
+
=> []
|
43
48
|
|
44
49
|
--
|
45
50
|
doctest_require: 'lib/tiny_xpath_helper.rb'
|
data/lib/tiny_xpath_helper.rb
CHANGED
@@ -3,9 +3,9 @@ require 'rexml/document'
|
|
3
3
|
class TinyXPathHelper
|
4
4
|
attr :node
|
5
5
|
|
6
|
-
def initialize(xml)
|
6
|
+
def initialize(xml, options = {})
|
7
7
|
@xml = xml
|
8
|
-
@node = self.class.xml_node_for_xmlish(xml)
|
8
|
+
@node = self.class.xml_node_for_xmlish(xml, options)
|
9
9
|
end
|
10
10
|
|
11
11
|
def find_xpath(xpath_expr, options = {})
|
@@ -31,7 +31,7 @@ class TinyXPathHelper
|
|
31
31
|
io_stream_classes + [ String, REXML::Document ]
|
32
32
|
end
|
33
33
|
|
34
|
-
def self.xml_node_for_xmlish( xml )
|
34
|
+
def self.xml_node_for_xmlish( xml, options = {} )
|
35
35
|
if io_stream_classes.any?{|k| k === xml }
|
36
36
|
xml = xml.read
|
37
37
|
end
|
@@ -39,7 +39,11 @@ class TinyXPathHelper
|
|
39
39
|
xml = REXML::Document.new(xml)
|
40
40
|
end
|
41
41
|
if REXML::Document === xml
|
42
|
-
|
42
|
+
if options[:allow_empty_document] and ! xml.root
|
43
|
+
xml = xml
|
44
|
+
else
|
45
|
+
xml = xml.root
|
46
|
+
end
|
43
47
|
end
|
44
48
|
if not REXML::Element === xml
|
45
49
|
raise TypeError.new("Expected REXML::Element, got #{xml.class}")
|
data/tiny_xpath_helper.gemspec
CHANGED