From dba196cb7f8d34b93f6872e4a43737bb52019065 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sun, 17 Jan 2010 03:26:20 +0530 Subject: Merge docrails --- .../active_support/core_ext/hash/conversions.rb | 48 ++++++++++++++++++++++ .../lib/active_support/core_ext/hash/except.rb | 8 ++++ .../core_ext/hash/indifferent_access.rb | 5 +++ .../lib/active_support/core_ext/hash/keys.rb | 6 ++- 4 files changed, 65 insertions(+), 2 deletions(-) (limited to 'activesupport/lib/active_support/core_ext/hash') 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 + # # => + # # + # # + # # 1 + # # 2 + # # + # + # 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 :root. + # + # * If +value+ is an array there's a recursive call with +key+ as :root, + # and +key+ singularized as :children. + # + # * 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 :root, 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 :root. + # + # * 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 :skip_types 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 :root option. + # + # The default XML builder is a fresh instance of Builder::XmlMarkup. You can + # configure your own builder with the :builder option. The method also accepts + # options like :dasherize 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 -- cgit v1.2.3