Changeset 3812
- Timestamp:
- 03/08/06 02:56:25 (3 years ago)
- Files:
-
- trunk/activesupport/CHANGELOG (modified) (1 diff)
- trunk/activesupport/lib/active_support.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/core_ext/array/conversions.rb (modified) (2 diffs)
- trunk/activesupport/lib/active_support/core_ext/hash.rb (modified) (2 diffs)
- trunk/activesupport/lib/active_support/core_ext/hash/conversions.rb (added)
- trunk/activesupport/lib/active_support/core_ext/string/inflections.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/inflector.rb (modified) (1 diff)
- trunk/activesupport/Rakefile (modified) (1 diff)
- trunk/activesupport/test/core_ext/array_ext_test.rb (modified) (1 diff)
- trunk/activesupport/test/core_ext/hash_ext_test.rb (modified) (2 diffs)
- trunk/activesupport/test/inflector_test.rb (modified) (2 diffs)
- trunk/activesupport/vendor (moved) (moved from trunk/actionpack/lib/action_view/vendor)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activesupport/CHANGELOG
r3739 r3812 1 1 *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] 2 17 3 18 * 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 23 23 24 24 $:.unshift(File.dirname(__FILE__)) 25 $:.unshift(File.dirname(__FILE__) + "/../vendor") 26 27 require 'builder' 25 28 26 29 require 'active_support/inflector' trunk/activesupport/lib/active_support/core_ext/array/conversions.rb
r3802 r3812 2 2 module CoreExtensions #:nodoc: 3 3 module Array #:nodoc: 4 # Enables to conversion of Arrays to human readable lists. ['one', 'two', 'three'] => "one, two, and three"5 4 module Conversions 6 5 # Converts the array to comma-seperated sentence where the last element is joined by the connector word. Options: … … 28 27 end 29 28 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 30 35 end 31 36 end trunk/activesupport/lib/active_support/core_ext/hash.rb
r2192 r3812 2 2 require File.dirname(__FILE__) + '/hash/indifferent_access' 3 3 require File.dirname(__FILE__) + '/hash/reverse_merge' 4 require File.dirname(__FILE__) + '/hash/conversions' 4 5 5 6 class Hash #:nodoc: … … 7 8 include ActiveSupport::CoreExtensions::Hash::IndifferentAccess 8 9 include ActiveSupport::CoreExtensions::Hash::ReverseMerge 10 include ActiveSupport::CoreExtensions::Hash::Conversions 9 11 end trunk/activesupport/lib/active_support/core_ext/string/inflections.rb
r3127 r3812 25 25 def underscore 26 26 Inflector.underscore(self) 27 end 28 29 def dasherize 30 Inflector.dasherize(self) 27 31 end 28 32 trunk/activesupport/lib/active_support/inflector.rb
r3571 r3812 121 121 camel_cased_word.to_s.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2').downcase 122 122 end 123 124 def dasherize(underscored_word) 125 underscored_word.gsub(/_/, '-') 126 end 123 127 124 128 def humanize(lower_case_and_underscored_word) trunk/activesupport/Rakefile
r3656 r3812 19 19 t.pattern = 'test/**/*_test.rb' 20 20 t.verbose = true 21 t.warning = true21 t.warning = false 22 22 } 23 23 trunk/activesupport/test/core_ext/array_ext_test.rb
r3803 r3812 68 68 assert_equal [%w(a b c), %w(d e f), ['g', false, false]], groups 69 69 end 70 end 70 71 72 class 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 71 80 end trunk/activesupport/test/core_ext/hash_ext_test.rb
r3468 r3812 1 1 require 'test/unit' 2 require File.dirname(__FILE__) + '/../../lib/active_support /core_ext/hash'2 require File.dirname(__FILE__) + '/../../lib/active_support' 3 3 4 4 class HashExtTest < Test::Unit::TestCase … … 166 166 end 167 167 end 168 169 class 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 195 end trunk/activesupport/test/inflector_test.rb
r3571 r3812 188 188 "1001" => "1001st" 189 189 } 190 191 UnderscoresToDashes = { 192 "street" => "street", 193 "street_address" => "street-address", 194 "person_street_address" => "person-street-address" 195 } 190 196 191 197 def test_pluralize_plurals … … 291 297 end 292 298 end 299 300 def test_dasherize 301 UnderscoresToDashes.each do |underscored, dasherized| 302 assert_equal(dasherized, Inflector.dasherize(underscored)) 303 end 304 end 293 305 end