Class: WhyValidationsSuckIn96::ValidatesFormat

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

Overview

Checks the validity of an attribute against a regular expression.

Examples:

Default usage

  setup_validations do
    validates_format_of :username, :with => /[a-z0-9]/i
  end

Constant Summary

DefaultOptions =
{:message => "does not match the given format"}

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

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

A new instance of ValidatesFormat

Parameters:

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

Options Hash (options):

  • (Regexp) :with N/A — A regular expression to check against

Raises:

  • (ArgumentError)


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

def initialize(validatable, options = {})
  super
  raise(ArgumentError, "Regular expression to check against must be given as :with") unless options[:with]
end

Instance Method Details

- (Object) validate



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

def validate
  super
  if attribute_value.to_s =~ options[:with]
    pass
  else
    fail
  end
end