Add support for Ruby 2.5's #transform_keys(!) to IndifferentHash

This commit is contained in:
Mike Pastore 2018-05-02 17:40:02 -05:00
parent 453ecd9cab
commit 7d8e5be852
2 changed files with 32 additions and 0 deletions

View File

@ -157,6 +157,17 @@ module Sinatra
end
end
if method_defined?(:transform_keys!) # Added in Ruby 2.5
def transform_keys(&block)
dup.transform_keys!(&block)
end
def transform_keys!
super
super(&method(:convert_key))
end
end
private
def convert_key(key)

View File

@ -223,4 +223,25 @@ class TestIndifferentHash < Minitest::Test
assert_equal :A, hash2[:a]
assert_equal :A, hash2[?a]
end
def test_transform_keys!
skip_if_lacking :transform_keys!
@hash.transform_keys! { |k| k.respond_to?(:to_sym) ? k.to_sym : k }
assert_equal :a, @hash[:a]
assert_equal :a, @hash[?a]
end
def test_transform_keys
skip_if_lacking :transform_keys
hash2 = @hash.transform_keys { |k| k.respond_to?(:upcase) ? k.upcase : k }
refute_equal @hash, hash2
refute_operator hash2, :key?, :a
refute_operator hash2, :key?, ?a
assert_equal :a, hash2[:A]
assert_equal :a, hash2[?A]
end
end