Class: WhyValidationsSuckIn96::ValidatesInclusion

Inherits:
WhyValidationsSuckIn96::Validation show all
Includes:
WhyValidationsSuckIn96::AttributeBasedValidation, WhyValidationsSuckIn96::SkippableValidation
Defined in:
lib/whyvalidationssuckin96/macros/validates_inclusion.rb

Overview

Checks the validity of an attribute against a list of values for it to be included in.

Examples:

Default usage

  setup_validations do
    validates_inclusion_of :unit_system, :in => %w[imperial metric]
  end

Constant Summary

DefaultOptions =
{:message => "is not in the acceptable collection"}

Constants inherited from WhyValidationsSuckIn96::Validation

DefaultOptions

Instance Method Summary

Methods included from WhyValidationsSuckIn96::AttributeBasedValidation

#attribute, #attribute_value

Methods inherited from WhyValidationsSuckIn96::Validation

#failed?, #has_run?, #message, new_subclass, #passed?, #validates?

Constructor Details

- (ValidatesInclusion) initialize(validatable, options = {})

A new instance of ValidatesInclusion

Parameters:

  • (Object) validatable — An object to be validated.
  • (Hash) options (defaults to: {}) — The options to set up the validation with.

Options Hash (options):

  • (#include?) :in N/A — A collection to check against for inclusion.

Raises:

  • (ArgumentError)


21
22
23
24
# File 'lib/whyvalidationssuckin96/macros/validates_inclusion.rb', line 21

def initialize(validatable, options = {})
  super
  raise(ArgumentError, "Collection to check for inclusion against should be specified with :in") unless options[:in]
end

Instance Method Details

- (Object) validate



26
27
28
29
30
31
32
33
# File 'lib/whyvalidationssuckin96/macros/validates_inclusion.rb', line 26

def validate
  super
  if options[:in].include?(attribute_value)
    pass
  else
    fail
  end
end