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.



285
286
287
288
# File 'lib/shoulda/matchers/active_record/define_enum_for_matcher.rb', line 285

def initialize(attribute_name)
  @attribute_name = attribute_name
  @options = { expected_enum_values: [], scopes: true, instance_methods: true }
end

Instance Method Details

#backed_by_column_of_type(expected_column_type) ⇒ Object



333
334
335
336
# File 'lib/shoulda/matchers/active_record/define_enum_for_matcher.rb', line 333

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

#descriptionObject



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/shoulda/matchers/active_record/define_enum_for_matcher.rb', line 290

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



365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/shoulda/matchers/active_record/define_enum_for_matcher.rb', line 365

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



378
379
380
381
# File 'lib/shoulda/matchers/active_record/define_enum_for_matcher.rb', line 378

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)


353
354
355
356
357
358
359
360
361
362
363
# File 'lib/shoulda/matchers/active_record/define_enum_for_matcher.rb', line 353

def matches?(subject)
  @record = subject

  enum_defined? &&
    enum_values_match? &&
    column_type_matches? &&
    enum_value_methods_exist? &&
    scope_presence_matches? &&
    default_value_matches? &&
    validating_matches?
end

#validating(value = true, allowing_nil: false) ⇒ Object



312
313
314
315
316
# File 'lib/shoulda/matchers/active_record/define_enum_for_matcher.rb', line 312

def validating(value = true, allowing_nil: false)
  options[:validating] = value
  options[:allowing_nil] = allowing_nil
  self
end

#with_default(default_value) ⇒ Object



348
349
350
351
# File 'lib/shoulda/matchers/active_record/define_enum_for_matcher.rb', line 348

def with_default(default_value)
  options[:default] = default_value
  self
end

#with_prefix(expected_prefix = true) ⇒ Object



323
324
325
326
# File 'lib/shoulda/matchers/active_record/define_enum_for_matcher.rb', line 323

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

#with_suffix(expected_suffix = true) ⇒ Object



328
329
330
331
# File 'lib/shoulda/matchers/active_record/define_enum_for_matcher.rb', line 328

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

#with_values(expected_enum_values) ⇒ Object



318
319
320
321
# File 'lib/shoulda/matchers/active_record/define_enum_for_matcher.rb', line 318

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

#without_instance_methodsObject



343
344
345
346
# File 'lib/shoulda/matchers/active_record/define_enum_for_matcher.rb', line 343

def without_instance_methods
  options[:instance_methods] = false
  self
end

#without_scopesObject



338
339
340
341
# File 'lib/shoulda/matchers/active_record/define_enum_for_matcher.rb', line 338

def without_scopes
  options[:scopes] = false
  self
end