Class: Shoulda::Matchers::ActiveRecord::HaveDbIndexMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/shoulda/matchers/active_record/have_db_index_matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(columns) ⇒ HaveDbIndexMatcher

Returns a new instance of HaveDbIndexMatcher.



116
117
118
119
# File 'lib/shoulda/matchers/active_record/have_db_index_matcher.rb', line 116

def initialize(columns)
  @expected_columns = normalize_columns_to_array(columns)
  @qualifiers = {}
end

Instance Method Details

#descriptionObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/shoulda/matchers/active_record/have_db_index_matcher.rb', line 154

def description
  String.new('have ').tap do |description|
    description <<
      if qualifiers.include?(:unique)
        "#{Shoulda::Matchers::Util.a_or_an(index_type)} "
      else
        'an '
      end

    description << 'index on '

    description << inspected_expected_columns
  end
end

#failure_messageObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/shoulda/matchers/active_record/have_db_index_matcher.rb', line 131

def failure_message
  message =
    "Expected #{described_table_name} to #{positive_expectation}"

  message <<
    if index_exists?
      ". The index does exist, but #{reason}."
    elsif reason
      ", but #{reason}."
    else
      ', but it does not.'
    end

  Shoulda::Matchers.word_wrap(message)
end

#failure_message_when_negatedObject



147
148
149
150
151
152
# File 'lib/shoulda/matchers/active_record/have_db_index_matcher.rb', line 147

def failure_message_when_negated
  Shoulda::Matchers.word_wrap(
    "Expected #{described_table_name} not to " +
    "#{negative_expectation}, but it does.",
  )
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
129
# File 'lib/shoulda/matchers/active_record/have_db_index_matcher.rb', line 126

def matches?(subject)
  @subject = subject
  index_exists? && correct_unique?
end

#unique(unique = true) ⇒ Object



121
122
123
124
# File 'lib/shoulda/matchers/active_record/have_db_index_matcher.rb', line 121

def unique(unique = true)
  @qualifiers[:unique] = unique
  self
end