From 2c110b825e743f62c84a0c3e247ad524a9ac81c6 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 11 Feb 2005 13:05:38 +0000 Subject: [PATCH] Added IndifferentAccess as a way to wrap a hash by a symbol-based store that also can be accessed by string keys git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@581 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 2 + activesupport/CHANGELOG | 2 + activesupport/lib/core_ext/hash.rb | 2 + .../lib/core_ext/hash/indifferent_access.rb | 38 +++++++++++++++++++ activesupport/lib/core_ext/hash/keys.rb | 1 - activesupport/test/core_ext/hash_ext_test.rb | 12 +++++- 6 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 activesupport/lib/core_ext/hash/indifferent_access.rb diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 2a9ced6dc0..ce7e0a1e42 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fixed double requiring of models with the same name as the controller + * Fixed that query params could be forced to nil on a POST due to the raw post fix #562 [moriq@moriq.com] * Fixed that cookies shouldn't be frozen in TestRequest #571 [Eric Hodel] diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 15c9b5b6ba..dfe2639526 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,3 +1,5 @@ +* Added IndifferentAccess as a way to wrap a hash by a symbol-based store that also can be accessed by string keys + * Added Inflector.humanize to turn attribute names like employee_salary into "Employee salary". Used by automated error reporting in AR. * Added availability of class inheritable attributes to the masses #477 [bitsweat] diff --git a/activesupport/lib/core_ext/hash.rb b/activesupport/lib/core_ext/hash.rb index a5c53db1e0..e899d3e1e2 100644 --- a/activesupport/lib/core_ext/hash.rb +++ b/activesupport/lib/core_ext/hash.rb @@ -1,5 +1,7 @@ require File.dirname(__FILE__) + '/hash/keys' +require File.dirname(__FILE__) + '/hash/indifferent_access' class Hash #:nodoc: include ActiveSupport::CoreExtensions::Hash::Keys + include ActiveSupport::CoreExtensions::Hash::IndifferentAccess end diff --git a/activesupport/lib/core_ext/hash/indifferent_access.rb b/activesupport/lib/core_ext/hash/indifferent_access.rb new file mode 100644 index 0000000000..3fe0999866 --- /dev/null +++ b/activesupport/lib/core_ext/hash/indifferent_access.rb @@ -0,0 +1,38 @@ +class HashWithIndifferentAccess < Hash + def initialize(constructor) + if constructor.is_a?(Hash) + super() + update(constructor.symbolize_keys) + else + super(constructor) + end + end + + alias_method :regular_read, :[] + + def [](key) + case key + when Symbol: regular_read(key) || regular_read(key.to_s) + when String: regular_read(key) || regular_read(key.to_sym) + else regular_read(key) + end + end + + alias_method :regular_writer, :[]= + + def []=(key, value) + regular_writer(key.is_a?(String) ? key.to_sym : key, value) + end +end + +module ActiveSupport #:nodoc: + module CoreExtensions #:nodoc: + module Hash #:nodoc: + module IndifferentAccess + def with_indifferent_access + HashWithIndifferentAccess.new(self) + end + end + end + end +end diff --git a/activesupport/lib/core_ext/hash/keys.rb b/activesupport/lib/core_ext/hash/keys.rb index 536995dd71..4dd982337c 100644 --- a/activesupport/lib/core_ext/hash/keys.rb +++ b/activesupport/lib/core_ext/hash/keys.rb @@ -2,7 +2,6 @@ module ActiveSupport #:nodoc: module CoreExtensions #:nodoc: module Hash #:nodoc: module Keys - # Return a new hash with all keys converted to symbols. def symbolize_keys inject({}) do |options, (key, value)| diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index 52a28c1553..de85db2edf 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -2,7 +2,6 @@ require File.dirname(__FILE__) + '/../../lib/core_ext/hash' class HashExtTest < Test::Unit::TestCase - def setup @strings = { 'a' => 1, 'b' => 2 } @symbols = { :a => 1, :b => 2 } @@ -33,6 +32,17 @@ def test_symbolize_keys! assert_raises(NoMethodError) { { [] => 1 }.symbolize_keys! } end + def test_indifferent_access + @strings = @strings.with_indifferent_access + @symbols = @symbols.with_indifferent_access + @mixed = @mixed.with_indifferent_access + + assert_equal @strings[:a], @strings["a"] + assert_equal @symbols[:a], @symbols["a"] + assert_equal @strings["b"], @mixed["b"] + assert_equal @strings[:b], @mixed["b"] + end + def test_assert_valid_keys assert_nothing_raised do { :failure => "stuff", :funny => "business" }.assert_valid_keys([ :failure, :funny ]) -- GitLab