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 12, 2015
1 parent fd67284 commit 6140b6f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ Gemfile.lock
*.swp
*.swo
bin
.idea
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
65 changes: 61 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 All @@ -500,4 +500,61 @@ class XmlHashTest < MiniTest::Spec
# FIXME: this NEVER worked!
# it { OpenStruct.new.extend(representer).from_xml(doc).songs.must_equal({"first" => "The Gargoyle", "second" => "Bronx"}) }
end
end

class XMLInheritenceTest < Minitest::Spec
class DateEntry
attr_accessor :value, :format
def initialize(value = nil)
@value = value
@format = 'YYYYMMDD'
end
end

class InvoiceEntry
attr_accessor :start_date, :end_date
def initialize(start_date = nil, end_date = nil)
@start_date = DateEntry.new(start_date) if start_date
@end_date = DateEntry.new(end_date) if end_date
end
end

class DateDecorator < ::Representable::Decorator
include Representable::XML
property :value, content: true
property :format, as: 'Format', attribute: true
end

class InvoiceDecorator < ::Representable::Decorator
include Representable::XML
property :start_date, decorator: DateDecorator, class: DateEntry
property :end_date, decorator: DateDecorator, class: DateEntry
end

let(:doc) {
<<-STR
<invoice_entry>
<start_date Format="YYYYMMDD">20141212</start_date>
<end_date Format="YYYYMMDD">20141216</end_date>
</invoice_entry>
STR
}

let(:invoice) {
InvoiceEntry.new('20141212', '20141216')
}

describe '#to_xml' do
it { InvoiceDecorator.new(invoice).to_xml.must_equal_xml doc }
end

describe '#from_xml' do
it 'should create proper entries' do
invoice_from_xml = InvoiceEntry.new
InvoiceDecorator.new(invoice_from_xml).from_xml(doc)
invoice_from_xml.start_date.value.must_equal '20141212'
invoice_from_xml.end_date.value.must_equal '20141216'
end
end

end

0 comments on commit 6140b6f

Please sign in to comment.