Changeset 3830
- Timestamp:
- 03/09/06 23:35:43 (3 years ago)
- Files:
-
- trunk/activesupport/lib/active_support/core_ext/array/conversions.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/core_ext/hash/conversions.rb (modified) (1 diff)
- trunk/activesupport/test/core_ext/array_ext_test.rb (modified) (2 diffs)
- trunk/activesupport/test/core_ext/hash_ext_test.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activesupport/lib/active_support/core_ext/array/conversions.rb
r3829 r3830 30 30 raise "Not all elements respond to to_xml" unless all? { |e| e.respond_to? :to_xml } 31 31 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) 35 39 36 40 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 })) } } 39 42 end 40 43 end trunk/activesupport/lib/active_support/core_ext/hash/conversions.rb
r3829 r3830 25 25 value = self[key] 26 26 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] 31 34 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 ) 36 39 end 37 40 end trunk/activesupport/test/core_ext/array_ext_test.rb
r3829 r3830 76 76 ].to_xml(:skip_instruct => true, :indent => 0) 77 77 78 assert_equal "< hashes><hash>", xml.first(14)78 assert_equal "<records><record>", xml.first(17) 79 79 assert xml.include?(%(<age type="integer">26</age>)) 80 80 assert xml.include?(%(<name>David</name>)) 81 81 assert xml.include?(%(<age type="integer">31</age>)) 82 82 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) 83 91 end 84 92 … … 88 96 ].to_xml(:skip_instruct => true, :skip_types => true, :indent => 0) 89 97 90 assert_equal "< hashes><hash>", xml.first(14)98 assert_equal "<records><record>", xml.first(17) 91 99 assert xml.include?(%(<street-address>Paulina</street-address>)) 92 100 assert xml.include?(%(<name>David</name>)) trunk/activesupport/test/core_ext/hash_ext_test.rb
r3829 r3830 210 210 assert xml.include?(%(<name>David</name>)) 211 211 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 212 227 end