Permalink
Browse files

Backwards compatibility with attributes method

Signed-off-by: John Nunemaker <nunemaker@gmail.com>
  • Loading branch information...
tamalw authored and jnunemaker committed Jun 22, 2009
1 parent 9e5b655 commit 66a2a2b860765a4c6577eb064dc0faf49b68a0f0
Showing with 36 additions and 1 deletion.
  1. +4 −0 History
  2. +7 −1 lib/crack/xml.rb
  3. +25 −0 test/xml_test.rb
View
@@ -1,3 +1,7 @@
+== 0.1.3 2009-06-22
+* 1 minor patch
+ * Parsing a text node with attributes stores them in the attributes method (tamalw)
+
== 0.1.2 2009-04-21
* 2 minor patches
* Correct unnormalization of attribute values (der-flo)
View
@@ -81,7 +81,13 @@ class << f
end
if @text
- return { name => typecast_value( unnormalize_xml_entities( inner_html ) ) }
+ t = typecast_value( unnormalize_xml_entities( inner_html ) )
+ unless t.respond_to? :attributes
+ # Some types don't work with singleton methods, so modifying the class instead
+ t.class.send :attr_accessor, :attributes
+ end
+ t.attributes = attributes
+ return { name => t }
else
#change repeating groups into an array
groups = @children.inject({}) { |s,e| (s[e.name] ||= []) << e; s }
View
@@ -68,6 +68,31 @@ class XmlTest < Test::Unit::TestCase
Crack::XML.parse(xml).should == hash
end
+
+ should "should include attributes hash if present" do
+ xml =<<-XML
+ <opt>
+ <user login="grep">Gary R Epstein</user>
+ <user>Simon T Tyson</user>
+ </opt>
+ XML
+
+ Crack::XML.parse(xml)['opt']['user'].class.should == Array
+
+ hash = {
+ 'opt' => {
+ 'user' => [
+ 'Gary R Epstein',
+ 'Simon T Tyson'
+ ]
+ }
+ }
+
+ Crack::XML.parse(xml).should == hash
+
+ Crack::XML.parse(xml)['opt']['user'][0].attributes.should == { 'login' => 'grep' }
+ Crack::XML.parse(xml)['opt']['user'][1].attributes.should == {}
+ end
should "should typecast an integer" do
xml = "<tag type='integer'>10</tag>"

0 comments on commit 66a2a2b

Please sign in to comment.