Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support for assigning a node's content to an attribute.

  • Loading branch information...
commit 88891f63b22077563f6cb3aab2cf86ddaa374ba1 1 parent d7c79e0
@technicalpickles technicalpickles authored committed
View
9 lib/happymapper.rb
@@ -32,6 +32,11 @@ def element(name, type, options={})
@elements[to_s] << element
attr_accessor element.method_name.intern
end
+
+ def content(name)
+ @content = name
+ attr_accessor name
+ end
def elements
@elements[to_s] || []
@@ -94,6 +99,8 @@ def parse(xml, options = {})
obj.send("#{elem.method_name}=",
elem.from_xml_node(n, namespace))
end
+
+ obj.send("#{@content}=", n.content) if @content
obj
end
@@ -112,4 +119,4 @@ def parse(xml, options = {})
require 'happymapper/item'
require 'happymapper/attribute'
-require 'happymapper/element'
+require 'happymapper/element'
View
9 spec/fixtures/notes.xml
@@ -0,0 +1,9 @@
+<notes>
+ <note id='1132' title='My world!' created_at='2007-07-03 19:15:55'>
+ It's a pretty place
+ </note>
+ <note id='1133' title='Your world!' created_at='2007-07-03 19:20:00'>
+ Also pretty
+ </note>
+</notes>
+
View
29 spec/happymapper_spec.rb
@@ -270,6 +270,18 @@ class Commit
end
end
+module Backpack
+ class Note
+ include HappyMapper
+
+ attribute :id, Integer
+ attribute :title, String
+ attribute :created_at, Date
+
+ content :body
+ end
+end
+
describe HappyMapper do
describe "being included into another class" do
@@ -290,7 +302,7 @@ def self.to_s
it "should set @elements to a hash" do
@klass.elements.should == []
end
-
+
it "should allow adding an attribute" do
lambda {
@klass.attribute :name, String
@@ -540,6 +552,21 @@ module Bar; class Baz; include HappyMapper; end; end
last_event.address.zip.should == '327506398'
track.tran_detail.cust_tran_id.should == '20090102-111321'
end
+
+ it "should be able to parse from a node's content " do
+ notes = Backpack::Note.parse(fixture_file('notes.xml'))
+ notes.size.should == 2
+
+ note = notes[0]
+ note.id.should == 1132
+ note.title.should == 'My world!'
+ note.body.should include("It's a pretty place")
+
+ note = notes[1]
+ note.id.should == 1133
+ note.title.should == 'Your world!'
+ note.body.should include("Also pretty")
+ end
it "should be able to parse google analytics api xml" do
data = Analytics::Feed.parse(fixture_file('analytics.xml'))
Please sign in to comment.
Something went wrong with that request. Please try again.