Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Added Hash#to_xml and Array#to_xml that makes it much easier to produ…
…ce 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
- Loading branch information
Showing
16 changed files
with
126 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
39 changes: 39 additions & 0 deletions
39
activesupport/lib/active_support/core_ext/hash/conversions.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.