diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2006-03-08 02:56:25 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2006-03-08 02:56:25 +0000 |
commit | cd989472a5248aae4f827c35347bdb871de1822a (patch) | |
tree | 13cad5fa4cd877788747930fe6076fcd01085a0f /activesupport/lib | |
parent | 24fca9d92ea9bef676f74259996c4039e014dfe2 (diff) | |
download | rails-cd989472a5248aae4f827c35347bdb871de1822a.tar.gz rails-cd989472a5248aae4f827c35347bdb871de1822a.tar.bz2 rails-cd989472a5248aae4f827c35347bdb871de1822a.zip |
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]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3812 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib')
6 files changed, 59 insertions, 2 deletions
diff --git a/activesupport/lib/active_support.rb b/activesupport/lib/active_support.rb index 7bd74101f5..a4c2462bdd 100644 --- a/activesupport/lib/active_support.rb +++ b/activesupport/lib/active_support.rb @@ -22,6 +22,9 @@ #++ $:.unshift(File.dirname(__FILE__)) +$:.unshift(File.dirname(__FILE__) + "/../vendor") + +require 'builder' require 'active_support/inflector' @@ -35,4 +38,4 @@ require 'active_support/option_merger' require 'active_support/values/time_zone' -require 'active_support/json' +require 'active_support/json'
\ No newline at end of file diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb index f119bb6c78..50777f9e1e 100644 --- a/activesupport/lib/active_support/core_ext/array/conversions.rb +++ b/activesupport/lib/active_support/core_ext/array/conversions.rb @@ -1,7 +1,6 @@ module ActiveSupport #:nodoc: module CoreExtensions #:nodoc: module Array #:nodoc: - # Enables to conversion of Arrays to human readable lists. ['one', 'two', 'three'] => "one, two, and three" module Conversions # Converts the array to comma-seperated sentence where the last element is joined by the connector word. Options: # * <tt>:connector</tt>: The word used to join the last element in arrays with more than two elements (default: "and") @@ -27,6 +26,12 @@ module ActiveSupport #:nodoc: join '/' end + def to_xml(options = {}) + raise "Not all elements respond to to_xml" unless all? { |e| e.respond_to? :to_xml } + options[:root] ||= all? { |e| e.is_a? first.class } ? first.class.to_s.underscore.pluralize : "records" + xml = options[:builder] || Builder::XmlMarkup.new + xml.__send__(options[:root]) { each { |e| e.to_xml(:builder => xml) } } + end end end end diff --git a/activesupport/lib/active_support/core_ext/hash.rb b/activesupport/lib/active_support/core_ext/hash.rb index f16368b18b..b8309baea3 100644 --- a/activesupport/lib/active_support/core_ext/hash.rb +++ b/activesupport/lib/active_support/core_ext/hash.rb @@ -1,9 +1,11 @@ require File.dirname(__FILE__) + '/hash/keys' require File.dirname(__FILE__) + '/hash/indifferent_access' require File.dirname(__FILE__) + '/hash/reverse_merge' +require File.dirname(__FILE__) + '/hash/conversions' class Hash #:nodoc: include ActiveSupport::CoreExtensions::Hash::Keys include ActiveSupport::CoreExtensions::Hash::IndifferentAccess include ActiveSupport::CoreExtensions::Hash::ReverseMerge + include ActiveSupport::CoreExtensions::Hash::Conversions end diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb new file mode 100644 index 0000000000..3218f6302c --- /dev/null +++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb @@ -0,0 +1,39 @@ +module ActiveSupport #:nodoc: + module CoreExtensions #:nodoc: + module Hash #:nodoc: + module Conversions + XML_TYPE_NAMES = { + "String" => "string", + "Fixnum" => "integer", + "Date" => "date", + "Time" => "datetime" + } + + XML_FORMATTING = { + "date" => Proc.new { |date| date.to_s(:db) }, + "datetime" => Proc.new { |time| time.to_s(:db) } + } + + def to_xml(options = {}) + options.reverse_merge!({ :builder => Builder::XmlMarkup.new, :root => "hash" }) + + options[:builder].__send__(options[:root]) do + for key in keys + value = self[key] + + if value.is_a?(self.class) + value.to_xml(:builder => options[:builder], :root => key) + else + type_name = XML_TYPE_NAMES[value.class.to_s] + options[:builder].__send__(key.to_s.dasherize, + XML_FORMATTING[type_name] ? XML_FORMATTING[type_name].call(value) : value, + value.nil? ? { } : { :type => type_name } + ) + end + end + end + end + end + end + end +end diff --git a/activesupport/lib/active_support/core_ext/string/inflections.rb b/activesupport/lib/active_support/core_ext/string/inflections.rb index 5d19702719..eef8a5bc89 100644 --- a/activesupport/lib/active_support/core_ext/string/inflections.rb +++ b/activesupport/lib/active_support/core_ext/string/inflections.rb @@ -26,6 +26,10 @@ module ActiveSupport #:nodoc: Inflector.underscore(self) end + def dasherize + Inflector.dasherize(self) + end + def demodulize Inflector.demodulize(self) end diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb index f91a73c7f3..bbc5e10579 100644 --- a/activesupport/lib/active_support/inflector.rb +++ b/activesupport/lib/active_support/inflector.rb @@ -120,6 +120,10 @@ module Inflector def underscore(camel_cased_word) camel_cased_word.to_s.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2').downcase end + + def dasherize(underscored_word) + underscored_word.gsub(/_/, '-') + end def humanize(lower_case_and_underscored_word) lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize |