Changeset 8579
- Timestamp:
- 01/06/08 20:53:47 (9 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activesupport/lib/active_support/core_ext/hash/conversions.rb
r8546 r8579 25 25 end 26 26 27 # This module exists to decorate files deserialized using Hash.from_xml with 28 # the <tt>original_filename</tt> and <tt>content_type</tt> methods. 29 module FileLike #:nodoc: 30 attr_writer :original_filename, :content_type 31 32 def original_filename 33 @original_filename || 'untitled' 34 end 35 36 def content_type 37 @content_type || 'application/octet-stream' 38 end 39 end 40 27 41 module ActiveSupport #:nodoc: 28 42 module CoreExtensions #:nodoc: 29 43 module Hash #:nodoc: 30 44 module Conversions 45 31 46 XML_TYPE_NAMES = { 32 47 "Symbol" => "symbol", … … 64 79 "yaml" => Proc.new { |yaml| YAML::load(yaml) rescue yaml }, 65 80 "base64Binary" => Proc.new { |bin| Base64.decode64(bin) }, 66 # FIXME: Get rid of eval and institute a proper decorator here67 81 "file" => Proc.new do |file, entity| 68 82 f = StringIO.new(Base64.decode64(file)) 69 eval "def f.original_filename() '#{entity["name"]}' || 'untitled' end" 70 eval "def f.content_type() '#{entity["content_type"]}' || 'application/octet-stream' end" 83 f.extend(FileLike) 84 f.original_filename = entity['name'] 85 f.content_type = entity['content_type'] 71 86 f 72 87 end trunk/activesupport/test/core_ext/hash_ext_test.rb
r8563 r8579 534 534 topic_xml = <<-EOT 535 535 <rsp stat="ok"> 536 <photos page="1" pages="1" perpage="100" total="16">537 <photo id="175756086" owner="55569174@N00" secret="0279bf37a1" server="76" title="Colored Pencil PhotoBooth Fun" ispublic="1" isfriend="0" isfamily="0"/>538 </photos>536 <photos page="1" pages="1" perpage="100" total="16"> 537 <photo id="175756086" owner="55569174@N00" secret="0279bf37a1" server="76" title="Colored Pencil PhotoBooth Fun" ispublic="1" isfriend="0" isfamily="0"/> 538 </photos> 539 539 </rsp> 540 540 EOT … … 600 600 end 601 601 602 def test_file_from_xml 603 blog_xml = <<-XML 604 <blog> 605 <logo type="file" name="logo.png" content_type="image/png"> 606 </logo> 607 </blog> 608 XML 609 hash = Hash.from_xml(blog_xml) 610 assert hash.has_key?('blog') 611 assert hash['blog'].has_key?('logo') 612 613 file = hash['blog']['logo'] 614 assert_equal 'logo.png', file.original_filename 615 assert_equal 'image/png', file.content_type 616 end 617 618 def test_file_from_xml_with_defaults 619 blog_xml = <<-XML 620 <blog> 621 <logo type="file"> 622 </logo> 623 </blog> 624 XML 625 file = Hash.from_xml(blog_xml)['blog']['logo'] 626 assert_equal 'untitled', file.original_filename 627 assert_equal 'application/octet-stream', file.content_type 628 end 629 602 630 def test_xsd_like_types_from_xml 603 631 bacon_xml = <<-EOT