Sha256: f98882d188870f7f9652a694b2eb7f664011e9e03a5cffc9830d8ffba6480a3a

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

module Mongoid
  module Matchers
    # TODO: Add documentation.
    def have_index_for *attrs
      HaveIndexMatcher.new(*attrs)
    end

    private

    class HaveIndexMatcher < Matcher
      attr_reader :attrs, :klass

      def initialize *attrs
        @attrs = attrs.map(&:to_sym)
      end

      def matches? subject
        @klass = class_of subject
        index_options.any? { |idx, _| idx.keys == attrs }
      end

      def failure_message
        "#{klass} to #{description}, but only found indexes #{indexes.inspect}"
      end

      def negative_failure_message
        "#{klass} to not #{description}, but found an index for #{attrs.inspect}"
      end

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

      private

      def index_options
        if Mongoid::VERSION.to_i < 4
          klass.index_options
        else
          Hash[klass.index_specifications.map{ |i| [i.key, i.options] }]
        end
      end

      def indexes
        index_options.keys.map(&:keys)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongoid-minitest-1.2.0 lib/matchers/document/have_index.rb
mongoid-minitest-1.1.0 lib/matchers/document/have_index.rb