Sha256: 18e0779bab56b465d318a40fee31e127b3fd1115c051dfd6df6d63797e5873d8

Contents?: true

Size: 1.54 KB

Versions: 4

Compression:

Stored size: 1.54 KB

Contents

module Mongoid
  module Matchers
    def have_index_for(index_key)
      HaveIndexFor.new(index_key)
    end

    class HaveIndexFor < Mongoid::Matchers::HaveIndexForBase
      def matches?(actual)
        @model = actual.is_a?(Class) ? actual : actual.class

        actual_index &&
          expected_index.key == actual_index.key &&
          expected_index.fields == actual_index.fields &&
          (expected_index.options.to_a - actual_index.options.to_a).empty?
      end

      def failure_message
        message = "Expected #{model.inspect} to #{description},"
        message << if actual_index.nil?
                     ' found no index'
                   else
                     " got #{index_description(actual_index)}"
                   end
        message
      end

      def failure_message_when_negated
        "Expected #{model.inspect} to not #{description}, got #{index_description(actual_index)}"
      end

      def description
        "have an index #{index_description(expected_index)}"
      end

      private

      def index_description(index)
        desc = index.key.inspect.to_s
        desc << " for fields #{index.fields.inspect}" if index.fields.present?
        desc << " including options #{index.options.inspect}" if index.options.present?
        desc
      end

      def expected_index
        @expected_index ||=
          Mongoid::Indexable::Specification.new(model, index_key, index_options)
      end

      def actual_index
        @actual_index ||= model.index_specification(expected_index.key)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mongoid-rspec-4.2.0 lib/matchers/indexes/v4/have_index_for.rb
mongoid-rspec-4.1.0 lib/matchers/indexes/v4/have_index_for.rb
mongoid-rspec-4.0.1 lib/matchers/indexes/v4/have_index_for.rb
mongoid-rspec-4.0.0 lib/matchers/indexes/v4/have_index_for.rb