Skip to content

Commit

Permalink
Backwards compatibility with attributes method
Browse files Browse the repository at this point in the history
Signed-off-by: John Nunemaker <nunemaker@gmail.com>
  • Loading branch information
tamalw authored and jnunemaker committed Jul 1, 2009
1 parent 9e5b655 commit 66a2a2b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
4 changes: 4 additions & 0 deletions History
@@ -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)
Expand Down
8 changes: 7 additions & 1 deletion lib/crack/xml.rb
Expand Up @@ -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 }
Expand Down
25 changes: 25 additions & 0 deletions test/xml_test.rb
Expand Up @@ -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>"
Expand Down

0 comments on commit 66a2a2b

Please sign in to comment.