brainspec / enumerize

Enumerated attributes with I18n and ActiveRecord/Mongoid support
MIT License
1.73k stars 190 forks source link

Add `Enumerize::Attribute#value?` method #408

Closed koic closed 2 years ago

koic commented 2 years ago

This PR adds Enumerize::Attribute#value? method.

Summary

This new method prevents the following breakage when autocorrecting with Performance/InefficientHashSearch cop.

-enumerize_attr.values.include?('attr')
+enumerize_attr.value?('attr') #=> ArgumentError: wrong number of arguments (given 2, expected 1)

The intention is to add a compatible API for the above, as it does not actually improve performance.

And this method is also used in Enumerize::Predicates#predicate_method?, so the enumerize's test also calls it.

Other Information

About Performance/InefficientHashSearch cop:

# bad
{ a: 1, b: 2 }.values.include?('garbage')

# good
{ a: 1, b: 2 }.value?('garbage')

https://docs.rubocop.org/rubocop-performance/1.14/cops_performance.html#performanceinefficienthashsearch

nashby commented 2 years ago

@koic thanks!