Class: Mongoid::Matchers::HaveIndexForMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/matchers/indexes.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(index_fields) ⇒ HaveIndexForMatcher

Returns a new instance of HaveIndexForMatcher.



4
5
6
# File 'lib/matchers/indexes.rb', line 4

def initialize(index_fields)
  @index_fields = index_fields.symbolize_keys!
end

Instance Method Details

#descriptionObject



45
46
47
48
49
# File 'lib/matchers/indexes.rb', line 45

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

#failure_message_for_shouldObject Also known as: failure_message



34
35
36
# File 'lib/matchers/indexes.rb', line 34

def failure_message_for_should
  "Expected #{@klass.inspect} to #{description}, got #{@errors.to_sentence}"
end

#failure_message_for_should_notObject Also known as: failure_message_when_negated



38
39
40
# File 'lib/matchers/indexes.rb', line 38

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

#matches?(klass) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/matchers/indexes.rb', line 13

def matches?(klass)
  @klass  = klass.is_a?(Class) ? klass : klass.class
  @errors = []

  index_specifications = @klass.index_specifications.find { |is| is.key == @index_fields }
  if index_specifications
    if !@options.nil? && !@options.empty?
      index_options = normalize_options(index_specifications.options)
      @options.each do |option, option_value|
        if index_options[option] != option_value
          @errors.push "index for #{@index_fields.inspect} with options of #{index_options.inspect}"
        end
      end
    end
  else
    @errors.push "no index for #{@index_fields}"
  end

  @errors.empty?
end

#with_options(options = { }) ⇒ Object



8
9
10
11
# File 'lib/matchers/indexes.rb', line 8

def with_options(options = { })
  @options = options
  self
end