Add support for Ruby 2.4's #transform_values(!) to IndifferentHash

This commit is contained in:
Mike Pastore 2018-05-02 17:39:13 -05:00
parent 92eeec07da
commit 453ecd9cab
2 changed files with 29 additions and 0 deletions

View File

@ -146,6 +146,17 @@ module Sinatra
super(other_hash.is_a?(self.class) ? other_hash : self.class[other_hash])
end
if method_defined?(:transform_values!) # Added in Ruby 2.4
def transform_values(&block)
dup.transform_values!(&block)
end
def transform_values!
super
super(&method(:convert_value))
end
end
private
def convert_key(key)

View File

@ -205,4 +205,22 @@ class TestIndifferentHash < Minitest::Test
@hash.replace(?a=>1, :q=>2)
assert_equal({ ?a=>1, ?q=>2 }, @hash)
end
def test_transform_values!
skip_if_lacking :transform_values!
@hash.transform_values! { |v| v.is_a?(Hash) ? Hash[v.to_a] : v }
assert_instance_of Sinatra::IndifferentHash, @hash[:simple_nested]
end
def test_transform_values
skip_if_lacking :transform_values
hash2 = @hash.transform_values { |v| v.respond_to?(:upcase) ? v.upcase : v }
refute_equal @hash, hash2
assert_equal :A, hash2[:a]
assert_equal :A, hash2[?a]
end
end