Skip to content

Commit

Permalink
Avoid to set tag name from representer. Use as option instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonoff committed Jun 11, 2015
1 parent fd67284 commit befb79c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
7 changes: 5 additions & 2 deletions lib/representable/xml/binding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ def serialize_for(value, parent)
end

def serialize_node(node, value)
return value if typed?
if typed?
value.name = as if value.is_a?(::Nokogiri::XML::Element) && as != '_self'
return value
end

node.content = value
node
Expand All @@ -64,7 +67,7 @@ def xpath
def find_nodes(doc)
selector = xpath
selector = "#{self[:wrap]}/#{xpath}" if self[:wrap]
nodes = doc.xpath(selector)
doc.xpath(selector)
end

def node_for(parent, name)
Expand Down
16 changes: 8 additions & 8 deletions test/parse_strategy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ class ParseStrategySyncTest < BaseTest

representer!(:module => mod, :name => :song_representer) do
property :title
self.representation_wrap = :song if format == :xml
end

representer!(:inject => :song_representer, :module => mod) do
property :song, :parse_strategy => :sync, :extend => song_representer
options = { :parse_strategy => :sync, :extend => song_representer }
options[:as] = :song if format == :xml
property :song, options
end

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

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

representer!(:inject => :song_representer, :module => mod) do
collection :songs, :parse_strategy => :sync, :extend => song_representer
options = { :parse_strategy => :sync, :extend => song_representer }
options[:as] = :song if format == :xml
collection :songs, options
end

let (:album) { OpenStruct.new(:songs => [song]).extend(representer) }
Expand All @@ -71,12 +73,10 @@ class ParseStrategySyncTest < BaseTest
collection_id = album.songs.object_id
song = album.songs.first
song_id = song.object_id

parse(album, input)

album.songs.first.title.must_equal "Suffer"
song.title.must_equal "Suffer"
#album.songs.object_id.must_equal collection_id # TODO: don't replace!
album.songs.object_id.must_equal collection_id # TODO: don't replace!
song.object_id.must_equal song_id
end
end
Expand Down
8 changes: 4 additions & 4 deletions test/xml_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def name=(v)
@album.extend(AlbumRepresenter)

assert_xml_equal "<album>
<song><name>Mr. Charisma</name></song>
<best_song><name>Mr. Charisma</name></best_song>
<song><name>I Hate My Brain</name></song>
<song><name>Mr. Charisma</name></song>
</album>", @album.to_xml
Expand Down Expand Up @@ -277,9 +277,9 @@ def to_node(*)
album = Album.new(band).extend(AlbumRepresenter)

assert_xml_equal %{<album>
<c_data_band>
<band>
<name><![CDATA[Bad Religion]]></name>
</c_data_band>
</band>
</album>}, album.to_xml
end
end
Expand Down Expand Up @@ -481,7 +481,7 @@ class XmlHashTest < MiniTest::Spec

describe "with objects" do
representer!(module: Representable::XML) do
hash :songs, class: OpenStruct do
hash :songs, class: OpenStruct, as: :open_struct do
property :title
end
end
Expand Down

0 comments on commit befb79c

Please sign in to comment.