Skip to content

Commit

Permalink
added method in? to Object
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioRuiz committed Aug 28, 2020
1 parent 00e94fd commit 61bd815
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,17 @@ value.is_a?(Boolean) #> true
text.is_a?(Boolean) #> false

```

#### in?(array)
Added method in? to all objects that accepts array as a parameter.

```ruby
'uno'.in?(['uno','dos']) #> true
:uno.in? [:uno, :dos] #> true
5.in? [1,2,3,4,6] #> false

```

### Other tools integration

#### Tabulo
Expand Down
18 changes: 13 additions & 5 deletions lib/nice/hash/add_to_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,12 @@ def nice_filter(keys)
alias patterns pattern_fields
end

###########################################################################
# symbolize hash of arrays and array of hashes
# Taken from gist https://gist.github.com/Integralist/9503099
# Thanks to @integralist
###########################################################################
class Object
###########################################################################
# symbolize hash of arrays and array of hashes
# Taken from gist https://gist.github.com/Integralist/9503099
# Thanks to @integralist
###########################################################################
def deep_symbolize_keys
if is_a? Hash
return reduce({}) do |memo, (k, v)|
Expand All @@ -312,6 +312,14 @@ def deep_symbolize_keys
end
self
end

###########################################################################
# include? but the opposite. Check if the object is included on the array
###########################################################################
def in?(array)
array.include?(self)
end

end

class Array
Expand Down
4 changes: 2 additions & 2 deletions nice_hash.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'nice_hash'
s.version = '1.16.3'
s.version = '1.17.0'
s.summary = "NiceHash creates hashes following certain patterns so your testing will be much easier. Parse and filter JSON. Perfect to be used in test data factories"
s.description = "NiceHash creates hashes following certain patterns so your testing will be much easier. Parse and filter JSON. You can easily generate all the hashes you want following the criteria you specify. Many other features coming to Hash class like the methods 'bury' or select_key, access the keys like methods: my_hash.my_key.other_key. You will be able to generate thousands of different hashes just declaring one and test easily APIs based on JSON for example. Perfect to be used in test data factories"
s.authors = ["Mario Ruiz"]
Expand All @@ -11,7 +11,7 @@ Gem::Specification.new do |s|
s.license = 'MIT'
s.post_install_message = "Thanks for installing! Visit us on https://github.com/MarioRuiz/nice_hash"
s.add_runtime_dependency 'string_pattern', '~> 2.2', '>= 2.2.2'
s.add_development_dependency 'rspec', '~> 3.8', '>= 3.8.0'
s.add_development_dependency 'rspec', '~> 3.9', '>= 3.9.0'
s.test_files = s.files.grep(%r{^(test|spec|features)/})
end

12 changes: 12 additions & 0 deletions spec/nice_hash_generic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,16 @@
expect(hash).to eq({"uno"=>111, :dos=>222, :tres=>3})
end

it 'returns true if object in? array' do
expect('uno'.in?(['uno','dos'])).to be true
expect(:uno.in? [:uno, :dos]).to be true
expect(5.in? [1,2,3,4,5]).to be true
end

it 'returns false if object not in? array' do
expect('unox'.in?(['uno','dos'])).to be false
expect(:unox.in? [:uno, :dos]).to be false
expect(6.in? [1,2,3,4,5]).to be false
end

end

0 comments on commit 61bd815

Please sign in to comment.