Skip to content

Commit

Permalink
Handle elements with both attributes and contents
Browse files Browse the repository at this point in the history
  • Loading branch information
szajbus committed Oct 25, 2013
1 parent 6ad84b0 commit a4bc4c9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Tag attributes become object attributes and attributes' name are underscored.

Saxy.parse("filename.xml", "product").each do |product|
puts product.name
puts product.images.thumb_size
puts product.images.thumb_size.contents
puts "#{product.images.thumb_size.width}x#{product.images.thumb_size.height}"
end

Expand Down
1 change: 1 addition & 0 deletions lib/saxy/element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def as_object
value = value.first if value.size == 1
object.send("#{name}=", value)
end
object.contents = value
object
else
value
Expand Down
9 changes: 9 additions & 0 deletions spec/saxy/element_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@
object.bar.should == 2
end

it "should dump as object with value when attributes and contents are set" do
element.set_attribute("foo", "bar")
element.append_value("value")
object = element.as_object

object.foo.should == "bar"
object.contents.should == "value"
end

it "should add attributes under underscored names" do
element.set_attribute("FooBar", "baz")
element.as_object.foo_bar.should == "baz"
Expand Down

0 comments on commit a4bc4c9

Please sign in to comment.