Merge pull request #1405 from iguchi1124/support-ruby25-hash-slice

Support ruby2.5 Hash#slice to Sinatra::IndifferentHash
This commit is contained in:
namusyaka 2018-04-28 16:00:12 +09:00 committed by GitHub
commit 83a2a6951f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 0 deletions

View File

@ -104,6 +104,11 @@ module Sinatra
super(*keys.map(&method(:convert_key)))
end if method_defined?(:fetch_values) # Added in Ruby 2.3
def slice(*keys)
keys.map!(&method(:convert_key))
self.class[super(*keys)]
end if method_defined?(:slice) # Added in Ruby 2.5
def values_at(*keys)
super(*keys.map(&method(:convert_key)))
end

View File

@ -169,6 +169,18 @@ class TestIndifferentHash < Minitest::Test
assert_nil @hash.dig('nested', ?a, 0, :d)
end
def test_slice
skip_if_lacking :slice
assert_equal Sinatra::IndifferentHash[a: :a], @hash.slice(:a)
assert_equal Sinatra::IndifferentHash[b: :b], @hash.slice(?b)
assert_equal Sinatra::IndifferentHash[3 => 3], @hash.slice(3)
assert_equal Sinatra::IndifferentHash.new, @hash.slice(:d)
assert_equal Sinatra::IndifferentHash[a: :a, b: :b, 3 => 3], @hash.slice(:a, :b, 3)
assert_equal Sinatra::IndifferentHash[simple_nested: { a: :a, ?b => :b }], @hash.slice(:simple_nested)
assert_equal Sinatra::IndifferentHash[nested: { a: [{ a: :a, ?b => :b }, :c, 4], ?f => :f, 7 => 7 }], @hash.slice(:nested)
end
def test_fetch_values
skip_if_lacking :fetch_values