lib/matchers/document/have_index.rb in mongoid-minitest-1.0.0 vs lib/matchers/document/have_index.rb in mongoid-minitest-1.1.0
- old
+ new
@@ -1,36 +1,49 @@
module Mongoid
module Matchers
+ # TODO: Add documentation.
+ def have_index_for *attrs
+ HaveIndexMatcher.new(*attrs)
+ end
+
+ private
+
class HaveIndexMatcher < Matcher
- def initialize *fields
- @fields = fields.map(&:to_sym)
+ attr_reader :attrs, :klass
+
+ def initialize *attrs
+ @attrs = attrs.map(&:to_sym)
end
def matches? subject
@klass = class_of subject
- @klass.index_options.any? do |index, options|
- index.keys == @fields
- end
+ index_options.any? { |idx, _| idx.keys == attrs }
end
def failure_message
- "#{@klass} to #{description}, but only found indexes #{indexes.inspect}"
+ "#{klass} to #{description}, but only found indexes #{indexes.inspect}"
end
def negative_failure_message
- "#{@klass} to not #{description}, but found an index for #{@fields.inspect}"
+ "#{klass} to not #{description}, but found an index for #{attrs.inspect}"
end
def description
- "have an index for #{@fields.inspect}"
+ "have an index for #{attrs.inspect}"
end
- def indexes
- @klass.index_options.keys.map(&:keys)
+ private
+
+ def index_options
+ if Mongoid::VERSION.to_i < 4
+ klass.index_options
+ else
+ Hash[klass.index_specifications.map{ |i| [i.key, i.options] }]
+ end
end
- end
- def have_index_for *fields
- HaveIndexMatcher.new(*fields)
+ def indexes
+ index_options.keys.map(&:keys)
+ end
end
end
end