Class: Shoulda::Matchers::ActiveRecord::DefineEnumForMatcher

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

Instance Method Summary collapse

Constructor Details

#initialize(attribute_name) ⇒ DefineEnumForMatcher

Returns a new instance of DefineEnumForMatcher.



174
175
176
177
# File 'lib/shoulda/matchers/active_record/define_enum_for_matcher.rb', line 174

def initialize(attribute_name)
  @attribute_name = attribute_name
  @options = { expected_enum_values: [] }
end

Instance Method Details

#backed_by_column_of_type(expected_column_type) ⇒ Object



224
225
226
227
# File 'lib/shoulda/matchers/active_record/define_enum_for_matcher.rb', line 224

def backed_by_column_of_type(expected_column_type)
  options[:expected_column_type] = expected_column_type
  self
end

#descriptionObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/shoulda/matchers/active_record/define_enum_for_matcher.rb', line 179

def description
  description = "#{simple_description} backed by "
  description << Shoulda::Matchers::Util.a_or_an(expected_column_type)

  if expected_enum_values.any?
    description << ' with values '
    description << Shoulda::Matchers::Util.inspect_value(
      expected_enum_values,
    )
  end

  if options[:prefix]
    description << ", prefix: #{options[:prefix].inspect}"
  end

  if options[:suffix]
    description << ", suffix: #{options[:suffix].inspect}"
  end

  description
end

#failure_messageObject



238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/shoulda/matchers/active_record/define_enum_for_matcher.rb', line 238

def failure_message
  message =
    if enum_defined?
      "Expected #{model} to #{expectation}. "
    else
      "Expected #{model} to #{expectation}, but "
    end

  message << "#{failure_message_continuation}."

  Shoulda::Matchers.word_wrap(message)
end

#failure_message_when_negatedObject



251
252
253
254
# File 'lib/shoulda/matchers/active_record/define_enum_for_matcher.rb', line 251

def failure_message_when_negated
  message = "Expected #{model} not to #{expectation}, but it did."
  Shoulda::Matchers.word_wrap(message)
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


229
230
231
232
233
234
235
236
# File 'lib/shoulda/matchers/active_record/define_enum_for_matcher.rb', line 229

def matches?(subject)
  @record = subject

  enum_defined? &&
    enum_values_match? &&
    column_type_matches? &&
    enum_value_methods_exist?
end

#with(expected_enum_values) ⇒ Object



206
207
208
209
210
211
212
# File 'lib/shoulda/matchers/active_record/define_enum_for_matcher.rb', line 206

def with(expected_enum_values)
  Shoulda::Matchers.warn_about_deprecated_method(
    'The `with` qualifier on `define_enum_for`',
    '`with_values`',
  )
  with_values(expected_enum_values)
end

#with_prefix(expected_prefix = true) ⇒ Object



214
215
216
217
# File 'lib/shoulda/matchers/active_record/define_enum_for_matcher.rb', line 214

def with_prefix(expected_prefix = true)
  options[:prefix] = expected_prefix
  self
end

#with_suffix(expected_suffix = true) ⇒ Object



219
220
221
222
# File 'lib/shoulda/matchers/active_record/define_enum_for_matcher.rb', line 219

def with_suffix(expected_suffix = true)
  options[:suffix] = expected_suffix
  self
end

#with_values(expected_enum_values) ⇒ Object



201
202
203
204
# File 'lib/shoulda/matchers/active_record/define_enum_for_matcher.rb', line 201

def with_values(expected_enum_values)
  options[:expected_enum_values] = expected_enum_values
  self
end