Sha256: eb7e45135d40a4c1a8f3e3d5b3b461caf58b2c3e0ef9c8b77d547a0b06a09826

Contents?: true

Size: 1.23 KB

Versions: 20

Compression:

Stored size: 1.23 KB

Contents

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

module WhyValidationsSuckIn96
  
  # Checks the validity of an attribute against a regular expression.
  #
  # @example Default usage
  #   setup_validations do
  #     validates_format_of :username, :with => /[a-z0-9]/i
  #   end
  class ValidatesFormat < Validation  
    include WhyValidationsSuckIn96::SkippableValidation
    include WhyValidationsSuckIn96::AttributeBasedValidation
    
    DefaultOptions = {:message => "does not match the given format"}
    
    # @param  [Object] validatable    An object to be validated
    # @param  [Hash]   options        The options to set up the validation with
    # @option options  [Regexp] :with A regular expression to check against
    def initialize(validatable, options = {})
      super
      raise(ArgumentError, "Regular expression to check against must be given as :with") unless options[:with]
    end
    
    def validate
      super
      if attribute_value.to_s =~ options[:with]
        pass
      else
        fail
      end
    end

  end # Validation

  ValidationBuilder.register_macro :validates_format_of, WhyValidationsSuckIn96::ValidatesFormat
end   # WhyValidationsSuckIn96

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
whyvalidationssuckin96-1.6.4 lib/whyvalidationssuckin96/macros/validates_format.rb
whyvalidationssuckin96-1.6.3 lib/whyvalidationssuckin96/macros/validates_format.rb
whyvalidationssuckin96-1.6.2 lib/whyvalidationssuckin96/macros/validates_format.rb
whyvalidationssuckin96-1.6.1 lib/whyvalidationssuckin96/macros/validates_format.rb
whyvalidationssuckin96-1.6.0 lib/whyvalidationssuckin96/macros/validates_format.rb
whyvalidationssuckin96-1.5.5 lib/whyvalidationssuckin96/macros/validates_format.rb
whyvalidationssuckin96-1.5.4 lib/whyvalidationssuckin96/macros/validates_format.rb
whyvalidationssuckin96-1.5.3 lib/whyvalidationssuckin96/macros/validates_format.rb
whyvalidationssuckin96-1.5.2 lib/whyvalidationssuckin96/macros/validates_format.rb
whyvalidationssuckin96-1.5.1 lib/whyvalidationssuckin96/macros/validates_format.rb
whyvalidationssuckin96-1.5.0 lib/whyvalidationssuckin96/macros/validates_format.rb
whyvalidationssuckin96-1.4.0 lib/whyvalidationssuckin96/macros/validates_format.rb
whyvalidationssuckin96-1.3.1 lib/whyvalidationssuckin96/macros/validates_format.rb
whyvalidationssuckin96-1.3.0 lib/whyvalidationssuckin96/macros/validates_format.rb
whyvalidationssuckin96-1.2.3 lib/whyvalidationssuckin96/macros/validates_format.rb
whyvalidationssuckin96-1.2.2 lib/whyvalidationssuckin96/macros/validates_format.rb
whyvalidationssuckin96-1.2.1 lib/whyvalidationssuckin96/macros/validates_format.rb
whyvalidationssuckin96-1.2.0 lib/whyvalidationssuckin96/macros/validates_format.rb
whyvalidationssuckin96-1.1.0 lib/whyvalidationssuckin96/macros/validates_format.rb
whyvalidationssuckin96-1.0.0 lib/whyvalidationssuckin96/macros/validates_format.rb