1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Namespace HashWithIndifferentAccess

This commit is contained in:
Akira Matsuda 2013-01-07 06:00:10 +09:00
parent 033248b5fb
commit c8e632bd9f
11 changed files with 17 additions and 19 deletions

View file

@ -228,7 +228,7 @@ module ActionController
end end
def decode_credentials(header) def decode_credentials(header)
HashWithIndifferentAccess[header.to_s.gsub(/^Digest\s+/,'').split(',').map do |pair| ActiveSupport::HashWithIndifferentAccess[header.to_s.gsub(/^Digest\s+/, '').split(',').map do |pair|
key, value = pair.split('=', 2) key, value = pair.split('=', 2)
[key.strip, value.to_s.gsub(/^"|"$/,'').delete('\'')] [key.strip, value.to_s.gsub(/^"|"$/,'').delete('\'')]
end] end]

View file

@ -72,7 +72,7 @@ module ActionDispatch
end end
end end
# Convert nested Hash to HashWithIndifferentAccess # Convert nested Hash to ActiveSupport::HashWithIndifferentAccess
def normalize_parameters(value) def normalize_parameters(value)
case value case value
when Hash when Hash

View file

@ -75,7 +75,7 @@ module ActionDispatch
end end
module Upload # :nodoc: module Upload # :nodoc:
# Convert nested Hash to HashWithIndifferentAccess and replace # Convert nested Hash to ActiveSupport::HashWithIndifferentAccess and replace
# file upload hash with UploadedFile objects # file upload hash with UploadedFile objects
def normalize_parameters(value) def normalize_parameters(value)
if Hash === value && value.has_key?(:tempfile) if Hash === value && value.has_key?(:tempfile)

View file

@ -2,7 +2,6 @@ require 'abstract_unit'
module AbstractController module AbstractController
module Testing module Testing
class UrlForTest < ActionController::TestCase class UrlForTest < ActionController::TestCase
class W class W
include ActionDispatch::Routing::RouteSet.new.tap { |r| r.draw { get ':controller(/:action(/:id(.:format)))' } }.url_helpers include ActionDispatch::Routing::RouteSet.new.tap { |r| r.draw { get ':controller(/:action(/:id(.:format)))' } }.url_helpers
@ -350,10 +349,10 @@ module AbstractController
def test_with_hash_with_indifferent_access def test_with_hash_with_indifferent_access
W.default_url_options[:controller] = 'd' W.default_url_options[:controller] = 'd'
W.default_url_options[:only_path] = false W.default_url_options[:only_path] = false
assert_equal("/c", W.new.url_for(HashWithIndifferentAccess.new('controller' => 'c', 'only_path' => true))) assert_equal("/c", W.new.url_for(ActiveSupport::HashWithIndifferentAccess.new('controller' => 'c', 'only_path' => true)))
W.default_url_options[:action] = 'b' W.default_url_options[:action] = 'b'
assert_equal("/c/a", W.new.url_for(HashWithIndifferentAccess.new('controller' => 'c', 'action' => 'a', 'only_path' => true))) assert_equal("/c/a", W.new.url_for(ActiveSupport::HashWithIndifferentAccess.new('controller' => 'c', 'action' => 'a', 'only_path' => true)))
end end
def test_url_params_with_nil_to_param_are_not_in_url def test_url_params_with_nil_to_param_are_not_in_url

View file

@ -119,7 +119,7 @@ module ActiveModel
# person.name = 'bob' # person.name = 'bob'
# person.changes # => { "name" => ["bill", "bob"] } # person.changes # => { "name" => ["bill", "bob"] }
def changes def changes
HashWithIndifferentAccess[changed.map { |attr| [attr, attribute_change(attr)] }] ActiveSupport::HashWithIndifferentAccess[changed.map { |attr| [attr, attribute_change(attr)] }]
end end
# Returns a hash of attributes that were changed before the model was saved. # Returns a hash of attributes that were changed before the model was saved.

View file

@ -107,7 +107,7 @@ module ActiveRecord
private private
def initialize_store_attribute(store_attribute) def initialize_store_attribute(store_attribute)
attribute = send(store_attribute) attribute = send(store_attribute)
unless attribute.is_a?(HashWithIndifferentAccess) unless attribute.is_a?(ActiveSupport::HashWithIndifferentAccess)
attribute = IndifferentCoder.as_indifferent_hash(attribute) attribute = IndifferentCoder.as_indifferent_hash(attribute)
send :"#{store_attribute}=", attribute send :"#{store_attribute}=", attribute
end end
@ -134,12 +134,12 @@ module ActiveRecord
def self.as_indifferent_hash(obj) def self.as_indifferent_hash(obj)
case obj case obj
when HashWithIndifferentAccess when ActiveSupport::HashWithIndifferentAccess
obj obj
when Hash when Hash
obj.with_indifferent_access obj.with_indifferent_access
else else
HashWithIndifferentAccess.new ActiveSupport::HashWithIndifferentAccess.new
end end
end end
end end

View file

