Sha256: 651752ff76336641f3dc869f1d472aa23bb98677f1c1064ec3dc8437d0b66b1e

Contents?: true

Size: 1.01 KB

Versions: 11

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

module CustomCops
  #  This cop checks for the presence of Mongoid indexes that have not been
  #  flagged with the flag `background: true`.
  #
  # @example
  #   #bad
  #   index(reference: 1)
  #
  #   #good
  #   index({ reference: 1 }, { background: true })
  #
  class NoForegroundIndices < RuboCop::Cop::Base
    MSG = 'Do not create indices that lack the background flag.'

    def_node_matcher :model_index?, <<~PATTERN
      (send nil? :index $...)
    PATTERN

    def_node_matcher :hash?, <<~PATTERN
      (hash $...)
    PATTERN

    def_node_matcher :background_pair?, <<~PATTERN
      (pair
        (sym :background)
        (:true)
      )
    PATTERN

    def on_send(node)
      model_index?(node) do |_fields, options|
        add_offense(node.loc.selector) unless background_enabled?(options)
      end
    end

    private

    def background_enabled?(hash)
      return false if hash.nil? || !hash?(hash)

      hash.pairs.any? { |pair| background_pair?(pair) }
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
simplycop-2.12.1 lib/simplycop/custom_cops/no_foreground_indices.rb
simplycop-2.12.0 lib/simplycop/custom_cops/no_foreground_indices.rb
simplycop-2.11.1 lib/simplycop/custom_cops/no_foreground_indices.rb
simplycop-2.11.0 lib/simplycop/custom_cops/no_foreground_indices.rb
simplycop-2.10.0 lib/simplycop/custom_cops/no_foreground_indices.rb
simplycop-2.9.1 lib/simplycop/custom_cops/no_foreground_indices.rb
simplycop-2.9.0 lib/simplycop/custom_cops/no_foreground_indices.rb
simplycop-2.8.0 lib/simplycop/custom_cops/no_foreground_indices.rb
simplycop-2.7.2 lib/simplycop/custom_cops/no_foreground_indices.rb
simplycop-2.7.1 lib/simplycop/custom_cops/no_foreground_indices.rb
simplycop-2.7.0 lib/simplycop/custom_cops/no_foreground_indices.rb