Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
[dm-serializer] Move child association serialization spec into shared…
Browse files Browse the repository at this point in the history
… spec so it is run against JSON and YAML as well, not just XML

[#829 state:resolved]
  • Loading branch information
xaviershay committed Jun 23, 2009
1 parent 0c04d3e commit 796b6f5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 43 deletions.
8 changes: 8 additions & 0 deletions dm-serializer/spec/lib/serialization_method_shared_spec.rb
Expand Up @@ -150,6 +150,14 @@
result = @harness.test(planet, :only => [:name], :exclude => [:name])
result.values_at("name", "aphelion").should == ["Mars", nil]
end

it 'should support child associations included via the :methods parameter' do
solar_system = SolarSystem.create(:name => "one")
planet = Planet.new(:name => "earth")
planet.solar_system = solar_system
result = @harness.test(planet, :methods => [:solar_system])
result['solar_system'].values_at('name', 'id').should == ['one', 1]
end
end

describe "(collections and proxies)" do
Expand Down
56 changes: 17 additions & 39 deletions dm-serializer/spec/public/to_xml_spec.rb
Expand Up @@ -27,29 +27,35 @@ def method_name
protected

def deserialize(result)
f = lambda {|element|
a = {}
element.elements.each do |e|
value =
if e.elements.size == 0
cast(e.text, e.attributes["type"])
else
f[e]
end
a.update(e.name => value)
end
a
}

doc = REXML::Document.new(result)
root = doc.elements[1]
if root.attributes["type"] == "array"
root.elements.collect do |element|
a = {}
element.elements.each do |v|
a.update(v.name => cast(v.text, v.attributes["type"]))
end
a
f[element]
end
else
a = {}
root.elements.each do |v|
a.update(v.name => cast(v.text, v.attributes["type"]))
end
a
f[root]
end
end

def cast(value, type)
boolean_conversions = {"true" => true, "false" => false}
value = boolean_conversions[value] if boolean_conversions.has_key?(value)
value = value.to_i if value && type == "integer"
value = value.to_i if value && ["integer", "datamapper::types::serial"].include?(type)
value
end
end.new
Expand All @@ -59,34 +65,6 @@ def cast(value, type)

it_should_behave_like "A serialization method"

def deserialize(xml)
doc = REXML::Document.new(xml)
{ doc.elements[1].name.to_sym => _deserialize(doc.elements[1])}
end


def _deserialize(element)
a = {}
element.elements.each do |e|
value =
if e.elements.size == 0
e.text
else
_deserialize(e)
end
a[e.name.to_sym] = value if value
end
a
end

it 'should' do
solar_system = SolarSystem.create(:name => "one")
planet = Planet.new(:name => "earth")
planet.solar_system = solar_system
xml = planet.to_xml(:methods => [:category, :solar_system])
deserialize(xml).should == {:planet=>{:solar_system_id=>"1", :category=>"terrestrial", :solar_system=>{:name=>"one", :id=>"1"}, :name=>"earth"}}
end

it "should not include the XML prologue, so that the result can be embedded in other XML documents" do
planet = Planet.new
xml = planet.to_xml(:element_name => "aplanet")
Expand Down
8 changes: 4 additions & 4 deletions dm-serializer/spec/public/to_yaml_spec.rb
Expand Up @@ -14,17 +14,17 @@ def method_name
end

def deserialize(result)
stringify_keys = lambda {|hash| hash.inject({}) {|a, (key, value)| a.update(key.to_s => value) }}
result = YAML.load(result)
(process = lambda {|object|
process = lambda {|object|
if object.is_a?(Array)
object.collect(&process)
elsif object.is_a?(Hash)
stringify_keys[object]
object.inject({}) {|a, (key, value)| a.update(key.to_s => process[value]) }
else
object
end
})[result]
}
process[result]
end
end.new
end
Expand Down

0 comments on commit 796b6f5

Please sign in to comment.