Sha256: fe8a389cbd1a664259d1c7eca3f0f9492098edb80fa8d64de2f9b5b0bd457b10

Contents?: true

Size: 823 Bytes

Versions: 3

Compression:

Stored size: 823 Bytes

Contents

module Mongoid
  module Matchers
    class HaveIndexMatcher < Matcher
      def initialize(field)
        @field = field.to_s
      end

      def matches?(subject)
        @klass = class_of(subject)

        @klass.index_options.each do |key, options|
          unless key[@field.to_sym] || key["#{@field}_id"]
            @error = "no index for #{@field.inspect}"

            return false
          end
        end

        true
      end

      def failure_message
        "#{@klass} to #{description}, got #{@error}"
      end

      def negative_failure_message
        "#{@klass} to not #{description}, got #{@klass} to #{description}"
      end

      def description
        "have an index for #{@field.inspect}"
      end
    end

    def have_index_for(field)
      HaveIndexMatcher.new(field)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mongoid-minitest-0.1.3 lib/matchers/document/have_index.rb
mongoid-minitest-0.1.3.pre lib/matchers/document/have_index.rb
mongoid-minitest-0.1.2 lib/matchers/document/have_index.rb