Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 8579

Show
Ignore:
Timestamp:
01/06/08 20:53:47 (9 months ago)
Author:
bitsweat
Message:

Use a decorator module for Files instantiated by Hash.from_xml. Add test coverage. Closes #10726 [Cheah Chu Yeow]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/lib/active_support/core_ext/hash/conversions.rb

    r8546 r8579  
    2525end 
    2626 
     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. 
     29module 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 
     39end 
     40 
    2741module ActiveSupport #:nodoc: 
    2842  module CoreExtensions #:nodoc: 
    2943    module Hash #:nodoc: 
    3044      module Conversions 
     45 
    3146        XML_TYPE_NAMES = { 
    3247          "Symbol"     => "symbol", 
     
    6479            "yaml"         => Proc.new  { |yaml|    YAML::load(yaml) rescue yaml }, 
    6580            "base64Binary" => Proc.new  { |bin|     Base64.decode64(bin) }, 
    66             # FIXME: Get rid of eval and institute a proper decorator here 
    6781            "file"         => Proc.new do |file, entity| 
    6882              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'] 
    7186              f 
    7287            end 
  • trunk/activesupport/test/core_ext/hash_ext_test.rb

    r8563 r8579  
    534534    topic_xml = <<-EOT 
    535535    <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> 
    539539    </rsp> 
    540540    EOT 
     
    600600  end 
    601601 
     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 
    602630  def test_xsd_like_types_from_xml 
    603631    bacon_xml = <<-EOT