lib/matchers/indexes.rb in mongoid-rspec-2.0.0 vs lib/matchers/indexes.rb in mongoid-rspec-2.1.0
- old
+ new
@@ -12,37 +12,22 @@
def matches?(klass)
@klass = klass.is_a?(Class) ? klass : klass.class
@errors = []
- if @klass.respond_to?(:index_options)
- # Mongoid 3
- 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 denormalising_options(@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
+ 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
- # Mongoid 4
- unless @klass.index_specifications.map(&:key).include?(@index_fields)
- @errors.push "no index for #{@index_fields}"
- else
- if !@options.nil? && !@options.empty?
- index_options = @klass.index_specifications.select { |is| is.key == @index_fields }.first.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
- end
+ @errors.push "no index for #{@index_fields}"
end
@errors.empty?
end
@@ -63,18 +48,18 @@
desc
end
private
MAPPINGS = {
- dropDups: :drop_dups
+ dropDups: :drop_dups,
+ expireAfterSeconds: :expire_after_seconds,
+ bucketSize: :bucket_size
}
- def denormalising_options(opts)
- options = {}
- opts.each_pair do |option, value|
- options[MAPPINGS[option] || option] = value
+ def normalize_options(options)
+ options.transform_keys do |key|
+ MAPPINGS[key] || key
end
- options
end
end
def have_index_for(index_fields)
HaveIndexForMatcher.new(index_fields)