Skip to content

Commit

Permalink
Add #select, #reject and #compact methods to `Sinatra::Indiffer…
Browse files Browse the repository at this point in the history
…entHash` (#1711)
  • Loading branch information
ob-stripe committed Aug 17, 2021
1 parent 0a50b56 commit 7092f0e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/sinatra/indifferent_hash.rb
Expand Up @@ -180,6 +180,20 @@ def transform_keys!
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)
Expand Down
53 changes: 53 additions & 0 deletions test/indifferent_hash_test.rb
Expand Up @@ -262,4 +262,57 @@ def test_transform_keys
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

0 comments on commit 7092f0e

Please sign in to comment.