Sha256: 4d40e4a6e508561c0142a8fd61a979e920142b9144485191753a7691f77f809c

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

require 'whyvalidationssuckin96/skippable_validation'
require 'whyvalidationssuckin96/attribute_based_validation'

module WhyValidationsSuckIn96

  # Checks the validity of an attribute against a collection of excluded values.
  #
  # @example Default usage
  #   setup_validations do
  #     validates_exclusion_of :subdomain, :in => %w[www ftp]
  #   end
  #
  # @example Usage when checking a set of values against another set
  #   setup_validations do
  #     validates_exclusion_of :colours, :in => %w[red green], :set => true
  #     # colours can now be an array containing anything except 'red' or 'green'
  #   end
  class ValidatesExclusion < Validation
    include WhyValidationsSuckIn96::SkippableValidation
    include WhyValidationsSuckIn96::AttributeBasedValidation

    DefaultOptions = {:message => "is in the excluded collection",
                      :set     => false}

    # @param  [Object] validatable        An object to be validated.
    # @param  [Hash]   options            The options to set up the validation with.
    # @option options  [#include?]   :in  A collection to check against for exclusion.
    # @option options  [true, false] :set Whether or not to do a set based comparison
    def initialize(validatable, options = {})
      super
      raise(ArgumentError, "Collection to check for exclusion against should be specified with :in") unless options[:in]
    end

    def validate
      super
      if options[:set]
        attribute_value.any? do |val|
          options[:in].include?(val)
        end ? fail : pass
      elsif !options[:set] && options[:in].include?(attribute_value)
        fail
      else
        pass
      end
    end

  end # Validation

  ValidationBuilder.register_macro :validates_exclusion_of, WhyValidationsSuckIn96::ValidatesExclusion
end   # WhyValidationsSuckIn96

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
whyvalidationssuckin96-1.5.3 lib/whyvalidationssuckin96/macros/validates_exclusion.rb