Sha256: 9da0d8e7db92af701b8c0d0cc92f1b9d036416ab3e9f71f07ad5ef94c3256abe

Contents?: true

Size: 1.3 KB

Versions: 12

Compression:

Stored size: 1.3 KB

Contents

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

module WhyValidationsSuckIn96
  
  # Checks the validity of an attribute against a list of values for it to be included in.
  #
  # @example Default usage
  #   setup_validations do
  #     validates_inclusion_of :unit_system, :in => %w[imperial metric]
  #   end
  class ValidatesInclusion < Validation  
    include WhyValidationsSuckIn96::SkippableValidation
    include WhyValidationsSuckIn96::AttributeBasedValidation
    
    DefaultOptions = {:message => "is not in the acceptable collection"}
    
    # @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 inclusion.
    def initialize(validatable, options = {})
      super
      raise(ArgumentError, "Collection to check for inclusion against should be specified with :in") unless options[:in]
    end
    
    def validate
      super
      if options[:in].include?(attribute_value)
        pass
      else
        fail
      end
    end

  end # Validation

  ValidationBuilder.register_macro :validates_inclusion_of, WhyValidationsSuckIn96::ValidatesInclusion
end   # WhyValidationsSuckIn96

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
whyvalidationssuckin96-1.5.2 lib/whyvalidationssuckin96/macros/validates_inclusion.rb
whyvalidationssuckin96-1.5.1 lib/whyvalidationssuckin96/macros/validates_inclusion.rb
whyvalidationssuckin96-1.5.0 lib/whyvalidationssuckin96/macros/validates_inclusion.rb
whyvalidationssuckin96-1.4.0 lib/whyvalidationssuckin96/macros/validates_inclusion.rb
whyvalidationssuckin96-1.3.1 lib/whyvalidationssuckin96/macros/validates_inclusion.rb
whyvalidationssuckin96-1.3.0 lib/whyvalidationssuckin96/macros/validates_inclusion.rb
whyvalidationssuckin96-1.2.3 lib/whyvalidationssuckin96/macros/validates_inclusion.rb
whyvalidationssuckin96-1.2.2 lib/whyvalidationssuckin96/macros/validates_inclusion.rb
whyvalidationssuckin96-1.2.1 lib/whyvalidationssuckin96/macros/validates_inclusion.rb
whyvalidationssuckin96-1.2.0 lib/whyvalidationssuckin96/macros/validates_inclusion.rb
whyvalidationssuckin96-1.1.0 lib/whyvalidationssuckin96/macros/validates_inclusion.rb
whyvalidationssuckin96-1.0.0 lib/whyvalidationssuckin96/macros/validates_inclusion.rb