aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/hash
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2010-01-17 03:26:20 +0530
committerPratik Naik <pratiknaik@gmail.com>2010-01-17 03:26:20 +0530
commitdba196cb7f8d34b93f6872e4a43737bb52019065 (patch)
tree97a2f784a2ec2bfae4f960af56a9280dad6f7774 /activesupport/lib/active_support/core_ext/hash
parent6e3bee6cf1f0d2684152292db0a8b757249824fd (diff)
downloadrails-dba196cb7f8d34b93f6872e4a43737bb52019065.tar.gz
rails-dba196cb7f8d34b93f6872e4a43737bb52019065.tar.bz2
rails-dba196cb7f8d34b93f6872e4a43737bb52019065.zip
Merge docrails
Diffstat (limited to 'activesupport/lib/active_support/core_ext/hash')
-rw-r--r--activesupport/lib/active_support/core_ext/hash/conversions.rb48
-rw-r--r--activesupport/lib/active_support/core_ext/hash/except.rb8
-rw-r--r--activesupport/lib/active_support/core_ext/hash/indifferent_access.rb5
-rw-r--r--activesupport/lib/active_support/core_ext/hash/keys.rb6
4 files changed, 65 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb
index cfd840fb93..48b185d05e 100644
--- a/activesupport/lib/active_support/core_ext/hash/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb
@@ -69,6 +69,54 @@ class Hash
)
end
+ # Returns a string containing an XML representation of its receiver:
+ #
+ # {"foo" => 1, "bar" => 2}.to_xml
+ # # =>
+ # # <?xml version="1.0" encoding="UTF-8"?>
+ # # <hash>
+ # # <foo type="integer">1</foo>
+ # # <bar type="integer">2</bar>
+ # # </hash>
+ #
+ # To do so, the method loops over the pairs and builds nodes that depend on
+ # the _values_. Given a pair +key+, +value+:
+ #
+ # * If +value+ is a hash there's a recursive call with +key+ as <tt>:root</tt>.
+ #
+ # * If +value+ is an array there's a recursive call with +key+ as <tt>:root</tt>,
+ # and +key+ singularized as <tt>:children</tt>.
+ #
+ # * If +value+ is a callable object it must expect one or two arguments. Depending
+ # on the arity, the callable is invoked with the +options+ hash as first argument
+ # with +key+ as <tt>:root</tt>, and +key+ singularized as second argument. Its
+ # return value becomes a new node.
+ #
+ # * If +value+ responds to +to_xml+ the method is invoked with +key+ as <tt>:root</tt>.
+ #
+ # * Otherwise, a node with +key+ as tag is created with a string representation of
+ # +value+ as text node. If +value+ is +nil+ an attribute "nil" set to "true" is added.
+ # Unless the option <tt>:skip_types</tt> exists and is true, an attribute "type" is
+ # added as well according to the following mapping:
+ #
+ # XML_TYPE_NAMES = {
+ # "Symbol" => "symbol",
+ # "Fixnum" => "integer",
+ # "Bignum" => "integer",
+ # "BigDecimal" => "decimal",
+ # "Float" => "float",
+ # "TrueClass" => "boolean",
+ # "FalseClass" => "boolean",
+ # "Date" => "date",
+ # "DateTime" => "datetime",
+ # "Time" => "datetime"
+ # }
+ #
+ # By default the root node is "hash", but that's configurable via the <tt>:root</tt> option.
+ #
+ # The default XML builder is a fresh instance of <tt>Builder::XmlMarkup</tt>. You can
+ # configure your own builder with the <tt>:builder</tt> option. The method also accepts
+ # options like <tt>:dasherize</tt> and friends, they are forwarded to the builder.
def to_xml(options = {})
require 'builder' unless defined?(Builder)
diff --git a/activesupport/lib/active_support/core_ext/hash/except.rb b/activesupport/lib/active_support/core_ext/hash/except.rb
index 6d04cb5621..207801d3a7 100644
--- a/activesupport/lib/active_support/core_ext/hash/except.rb
+++ b/activesupport/lib/active_support/core_ext/hash/except.rb
@@ -3,6 +3,14 @@ class Hash
# limiting a set of parameters to everything but a few known toggles:
#
# @person.update_attributes(params[:person].except(:admin))
+ #
+ # If the receiver responds to +convert_key+, the method is called on each of the
+ # arguments. This allows +except+ to play nice with hashes with indifferent access
+ # for instance:
+ #
+ # {:a => 1}.with_indifferent_access.except(:a) # => {}
+ # {:a => 1}.with_indifferent_access.except("a") # => {}
+ #
def except(*keys)
dup.except!(*keys)
end
diff --git a/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb b/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb
index b30e1602b6..0420e206af 100644
--- a/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb
+++ b/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb
@@ -1,6 +1,11 @@
require 'active_support/hash_with_indifferent_access'
class Hash
+
+ # Returns an +ActiveSupport::HashWithIndifferentAccess+ out of its receiver:
+ #
+ # {:a => 1}.with_indifferent_access["a"] # => 1
+ #
def with_indifferent_access
hash = ActiveSupport::HashWithIndifferentAccess.new(self)
hash.default = self.default
diff --git a/activesupport/lib/active_support/core_ext/hash/keys.rb b/activesupport/lib/active_support/core_ext/hash/keys.rb
index ffaa69570f..ecd63293b4 100644
--- a/activesupport/lib/active_support/core_ext/hash/keys.rb
+++ b/activesupport/lib/active_support/core_ext/hash/keys.rb
@@ -15,7 +15,8 @@ class Hash
self
end
- # Return a new hash with all keys converted to symbols.
+ # Return a new hash with all keys converted to symbols, as long as
+ # they respond to +to_sym+.
def symbolize_keys
inject({}) do |options, (key, value)|
options[(key.to_sym rescue key) || key] = value
@@ -23,7 +24,8 @@ class Hash
end
end
- # Destructively convert all keys to symbols.
+ # Destructively convert all keys to symbols, as long as they respond
+ # to +to_sym+.
def symbolize_keys!
self.replace(self.symbolize_keys)
end