Module: RuboCop::Cop::RSpec::RepeatedItems
- Included in:
- RepeatedExample, RepeatedExampleGroupBody, RepeatedExampleGroupDescription, RepeatedIncludeExample, ScatteredSetup
- Defined in:
- lib/rubocop/cop/rspec/mixin/repeated_items.rb
Overview
Helps find repeated items in a collection
Provides a generic method to find repeated items by grouping them by a key and returning pairs of [item, repeated_lines] for items that appear more than once.
Instance Method Summary collapse
-
#add_repeated_lines(items) ⇒ Array<Array>
Maps a group of items to pairs of [item, repeated_lines].
-
#find_repeated_groups(items, key_proc:) ⇒ Array<Array>
Groups items by key and returns only groups with more than one item.
Instance Method Details
#add_repeated_lines(items) ⇒ Array<Array>
Maps a group of items to pairs of [item, repeated_lines]
29 30 31 32 |
# File 'lib/rubocop/cop/rspec/mixin/repeated_items.rb', line 29 def add_repeated_lines(items) repeated_lines = items.map(&:first_line) items.map { |item| [item, repeated_lines - [item.first_line]] } end |
#find_repeated_groups(items, key_proc:) ⇒ Array<Array>
Groups items by key and returns only groups with more than one item
18 19 20 21 22 23 |
# File 'lib/rubocop/cop/rspec/mixin/repeated_items.rb', line 18 def find_repeated_groups(items, key_proc:) items .group_by(&key_proc) .values .reject(&:one?) end |