Class: Mongoid::Matchers::HaveIndexForMatcher
- Inherits:
-
Object
- Object
- Mongoid::Matchers::HaveIndexForMatcher
- Defined in:
- lib/matchers/indexes.rb
Overview
:nodoc:
Instance Method Summary collapse
- #description ⇒ Object
- #failure_message_for_should ⇒ Object (also: #failure_message)
- #failure_message_for_should_not ⇒ Object (also: #failure_message_when_negated)
-
#initialize(index_fields) ⇒ HaveIndexForMatcher
constructor
A new instance of HaveIndexForMatcher.
- #matches?(klass) ⇒ Boolean
- #with_options(options = { }) ⇒ Object
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
#description ⇒ Object
60 61 62 63 64 |
# File 'lib/matchers/indexes.rb', line 60 def description desc = "have an index for #{@index_fields.inspect}" desc << " with options of #{@options.inspect}" if @options desc end |
#failure_message_for_should ⇒ Object Also known as: failure_message
49 50 51 |
# File 'lib/matchers/indexes.rb', line 49 def "Expected #{@klass.inspect} to #{description}, got #{@errors.to_sentence}" end |
#failure_message_for_should_not ⇒ Object Also known as: failure_message_when_negated
53 54 55 |
# File 'lib/matchers/indexes.rb', line 53 def "Expected #{@klass.inspect} to not #{description}, got #{@klass.inspect} to #{description}" end |
#matches?(klass) ⇒ Boolean
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/matchers/indexes.rb', line 13 def matches?(klass) @klass = klass.is_a?(Class) ? klass : klass.class @errors = [] if @klass.respond_to?(:index_options) # Mongoid 3 unless @klass.[@index_fields] @errors.push "no index for #{@index_fields}" else if !@options.nil? && !@options.empty? @options.each do |option, option_value| if (@klass.[@index_fields])[option] != option_value @errors.push "index for #{@index_fields.inspect} with options of #{.inspect}" end end end end else # Mongoid 4 unless @klass.index_specifications.map(&:key).include?(@index_fields) @errors.push "no index for #{@index_fields}" else if !@options.nil? && !@options.empty? = @klass.index_specifications.select { |is| is.key == @index_fields }.first. @options.each do |option, option_value| if [option] != option_value @errors.push "index for #{@index_fields.inspect} with options of #{@klass.[@index_fields].inspect}" end end end end end @errors.empty? end |
#with_options(options = { }) ⇒ Object
8 9 10 11 |
# File 'lib/matchers/indexes.rb', line 8 def ( = { }) @options = self end |