aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_controller/cgi_ext/cgi_methods.rb
diff options
context:
space:
mode:
authorJamis Buck <jamis@37signals.com>2006-03-18 05:07:27 +0000
committerJamis Buck <jamis@37signals.com>2006-03-18 05:07:27 +0000
commit459559a8bced28f25888802e40c7182392ea94cc (patch)
tree729a49dc50c7461a31368a164eabd5bff0a77dd2 /actionpack/lib/action_controller/cgi_ext/cgi_methods.rb
parent93f8bd15a11d5292885d585669a61a7364d1a1b0 (diff)
downloadrails-459559a8bced28f25888802e40c7182392ea94cc.tar.gz
rails-459559a8bced28f25888802e40c7182392ea94cc.tar.bz2
rails-459559a8bced28f25888802e40c7182392ea94cc.zip
XML-formatted requests are typecast according to "type" attributes for :xml_simple
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3915 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_controller/cgi_ext/cgi_methods.rb')
-rwxr-xr-xactionpack/lib/action_controller/cgi_ext/cgi_methods.rb44
1 files changed, 43 insertions, 1 deletions
diff --git a/actionpack/lib/action_controller/cgi_ext/cgi_methods.rb b/actionpack/lib/action_controller/cgi_ext/cgi_methods.rb
index 776f74a100..f19e70839d 100755
--- a/actionpack/lib/action_controller/cgi_ext/cgi_methods.rb
+++ b/actionpack/lib/action_controller/cgi_ext/cgi_methods.rb
@@ -63,7 +63,11 @@ class CGIMethods #:nodoc:
when Proc
strategy.call(raw_post_data)
when :xml_simple
- XmlSimple.xml_in(raw_post_data, 'ForceArray' => false, 'keeproot' => true)
+ typecast_xml_value(XmlSimple.xml_in(raw_post_data,
+ 'forcearray' => false,
+ 'forcecontent' => true,
+ 'keeproot' => true,
+ 'contentkey' => '__content__'))
when :yaml
YAML.load(raw_post_data)
when :xml_node
@@ -77,8 +81,46 @@ class CGIMethods #:nodoc:
"raw_post_data" => raw_post_data, "format" => mime_type }
end
+ def self.typecast_xml_value(value)
+ case value
+ when Hash
+ if value.has_key?("__content__")
+ content = translate_xml_entities(value["__content__"])
+ case value["type"]
+ when "integer" then content.to_i
+ when "boolean" then content == "true"
+ when "datetime" then Time.parse(content)
+ when "date" then Date.parse(content)
+ else content
+ end
+ else
+ value.empty? ? nil : value.inject({}) do |h,(k,v)|
+ h[k] = typecast_xml_value(v)
+ h
+ end
+ end
+ when Array
+ value.map! { |i| typecast_xml_value(i) }
+ case value.length
+ when 0 then nil
+ when 1 then value.first
+ else value
+ end
+ else
+ raise "can't typecast #{value.inspect}"
+ end
+ end
+
private
+ def self.translate_xml_entities(value)
+ value.gsub(/&lt;/, "<").
+ gsub(/&gt;/, ">").
+ gsub(/&quot;/, '"').
+ gsub(/&apos;/, "'").
+ gsub(/&amp;/, "&")
+ end
+
def self.dasherize_keys(params)
case params.class.to_s
when "Hash"