Class: Shoulda::Matchers::ActiveModel::ValidateInclusionOfMatcher

Inherits:
ValidationMatcher
  • Object
show all
Defined in:
lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb

Constant Summary collapse

BLANK_VALUES =
['', ' ', "\n", "\r", "\t", "\f"].freeze
ARBITRARY_OUTSIDE_STRING =
Shoulda::Matchers::ExampleClass.name
ARBITRARY_OUTSIDE_INTEGER =
123456789
ARBITRARY_OUTSIDE_DECIMAL =
BigDecimal('0.123456789')
ARBITRARY_OUTSIDE_DATE =
Date.jd(9999999)
ARBITRARY_OUTSIDE_DATETIME =
DateTime.jd(9999999)
ARBITRARY_OUTSIDE_TIME =
Time.at(9999999999)

Instance Attribute Summary

Attributes included from Qualifiers::IgnoringInterferenceByWriter

#ignore_interference_by_writer

Instance Method Summary collapse

Methods inherited from ValidationMatcher

#allow_blank, #description, #expects_custom_validation_message?, #expects_strict?, #failure_message, #failure_message_when_negated, #failure_reason, #on, #strict

Methods included from Qualifiers::IgnoringInterferenceByWriter

#ignoring_interference_by_writer

Constructor Details

#initialize(attribute) ⇒ ValidateInclusionOfMatcher

Returns a new instance of ValidateInclusionOfMatcher.



305
306
307
308
309
310
311
312
313
314
# File 'lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb', line 305

def initialize(attribute)
  super(attribute)
  @options = {}
  @array = nil
  @range = nil
  @minimum = nil
  @maximum = nil
  @low_message = :inclusion
  @high_message = :inclusion
end

Instance Method Details

#allow_nilObject



328
329
330
331
# File 'lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb', line 328

def allow_nil
  @options[:allow_nil] = true
  self
end

#does_not_match?(subject) ⇒ Boolean

Returns:

  • (Boolean)


397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb', line 397

def does_not_match?(subject)
  super(subject)

  if @range
    does_not_match_for_range?
  elsif @array
    if does_not_match_for_array?
      true
    else
      @failure_message = "#{@array} matches array in validation"
      false
    end
  end
end

#expects_to_allow_nil?Boolean

Returns:

  • (Boolean)


333
334
335
# File 'lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb', line 333

def expects_to_allow_nil?
  @options[:allow_nil]
end

#in_array(array) ⇒ Object



316
317
318
319
# File 'lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb', line 316

def in_array(array)
  @array = array
  self
end

#in_range(range) ⇒ Object



321
322
323
324
325
326
# File 'lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb', line 321

def in_range(range)
  @range = range
  @minimum = minimum_range_value
  @maximum = maximum_range_value
  self
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb', line 382

def matches?(subject)
  super(subject)

  if @range
    matches_for_range?
  elsif @array
    if matches_for_array?
      true
    else
      @failure_message = "#{@array} doesn't match array in validation"
      false
    end
  end
end

#simple_descriptionObject



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb', line 364

def simple_description
  if @range
    "validate that :#{@attribute} lies inside the range " +
      Shoulda::Matchers::Util.inspect_range(@range)
  else
    description = "validate that :#{@attribute}"

    description <<
      if @array.length > 1
        " is either #{inspected_array}"
      else
        " is #{inspected_array}"
      end

    description
  end
end

#with_high_message(message) ⇒ Object



356
357
358
359
360
361
362
# File 'lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb', line 356

def with_high_message(message)
  if message
    @high_message = message
  end

  self
end

#with_low_message(message) ⇒ Object



347
348
349
350
351
352
353
354
# File 'lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb', line 347

def with_low_message(message)
  if message
    @expects_custom_validation_message = true
    @low_message = message
  end

  self
end

#with_message(message) ⇒ Object



337
338
339
340
341
342
343
344
345
# File 'lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb', line 337

def with_message(message)
  if message
    @expects_custom_validation_message = true
    @low_message = message
    @high_message = message
  end

  self
end