diff options
Diffstat (limited to 'activesupport/lib/active_support/core_ext')
4 files changed, 51 insertions, 1 deletions
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 |