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

Changeset 3830

Show
Ignore:
Timestamp:
03/09/06 23:35:43 (3 years ago)
Author:
david
Message:

Implemented nested to_xml and named elements for array xmling [DHH]

Files:

Legend:

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

    r3829 r3830  
    3030          raise "Not all elements respond to to_xml" unless all? { |e| e.respond_to? :to_xml } 
    3131 
    32           options[:root]    ||= all? { |e| e.is_a? first.class } ? first.class.to_s.underscore.pluralize : "records" 
    33           options[:indent]  ||= 2 
    34           options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent]) 
     32          options[:root]     ||= all? { |e| e.is_a?(first.class) && first.class.to_s != "Hash" } ? first.class.to_s.underscore.pluralize : "records" 
     33          options[:children] ||= options[:root].singularize 
     34          options[:indent]   ||= 2 
     35          options[:builder]  ||= Builder::XmlMarkup.new(:indent => options[:indent]) 
     36 
     37          root     = options.delete(:root) 
     38          children = options.delete(:children) 
    3539 
    3640          options[:builder].instruct! unless options.delete(:skip_instruct) 
    37           root = options.delete(:root) 
    38           options[:builder].__send__(root) { each { |e| e.to_xml(options.merge({ :skip_instruct => true })) } } 
     41          options[:builder].__send__(root) { each { |e| e.to_xml(options.merge({ :skip_instruct => true, :root => children })) } } 
    3942        end 
    4043      end 
  • trunk/activesupport/lib/active_support/core_ext/hash/conversions.rb

    r3829 r3830  
    2525              value = self[key] 
    2626 
    27               if value.is_a?(self.class) 
    28                 value.to_xml(options.merge({ :root => key, :skip_instruct => true })) 
    29               else 
    30                 type_name = XML_TYPE_NAMES[value.class.to_s] 
     27              case value.class.to_s # TODO: Figure out why I have to to_s the class to do comparisons in order for tests to run 
     28                when "Hash" 
     29                  value.to_xml(options.merge({ :root => key, :skip_instruct => true })) 
     30                when "Array" 
     31                  value.to_xml(options.merge({ :root => key, :children => key.to_s.singularize, :skip_instruct => true})) 
     32                else 
     33                  type_name = XML_TYPE_NAMES[value.class.to_s] 
    3134 
    32                 options[:builder].__send__(key.to_s.dasherize,  
    33                   XML_FORMATTING[type_name] ? XML_FORMATTING[type_name].call(value) : value, 
    34                   options[:skip_types] || value.nil? || type_name.nil? ? { } : { :type => type_name } 
    35                
     35                  options[:builder].__send__(key.to_s.dasherize,  
     36                    XML_FORMATTING[type_name] ? XML_FORMATTING[type_name].call(value) : value, 
     37                    options[:skip_types] || value.nil? || type_name.nil? ? { } : { :type => type_name } 
     38                 
    3639              end 
    3740            end 
  • trunk/activesupport/test/core_ext/array_ext_test.rb

    r3829 r3830  
    7676    ].to_xml(:skip_instruct => true, :indent => 0) 
    7777 
    78     assert_equal "<hashes><hash>", xml.first(14
     78    assert_equal "<records><record>", xml.first(17
    7979    assert xml.include?(%(<age type="integer">26</age>)) 
    8080    assert xml.include?(%(<name>David</name>)) 
    8181    assert xml.include?(%(<age type="integer">31</age>)) 
    8282    assert xml.include?(%(<name>Jason</name>)) 
     83  end 
     84 
     85  def test_to_xml_with_dedicated_name 
     86    xml = [ 
     87      { :name => "David", :age => 26 }, { :name => "Jason", :age => 31 } 
     88    ].to_xml(:skip_instruct => true, :indent => 0, :root => "people") 
     89 
     90    assert_equal "<people><person>", xml.first(16) 
    8391  end 
    8492   
     
    8896    ].to_xml(:skip_instruct => true, :skip_types => true, :indent => 0) 
    8997 
    90     assert_equal "<hashes><hash>", xml.first(14
     98    assert_equal "<records><record>", xml.first(17
    9199    assert xml.include?(%(<street-address>Paulina</street-address>)) 
    92100    assert xml.include?(%(<name>David</name>)) 
  • trunk/activesupport/test/core_ext/hash_ext_test.rb

    r3829 r3830  
    210210    assert xml.include?(%(<name>David</name>)) 
    211211  end 
     212   
     213  def test_two_levels_with_array 
     214    xml = { :name => "David", :addresses => [{ :street => "Paulina" }, { :street => "Evergreen" }] }.to_xml(@xml_options) 
     215    assert_equal "<person>", xml.first(8) 
     216    assert xml.include?(%(<addresses><address>)) 
     217    assert xml.include?(%(<address><street>Paulina</street></address>)) 
     218    assert xml.include?(%(<address><street>Evergreen</street></address>)) 
     219    assert xml.include?(%(<name>David</name>)) 
     220  end 
     221 
     222   
     223  def test_three_levels_with_array 
     224    xml = { :name => "David", :addresses => [{ :streets => [ { :name => "Paulina" }, { :name => "Paulina" } ] } ] }.to_xml(@xml_options) 
     225    assert xml.include?(%(<addresses><address><streets><street><name>)) 
     226  end 
    212227end