Class: Mongoid::Matchers::HaveIndexFor

Inherits:
HaveIndexForBase show all
Defined in:
lib/matchers/indexes/v3/have_index_for.rb,
lib/matchers/indexes/v4/have_index_for.rb

Instance Attribute Summary

Attributes inherited from HaveIndexForBase

#index_key, #index_options, #model

Instance Method Summary collapse

Methods inherited from HaveIndexForBase

#initialize, #with_options

Constructor Details

This class inherits a constructor from Mongoid::Matchers::HaveIndexForBase

Instance Method Details

#descriptionObject



38
39
40
41
42
# File 'lib/matchers/indexes/v3/have_index_for.rb', line 38

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

#failure_message_for_shouldObject Also known as: failure_message



27
28
29
# File 'lib/matchers/indexes/v3/have_index_for.rb', line 27

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

#failure_message_for_should_notObject Also known as: failure_message_when_negated



31
32
33
# File 'lib/matchers/indexes/v3/have_index_for.rb', line 31

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

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/matchers/indexes/v3/have_index_for.rb', line 8

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

  if model.index_options[index_key]
    if !index_options.nil? && !index_options.empty?
      index_options.each do |option, option_value|
        if denormalising_options(model.index_options[index_key])[option] != option_value
          @errors.push "index for #{index_key.inspect} with options of #{model.index_options[index_key].inspect}"
        end
      end
    end
  else
    @errors.push "no index for #{index_key}"
  end

  @errors.empty?
end