Permalink
Browse files

type convertes can specify a format method to convert back to string

  • Loading branch information...
craigambrose committed May 14, 2012
1 parent 29c4ef3 commit 2a4d9eed09b86a592242b74997bd9233d6508195
@@ -15,19 +15,25 @@ def map_value_onto_object(object, value)
end
end
- def value_from_object(object)
- if object.respond_to?(@name) && !Object.new.respond_to?(@name)
- object.send(@name)
- else
- object[@name]
- end
+ def string_value_from_object(object)
+ result = raw_value_from_object(object)
+ result = @parser.format(result) if @parser && @parser.respond_to?(:format)
+ result.to_s
end
def find_or_insert_node(doc, base)
find_or_insert_nested_node(doc, base, path_parts)
end
private
+ def raw_value_from_object(object)
+ if object.respond_to?(@name) && !Object.new.respond_to?(@name)
+ object.send(@name)
+ else
+ object[@name]
+ end
+ end
+
def setter_method
"#{@name}=".to_sym
end
@@ -5,7 +5,7 @@ module Internal
module Mappings
class ElementAttribute < Element
def update_parent_node(builder, doc, parent, object)
- parent[base_attribute_name] = value_from_object(object).to_s
+ parent[base_attribute_name] = string_value_from_object(object)
parent
end
@@ -11,11 +11,11 @@ def initialize(name, options = {})
def update_parent_node(builder, doc, parent, object)
node = find_or_insert_child_element(doc, parent, path_parts.last)
- value = value_from_object(object)
+ value = string_value_from_object(object)
if @cdata
- node.add_child(Nokogiri::XML::CDATA.new(doc, value.to_s))
+ node.add_child(Nokogiri::XML::CDATA.new(doc, value))
else
- node.content = value.to_s
+ node.content = value
end
node
end
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<propertyList>
- <business modTime="somedate: 2010-08-02-13:25">
+ <business modTime="2010-08-02-13:25">
<officeDetails>
<officeName><![CDATA[Sydney Premier Real Estate]]></officeName>
<addressStreet><![CDATA[2/8]]></addressStreet>
@@ -37,17 +37,17 @@
<img id="z"/>
</images>
</business>
- <residential modTime="somedate: 2010-06-09-09:15"/>
- <residential modTime="somedate: 2010-06-07-08:13"/>
- <residential modTime="somedate: 2010-06-10-07:35"/>
- <residential modTime="somedate: 2010-06-08-09:54"/>
- <residential modTime="somedate: 2010-06-09-08:28"/>
- <residential modTime="somedate: 2010-06-10-10:46"/>
- <residential modTime="somedate: 2010-06-07-08:13"/>
- <residential modTime="somedate: 2010-06-07-08:14"/>
- <residential modTime="somedate: 2010-06-08-15:55"/>
- <residential modTime="somedate: 2010-06-03-15:58"/>
- <residential modTime="somedate: 2010-06-04-12:40"/>
- <residential modTime="somedate: 2010-06-04-07:52"/>
- <residential modTime="somedate: 2010-06-08-13:29"/>
+ <residential modTime="2010-06-09-09:15"/>
+ <residential modTime="2010-06-07-08:13"/>
+ <residential modTime="2010-06-10-07:35"/>
+ <residential modTime="2010-06-08-09:54"/>
+ <residential modTime="2010-06-09-08:28"/>
+ <residential modTime="2010-06-10-10:46"/>
+ <residential modTime="2010-06-07-08:13"/>
+ <residential modTime="2010-06-07-08:14"/>
+ <residential modTime="2010-06-08-15:55"/>
+ <residential modTime="2010-06-03-15:58"/>
+ <residential modTime="2010-06-04-12:40"/>
+ <residential modTime="2010-06-04-07:52"/>
+ <residential modTime="2010-06-08-13:29"/>
</propertyList>
@@ -43,6 +43,10 @@ class ReaxmlDateTime
def self.parse(string)
"somedate: #{string}"
end
+
+ def self.format(value)
+ value.sub(/^somedate\: /, '')
+ end
end
class Agent
@@ -117,7 +121,7 @@ class PropertyList
parser = SaxStream::Parser.new(collector, [PropertyList])
parser.parse_stream(open_fixture(:reaxml))
business = collector.mapped_objects.first
- business.to_xml.should == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<business modTime=\"somedate: 2010-08-02-13:25\">\n <officeDetails>\n <officeName><![CDATA[Sydney Premier Real Estate]]></officeName>\n <addressStreet><![CDATA[2/8]]></addressStreet>\n </officeDetails>\n <listingAgent>\n <name>Sonia Hume</name>\n </listingAgent>\n <images>\n <img id=\"m\"/>\n <img id=\"a\"/>\n <img id=\"b\"/>\n <img id=\"c\"/>\n <img id=\"d\"/>\n <img id=\"e\"/>\n <img id=\"f\"/>\n <img id=\"g\"/>\n <img id=\"h\"/>\n <img id=\"i\"/>\n <img id=\"j\"/>\n <img id=\"k\"/>\n <img id=\"l\"/>\n <img id=\"n\"/>\n <img id=\"o\"/>\n <img id=\"p\"/>\n <img id=\"q\"/>\n <img id=\"r\"/>\n <img id=\"s\"/>\n <img id=\"t\"/>\n <img id=\"u\"/>\n <img id=\"v\"/>\n <img id=\"w\"/>\n <img id=\"x\"/>\n <img id=\"y\"/>\n <img id=\"z\"/>\n </images>\n</business>\n"
+ business.to_xml.should == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<business modTime=\"2010-08-02-13:25\">\n <officeDetails>\n <officeName><![CDATA[Sydney Premier Real Estate]]></officeName>\n <addressStreet><![CDATA[2/8]]></addressStreet>\n </officeDetails>\n <listingAgent>\n <name>Sonia Hume</name>\n </listingAgent>\n <images>\n <img id=\"m\"/>\n <img id=\"a\"/>\n <img id=\"b\"/>\n <img id=\"c\"/>\n <img id=\"d\"/>\n <img id=\"e\"/>\n <img id=\"f\"/>\n <img id=\"g\"/>\n <img id=\"h\"/>\n <img id=\"i\"/>\n <img id=\"j\"/>\n <img id=\"k\"/>\n <img id=\"l\"/>\n <img id=\"n\"/>\n <img id=\"o\"/>\n <img id=\"p\"/>\n <img id=\"q\"/>\n <img id=\"r\"/>\n <img id=\"s\"/>\n <img id=\"t\"/>\n <img id=\"u\"/>\n <img id=\"v\"/>\n <img id=\"w\"/>\n <img id=\"x\"/>\n <img id=\"y\"/>\n <img id=\"z\"/>\n </images>\n</business>\n"
end
it "builds the XML for a property list" do
@@ -11,13 +11,13 @@ module Internal
def attribute_mapping(path, value)
Mappings::ElementAttribute.new(path.gsub(/^@/, ''), :to => path).tap do |result|
- result.stub!(:value_from_object).with(object).and_return(value)
+ result.stub!(:string_value_from_object).with(object).and_return(value)
end
end
def element_mapping(path, value)
Mappings::ElementContent.new(path.split('/').first, :to => path).tap do |result|
- result.stub!(:value_from_object).with(object).and_return(value)
+ result.stub!(:string_value_from_object).with(object).and_return(value)
end
end

0 comments on commit 2a4d9ee

Please sign in to comment.