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

Changeset 3812

Show
Ignore:
Timestamp:
03/08/06 02:56:25 (3 years ago)
Author:
david
Message:

Added Hash#to_xml and Array#to_xml that makes it much easier to produce XML from basic structures [DHH] Moved Jim Weirich's wonderful Builder from Action Pack to Active Support (it's simply too useful to be stuck in AP) [DHH]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/CHANGELOG

    r3739 r3812  
    11*SVN* 
     2 
     3* Added Hash#to_xml and Array#to_xml that makes it much easier to produce XML from basic structures [DHH]. Examples: 
     4 
     5    { :name => "David", :street_name => "Paulina", :age => 26, :moved_on => Date.new(2005, 11, 15) }.to_xml 
     6     
     7  ...returns: 
     8 
     9      <person> 
     10        <street-name type="string">Paulina</street> 
     11        <name type="string">David</name> 
     12        <age type="integer">26</age> 
     13        <moved-on type="date">2005-11-15</moved-on> 
     14      </person> 
     15 
     16* Moved Jim Weirich's wonderful Builder from Action Pack to Active Support (it's simply too useful to be stuck in AP) [DHH] 
    217 
    318* Fixed that Array#to_sentence will return "" on an empty array instead of ", and" #3842, #4031 [rubyonrails@beautifulpixel.com] 
  • trunk/activesupport/lib/active_support.rb

    r3493 r3812  
    2323 
    2424$:.unshift(File.dirname(__FILE__)) 
     25$:.unshift(File.dirname(__FILE__) + "/../vendor") 
     26 
     27require 'builder' 
    2528 
    2629require 'active_support/inflector' 
  • trunk/activesupport/lib/active_support/core_ext/array/conversions.rb

    r3802 r3812  
    22  module CoreExtensions #:nodoc: 
    33    module Array #:nodoc: 
    4       # Enables to conversion of Arrays to human readable lists. ['one', 'two', 'three'] => "one, two, and three" 
    54      module Conversions 
    65        # Converts the array to comma-seperated sentence where the last element is joined by the connector word. Options: 
     
    2827        end 
    2928         
     29        def to_xml(options = {}) 
     30          raise "Not all elements respond to to_xml" unless all? { |e| e.respond_to? :to_xml } 
     31          options[:root] ||= all? { |e| e.is_a? first.class } ? first.class.to_s.underscore.pluralize : "records" 
     32          xml = options[:builder] || Builder::XmlMarkup.new 
     33          xml.__send__(options[:root]) { each { |e| e.to_xml(:builder => xml) } } 
     34        end 
    3035      end 
    3136    end 
  • trunk/activesupport/lib/active_support/core_ext/hash.rb

    r2192 r3812  
    22require File.dirname(__FILE__) + '/hash/indifferent_access' 
    33require File.dirname(__FILE__) + '/hash/reverse_merge' 
     4require File.dirname(__FILE__) + '/hash/conversions' 
    45 
    56class Hash #:nodoc: 
     
    78  include ActiveSupport::CoreExtensions::Hash::IndifferentAccess 
    89  include ActiveSupport::CoreExtensions::Hash::ReverseMerge 
     10  include ActiveSupport::CoreExtensions::Hash::Conversions 
    911end 
  • trunk/activesupport/lib/active_support/core_ext/string/inflections.rb

    r3127 r3812  
    2525        def underscore 
    2626          Inflector.underscore(self) 
     27        end 
     28 
     29        def dasherize 
     30          Inflector.dasherize(self) 
    2731        end 
    2832 
  • trunk/activesupport/lib/active_support/inflector.rb

    r3571 r3812  
    121121    camel_cased_word.to_s.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2').downcase 
    122122  end 
     123   
     124  def dasherize(underscored_word) 
     125    underscored_word.gsub(/_/, '-') 
     126  end 
    123127 
    124128  def humanize(lower_case_and_underscored_word) 
  • trunk/activesupport/Rakefile

    r3656 r3812  
    1919  t.pattern = 'test/**/*_test.rb' 
    2020  t.verbose = true 
    21   t.warning = tru
     21  t.warning = fals
    2222} 
    2323 
  • trunk/activesupport/test/core_ext/array_ext_test.rb

    r3803 r3812  
    6868    assert_equal [%w(a b c), %w(d e f), ['g', false, false]], groups 
    6969  end 
     70end 
    7071 
     72class ArraToXmlTests < Test::Unit::TestCase 
     73  def test_to_xml 
     74    a = [ { :name => "David", :street_address => "Paulina" }, { :name => "Jason", :street_address => "Evergreen" } ] 
     75    assert_equal( 
     76      "<hashes><hash><street-address type=\"string\">Paulina</street-address><name type=\"string\">David</name></hash><hash><street-address type=\"string\">Evergreen</street-address><name type=\"string\">Jason</name></hash></hashes>", 
     77      a.to_xml 
     78    ) 
     79  end 
    7180end 
  • trunk/activesupport/test/core_ext/hash_ext_test.rb

    r3468 r3812  
    11require 'test/unit' 
    2 require File.dirname(__FILE__) + '/../../lib/active_support/core_ext/hash
     2require File.dirname(__FILE__) + '/../../lib/active_support
    33 
    44class HashExtTest < Test::Unit::TestCase 
     
    166166  end 
    167167end 
     168 
     169class HashToXmlTest < Test::Unit::TestCase 
     170  def test_one_level 
     171    h = { :name => "David", :street => "Paulina" } 
     172    assert_equal %(<person><street type="string">Paulina</street><name type="string">David</name></person>), h.to_xml(:root => :person) 
     173  end 
     174 
     175  def test_one_level_with_types 
     176    h = { :name => "David", :street => "Paulina", :age => 26, :moved_on => Date.new(2005, 11, 15) } 
     177    assert_equal( 
     178      "<person><street type=\"string\">Paulina</street><name type=\"string\">David</name><age type=\"integer\">26</age><moved-on type=\"date\">2005-11-15</moved-on></person>",  
     179      h.to_xml(:root => :person) 
     180    ) 
     181  end 
     182 
     183  def test_one_level_with_nils 
     184    h = { :name => "David", :street => "Paulina", :age => nil } 
     185    assert_equal( 
     186      "<person><street type=\"string\">Paulina</street><name type=\"string\">David</name><age></age></person>",  
     187      h.to_xml(:root => :person) 
     188    ) 
     189  end 
     190 
     191  def test_two_levels 
     192    h = { :name => "David", :address => { :street => "Paulina" } } 
     193    assert_equal %(<person><address><street type="string">Paulina</street></address><name type="string">David</name></person>), h.to_xml(:root => :person) 
     194  end 
     195end 
  • trunk/activesupport/test/inflector_test.rb

    r3571 r3812  
    188188    "1001" => "1001st" 
    189189  } 
     190   
     191  UnderscoresToDashes = { 
     192    "street"                => "street", 
     193    "street_address"        => "street-address", 
     194    "person_street_address" => "person-street-address" 
     195  } 
    190196 
    191197  def test_pluralize_plurals 
     
    291297    end 
    292298  end 
     299 
     300  def test_dasherize 
     301    UnderscoresToDashes.each do |underscored, dasherized| 
     302      assert_equal(dasherized, Inflector.dasherize(underscored)) 
     303    end 
     304  end 
    293305end