From e83a05af076637cc78f0408f5810d5f8f965e10c Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sat, 11 Jul 2009 16:20:58 -0500 Subject: Integrate AMo XML serializer into AR --- activemodel/lib/active_model/serializers/xml.rb | 49 ++++++++++++++----------- 1 file changed, 27 insertions(+), 22 deletions(-) (limited to 'activemodel') diff --git a/activemodel/lib/active_model/serializers/xml.rb b/activemodel/lib/active_model/serializers/xml.rb index d187859b44..7cdd281223 100644 --- a/activemodel/lib/active_model/serializers/xml.rb +++ b/activemodel/lib/active_model/serializers/xml.rb @@ -17,6 +17,15 @@ module ActiveModel @value = compute_value end + # There is a significant speed improvement if the value + # does not need to be escaped, as tag! escapes all values + # to ensure that valid XML is generated. For known binary + # values, it is at least an order of magnitude faster to + # Base64 encode binary values and directly put them in the + # output XML than to pass the original value or the Base64 + # encoded value to the tag! method. It definitely makes + # no sense to Base64 encode the value and then give it to + # tag!, since that just adds additional overhead. def needs_encoding? ![ :binary, :date, :datetime, :boolean, :float, :integer ].include?(type) end @@ -105,28 +114,6 @@ module ActiveModel end end - def add_attributes - (serializable_attributes + serializable_method_attributes).each do |attribute| - add_tag(attribute) - end - end - - def add_procs - if procs = options.delete(:procs) - [ *procs ].each do |proc| - proc.call(options) - end - end - end - - def add_tag(attribute) - builder.tag!( - reformat_name(attribute.name), - attribute.value.to_s, - attribute.decorations(!options[:skip_types]) - ) - end - def serialize args = [root] @@ -152,6 +139,24 @@ module ActiveModel name = name.camelize if camelize? dasherize? ? name.dasherize : name end + + def add_attributes + (serializable_attributes + serializable_method_attributes).each do |attribute| + builder.tag!( + reformat_name(attribute.name), + attribute.value.to_s, + attribute.decorations(!options[:skip_types]) + ) + end + end + + def add_procs + if procs = options.delete(:procs) + [ *procs ].each do |proc| + proc.call(options) + end + end + end end def to_xml(options = {}, &block) -- cgit v1.2.3