@ -311,7 +311,7 @@ class TestNestedAttributesOnAHasOneAssociation < ActiveRecord::TestCase
end end
def test_should_also_work_with_a_HashWithIndifferentAccess def test_should_also_work_with_a_HashWithIndifferentAccess
@pirate.ship_attributes = HashWithIndifferentAccess.new(:id => @ship.id, :name => 'Davy Jones Gold Dagger') @pirate.ship_attributes = ActiveSupport::HashWithIndifferentAccess.new(:id => @ship.id, :name => 'Davy Jones Gold Dagger')
assert @pirate.ship.persisted? assert @pirate.ship.persisted?
assert_equal 'Davy Jones Gold Dagger', @pirate.ship.name assert_equal 'Davy Jones Gold Dagger', @pirate.ship.name
@ -593,7 +593,7 @@ module NestedAttributesOnACollectionAssociationTests
end end
def test_should_also_work_with_a_HashWithIndifferentAccess def test_should_also_work_with_a_HashWithIndifferentAccess
@pirate.send(association_setter, HashWithIndifferentAccess.new('foo' => HashWithIndifferentAccess.new(:id => @child_1.id, :name => 'Grace OMalley'))) @pirate.send(association_setter, ActiveSupport::HashWithIndifferentAccess.new('foo' => ActiveSupport::HashWithIndifferentAccess.new(:id => @child_1.id, :name => 'Grace OMalley')))
@pirate.save @pirate.save
assert_equal 'Grace OMalley', @child_1.reload.name assert_equal 'Grace OMalley', @child_1.reload.name
end end

View file

@ -67,9 +67,9 @@ class StoreTest < ActiveRecord::TestCase
end end
test "preserve store attributes data in HashWithIndifferentAccess format without any conversion" do test "preserve store attributes data in HashWithIndifferentAccess format without any conversion" do
@john.json_data = HashWithIndifferentAccess.new(:height => 'tall', 'weight' => 'heavy') @john.json_data = ActiveSupport::HashWithIndifferentAccess.new(:height => 'tall', 'weight' => 'heavy')
@john.height = 'low' @john.height = 'low'
assert_equal true, @john.json_data.instance_of?(HashWithIndifferentAccess) assert_equal true, @john.json_data.instance_of?(ActiveSupport::HashWithIndifferentAccess)
assert_equal 'low', @john.json_data[:height] assert_equal 'low', @john.json_data[:height]
assert_equal 'low', @john.json_data['height'] assert_equal 'low', @john.json_data['height']
assert_equal 'heavy', @john.json_data[:weight] assert_equal 'heavy', @john.json_data[:weight]
@ -95,7 +95,7 @@ class StoreTest < ActiveRecord::TestCase
test "convert store attributes from any format other than Hash or HashWithIndifferent access losing the data" do test "convert store attributes from any format other than Hash or HashWithIndifferent access losing the data" do
@john.json_data = "somedata" @john.json_data = "somedata"
@john.height = 'low' @john.height = 'low'
assert_equal true, @john.json_data.instance_of?(HashWithIndifferentAccess) assert_equal true, @john.json_data.instance_of?(ActiveSupport::HashWithIndifferentAccess)
assert_equal 'low', @john.json_data[:height] assert_equal 'low', @john.json_data[:height]
assert_equal 'low', @john.json_data['height'] assert_equal 'low', @john.json_data['height']
assert_equal false, @john.json_data.delete_if { |k, v| k == 'height' }.any? assert_equal false, @john.json_data.delete_if { |k, v| k == 'height' }.any?
@ -143,5 +143,4 @@ class StoreTest < ActiveRecord::TestCase
assert_raise(NoMethodError) { @john.stored_attributes = Hash.new } assert_raise(NoMethodError) { @john.stored_attributes = Hash.new }
assert_raise(NoMethodError) { @john.stored_attributes } assert_raise(NoMethodError) { @john.stored_attributes }
end end
end end

View file

@ -8,7 +8,7 @@ require 'active_support/core_ext/object/deep_dup'
require 'active_support/inflections' require 'active_support/inflections'
class HashExtTest < ActiveSupport::TestCase class HashExtTest < ActiveSupport::TestCase
class IndifferentHash < HashWithIndifferentAccess class IndifferentHash < ActiveSupport::HashWithIndifferentAccess
end end
class SubclassingArray < Array class SubclassingArray < Array

View file

@ -114,7 +114,7 @@ To send a hash you include the key name inside the brackets:
When this form is submitted, the value of `params[:client]` will be `{"name" => "Acme", "phone" => "12345", "address" => {"postcode" => "12345", "city" => "Carrot City"}}`. Note the nested hash in `params[:client][:address]`. When this form is submitted, the value of `params[:client]` will be `{"name" => "Acme", "phone" => "12345", "address" => {"postcode" => "12345", "city" => "Carrot City"}}`. Note the nested hash in `params[:client][:address]`.
Note that the `params` hash is actually an instance of `HashWithIndifferentAccess` from Active Support, which acts like a hash that lets you use symbols and strings interchangeably as keys. Note that the `params` hash is actually an instance of `ActiveSupport::HashWithIndifferentAccess`, which acts like a hash that lets you use symbols and strings interchangeably as keys.
### JSON/XML parameters ### JSON/XML parameters

View file

@ -417,7 +417,7 @@ def create
end end
``` ```
The `render` method here is taking a very simple hash with a key of `text` and value of `params[:post].inspect`. The `params` method is the object which represents the parameters (or fields) coming in from the form. The `params` method returns a `HashWithIndifferentAccess` object, which allows you to access the keys of the hash using either strings or symbols. In this situation, the only parameters that matter are the ones from the form. The `render` method here is taking a very simple hash with a key of `text` and value of `params[:post].inspect`. The `params` method is the object which represents the parameters (or fields) coming in from the form. The `params` method returns an `ActiveSupport::HashWithIndifferentAccess` object, which allows you to access the keys of the hash using either strings or symbols. In this situation, the only parameters that matter are the ones from the form.
If you re-submit the form one more time you'll now no longer get the missing template error. Instead, you'll see something that looks like the following: If you re-submit the form one more time you'll now no longer get the missing template error. Instead, you'll see something that looks like the following: