Sha256: d95f7e18c53dda0096b95a1e61b4699820fcc23a7127a5005d92f408922c70ba

Contents?: true

Size: 1.49 KB

Versions: 6

Compression:

Stored size: 1.49 KB

Contents

module Mongoid
  module Matchers
    class HaveIndexForMatcher # :nodoc:
      def initialize(index_fields)
        @index_fields = index_fields
      end
      
      def with_options(options = {})
        @options = options
        self
      end
      
      def matches?(klass)
        @klass = klass.is_a?(Class) ? klass : klass.class
        @errors = []
        
        unless @klass.index_options[@index_fields]
          @errors.push "no index for #{@index_fields}"
        else
          if !@options.nil? && !@options.empty?
            @options.each do |option, option_value|
              if @klass.index_options[@index_fields].include?(option) && @klass.index_options[@index_fields][option] != option_value
                @errors.push "index for #{@index_fields.inspect} with options of #{@klass.index_options[@index_fields].inspect}"
              end
            end
          end
        end
        
        @errors.empty?
      end
      
      def failure_message_for_should
        "Expected #{@klass.inspect} to #{description}, got #{@errors.to_sentence}"
      end

      def failure_message_for_should_not
        "Expected #{@klass.inspect} to not #{description}, got #{@klass.inspect} to #{description}"
      end

      def description
        desc = "have an index for #{@index_fields.inspect}"
        desc << " with options of #{@options.inspect}" if @options
        desc
      end
    end
    
    def have_index_for(index_fields)
      HaveIndexForMatcher.new(index_fields)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
mongoid-rspec-1.5.4 lib/matchers/indexes.rb
mongoid-rspec-1.5.3 lib/matchers/indexes.rb
mongoid-rspec-1.5.1 lib/matchers/indexes.rb
mongoid-rspec-1.5.0 lib/matchers/indexes.rb
mongoid-rspec-1.4.6 lib/matchers/indexes.rb
mongoid-rspec-1.4.5 lib/matchers/indexes.rb