From c7cc9583689d63d342983d739ccf5c4e94233a48 Mon Sep 17 00:00:00 2001 From: Josh Franklin Date: Fri, 26 Mar 2010 12:43:59 +0100 Subject: Add support for a type=binary with an optional encoding=base64. If the encoding attribute is absent, the data is considered unencoded. [#2966 state:resolved] --- activesupport/lib/active_support/core_ext/hash/conversions.rb | 9 +++++++++ activesupport/test/core_ext/hash_ext_test.rb | 4 +++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb index 48b185d05e..c882434f78 100644 --- a/activesupport/lib/active_support/core_ext/hash/conversions.rb +++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb @@ -54,6 +54,15 @@ class Hash "string" => Proc.new { |string| string.to_s }, "yaml" => Proc.new { |yaml| YAML::load(yaml) rescue yaml }, "base64Binary" => Proc.new { |bin| ActiveSupport::Base64.decode64(bin) }, + "binary" => Proc.new do |bin, entity| + case entity['encoding'] + when 'base64' + ActiveSupport::Base64.decode64(bin) + # TODO: Add support for other encodings + else + bin + end + end, "file" => Proc.new do |file, entity| f = StringIO.new(ActiveSupport::Base64.decode64(file)) f.extend(FileLike) diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index 5b1d53ac7b..86272a28c1 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -757,6 +757,7 @@ class HashToXmlTest < Test::Unit::TestCase 2007-12-25T12:34:56+0000 YmFiZS5wbmc= + VGhhdCdsbCBkbywgcGlnLg== EOT @@ -766,7 +767,8 @@ class HashToXmlTest < Test::Unit::TestCase :price => BigDecimal("12.50"), :expires_at => Time.utc(2007,12,25,12,34,56), :notes => "", - :illustration => "babe.png" + :illustration => "babe.png", + :caption => "That'll do, pig." }.stringify_keys assert_equal expected_bacon_hash, Hash.from_xml(bacon_xml)["bacon"] -- cgit v1.2.3