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

Instance Method Details

#add_repeated_lines(items) ⇒ Array<Array>

Maps a group of items to pairs of [item, repeated_lines]

Parameters:

  • items (Array)

    array of items that share the same key

Returns:

  • (Array<Array>)

    array of [item, repeated_lines] pairs



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

Parameters:

  • items (Enumerable)

    the filtered collection to group

  • key_proc (Proc)

    block returning the grouping key for each item

Returns:

  • (Array<Array>)

    array of groups containing more than one item that share the same key and there are multiple items in the group



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