aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/hash/conversions.rb
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2008-01-06 20:53:47 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2008-01-06 20:53:47 +0000
commit880081549d600a3d592241aadc8b2c6653a3d716 (patch)
tree84a6bcf7267b3724c4527228a0003df66cb1176d /activesupport/lib/active_support/core_ext/hash/conversions.rb
parenta1b0349362fd6c17af5aeff481996f6fac235828 (diff)
downloadrails-880081549d600a3d592241aadc8b2c6653a3d716.tar.gz
rails-880081549d600a3d592241aadc8b2c6653a3d716.tar.bz2
rails-880081549d600a3d592241aadc8b2c6653a3d716.zip
Use a decorator module for Files instantiated by Hash.from_xml. Add test coverage. Closes #10726 [Cheah Chu Yeow]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8579 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib/active_support/core_ext/hash/conversions.rb')
-rw-r--r--activesupport/lib/active_support/core_ext/hash/conversions.rb21
1 files changed, 18 insertions, 3 deletions
diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb
index f6ebb90400..8e80f6e4ff 100644
--- a/activesupport/lib/active_support/core_ext/hash/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb
@@ -24,10 +24,25 @@ class XmlSimple
end
end
+# This module exists to decorate files deserialized using Hash.from_xml with
+# the <tt>original_filename</tt> and <tt>content_type</tt> methods.
+module FileLike #:nodoc:
+ attr_writer :original_filename, :content_type
+
+ def original_filename
+ @original_filename || 'untitled'
+ end
+
+ def content_type
+ @content_type || 'application/octet-stream'
+ end
+end
+
module ActiveSupport #:nodoc:
module CoreExtensions #:nodoc:
module Hash #:nodoc:
module Conversions
+
XML_TYPE_NAMES = {
"Symbol" => "symbol",
"Fixnum" => "integer",
@@ -63,11 +78,11 @@ module ActiveSupport #:nodoc:
"string" => Proc.new { |string| string.to_s },
"yaml" => Proc.new { |yaml| YAML::load(yaml) rescue yaml },
"base64Binary" => Proc.new { |bin| Base64.decode64(bin) },
- # FIXME: Get rid of eval and institute a proper decorator here
"file" => Proc.new do |file, entity|
f = StringIO.new(Base64.decode64(file))
- eval "def f.original_filename() '#{entity["name"]}' || 'untitled' end"
- eval "def f.content_type() '#{entity["content_type"]}' || 'application/octet-stream' end"
+ f.extend(FileLike)
+ f.original_filename = entity['name']
+ f.content_type = entity['content_type']
f
end
}