Skip to content

Commit befb79c

Browse files
committed
Avoid to set tag name from representer. Use as option instead.
1 parent fd67284 commit befb79c

3 files changed

Lines changed: 17 additions & 14 deletions

File tree

lib/representable/xml/binding.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ def serialize_for(value, parent)
3737
end
3838

3939
def serialize_node(node, value)
40-
return value if typed?
40+
if typed?
41+
value.name = as if value.is_a?(::Nokogiri::XML::Element) && as != '_self'
42+
return value
43+
end
4144

4245
node.content = value
4346
node
@@ -64,7 +67,7 @@ def xpath
6467
def find_nodes(doc)
6568
selector = xpath
6669
selector = "#{self[:wrap]}/#{xpath}" if self[:wrap]
67-
nodes = doc.xpath(selector)
70+
doc.xpath(selector)
6871
end
6972

7073
def node_for(parent, name)

test/parse_strategy_test.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ class ParseStrategySyncTest < BaseTest
1717

1818
representer!(:module => mod, :name => :song_representer) do
1919
property :title
20-
self.representation_wrap = :song if format == :xml
2120
end
2221

2322
representer!(:inject => :song_representer, :module => mod) do
24-
property :song, :parse_strategy => :sync, :extend => song_representer
23+
options = { :parse_strategy => :sync, :extend => song_representer }
24+
options[:as] = :song if format == :xml
25+
property :song, options
2526
end
2627

2728
let (:hit) { hit = OpenStruct.new(:song => song).extend(representer) }
@@ -46,19 +47,20 @@ class ParseStrategySyncTest < BaseTest
4647
for_formats(
4748
:hash => [Representable::Hash, {"songs"=>[{"title"=>"Resist Stance"}]}, {"songs"=>[{"title"=>"Suffer"}]}],
4849
#:json => [Representable::JSON, "{\"song\":{\"name\":\"Alive\"}}", "{\"song\":{\"name\":\"You've Taken Everything\"}}"],
49-
:xml => [Representable::XML, "<open_struct><song><title>Resist Stance</title></song></open_struct>", "<open_struct><songs><title>Suffer</title></songs></open_struct>"],
50+
:xml => [Representable::XML, "<open_struct><song><title>Resist Stance</title></song></open_struct>", "<open_struct><song><title>Suffer</title></song></open_struct>"],
5051
:yaml => [Representable::YAML, "---\nsongs:\n- title: Resist Stance\n", "---\nsongs:\n- title: Suffer\n"],
5152
) do |format, mod, output, input|
5253

5354
describe "[#{format}] collection with :parse_strategy: :sync" do # TODO: introduce :representable option?
5455
let (:format) { format }
5556
representer!(:module => mod, :name => :song_representer) do
5657
property :title
57-
self.representation_wrap = :song if format == :xml
5858
end
5959

6060
representer!(:inject => :song_representer, :module => mod) do
61-
collection :songs, :parse_strategy => :sync, :extend => song_representer
61+
options = { :parse_strategy => :sync, :extend => song_representer }
62+
options[:as] = :song if format == :xml
63+
collection :songs, options
6264
end
6365

6466
let (:album) { OpenStruct.new(:songs => [song]).extend(representer) }
@@ -71,12 +73,10 @@ class ParseStrategySyncTest < BaseTest
7173
collection_id = album.songs.object_id
7274
song = album.songs.first
7375
song_id = song.object_id
74-
7576
parse(album, input)
76-
7777
album.songs.first.title.must_equal "Suffer"
7878
song.title.must_equal "Suffer"
79-
#album.songs.object_id.must_equal collection_id # TODO: don't replace!
79+
album.songs.object_id.must_equal collection_id # TODO: don't replace!
8080
song.object_id.must_equal song_id
8181
end
8282
end

test/xml_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def name=(v)
160160
@album.extend(AlbumRepresenter)
161161

162162
assert_xml_equal "<album>
163-
<song><name>Mr. Charisma</name></song>
163+
<best_song><name>Mr. Charisma</name></best_song>
164164
<song><name>I Hate My Brain</name></song>
165165
<song><name>Mr. Charisma</name></song>
166166
</album>", @album.to_xml
@@ -277,9 +277,9 @@ def to_node(*)
277277
album = Album.new(band).extend(AlbumRepresenter)
278278

279279
assert_xml_equal %{<album>
280-
<c_data_band>
280+
<band>
281281
<name><![CDATA[Bad Religion]]></name>
282-
</c_data_band>
282+
</band>
283283
</album>}, album.to_xml
284284
end
285285
end
@@ -481,7 +481,7 @@ class XmlHashTest < MiniTest::Spec
481481

482482
describe "with objects" do
483483
representer!(module: Representable::XML) do
484-
hash :songs, class: OpenStruct do
484+
hash :songs, class: OpenStruct, as: :open_struct do
485485
property :title
486486
end
487487
end

0 commit comments

Comments
 (0)