1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00

Add #select, #reject and #compact methods to Sinatra::IndifferentHash (#1711)

This commit is contained in:
Olivier Bellone 2021-08-17 07:37:32 -07:00 committed by GitHub
parent 0a50b5608d
commit 7092f0e0a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 0 deletions

View file

@ -180,6 +180,20 @@ module Sinatra
end
end
def select(*args, &block)
return to_enum(:select) unless block_given?
dup.tap { |hash| hash.select!(*args, &block) }
end
def reject(*args, &block)
return to_enum(:reject) unless block_given?
dup.tap { |hash| hash.reject!(*args, &block) }
end
def compact
dup.tap(&:compact!)
end if method_defined?(:compact) # Added in Ruby 2.4
private
def convert_key(key)

View file

@ -262,4 +262,57 @@ class TestIndifferentHash < Minitest::Test
assert_equal :a, hash2[:A]
assert_equal :a, hash2[?A]
end
def test_select
hash = @hash.select { |k, v| v == :a }
assert_equal Sinatra::IndifferentHash[a: :a], hash
assert_instance_of Sinatra::IndifferentHash, hash
hash2 = @hash.select { |k, v| true }
assert_equal @hash, hash2
assert_instance_of Sinatra::IndifferentHash, hash2
enum = @hash.select
assert_instance_of Enumerator, enum
end
def test_select!
@hash.select! { |k, v| v == :a }
assert_equal Sinatra::IndifferentHash[a: :a], @hash
end
def test_reject
hash = @hash.reject { |k, v| v != :a }
assert_equal Sinatra::IndifferentHash[a: :a], hash
assert_instance_of Sinatra::IndifferentHash, hash
hash2 = @hash.reject { |k, v| false }
assert_equal @hash, hash2
assert_instance_of Sinatra::IndifferentHash, hash2
enum = @hash.reject
assert_instance_of Enumerator, enum
end
def test_reject!
@hash.reject! { |k, v| v != :a }
assert_equal Sinatra::IndifferentHash[a: :a], @hash
end
def test_compact
skip_if_lacking :compact
hash_with_nil_values = @hash.merge({?z => nil})
compacted_hash = hash_with_nil_values.compact
assert_equal @hash, compacted_hash
assert_instance_of Sinatra::IndifferentHash, compacted_hash
empty_hash = Sinatra::IndifferentHash.new
compacted_hash = empty_hash.compact
assert_equal empty_hash, compacted_hash
non_empty_hash = Sinatra::IndifferentHash[a: :a]
compacted_hash = non_empty_hash.compact
assert_equal non_empty_hash, compacted_hash
end
end