Class: WhyValidationsSuckIn96::ValidatesDate

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

Overview

Checks the validity of an date attribute against a regular expression.

Examples:

Default usage

setup_validations do
  validates_as_date :start_date
end

Constant Summary collapse

DefaultDelimiters =
%r{[-/]}
DefaultParser =
lambda do |str|
  month, day, year = str.split(DefaultDelimiters, 3)
  {:month => month, :day => day, :year => year}
end
DefaultOptions =
{
  :message => "does not match the given date format or is not a valid date",
  :parser => DefaultParser
}

Instance Attribute Summary collapse

Attributes inherited from Validation

#options, #validatable

Instance Method Summary collapse

Methods included from AttributeBasedValidation

#attribute, #attribute_value, #validates?

Methods inherited from Validation

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

Constructor Details

#initialize(validatable, options = {}) ⇒ ValidatesDate

Returns a new instance of ValidatesDate.

Parameters:

  • validatable (Object)

    An object to be validated

  • options (Hash) (defaults to: {})

    The options to set up the validation with

Options Hash (options):

  • :with (Regexp)

    A regular expression to check against



32
33
34
# File 'lib/whyvalidationssuckin96/macros/validates_date.rb', line 32

def initialize(validatable, options = {})
  super
end

Instance Attribute Details

#dateObject (readonly)

Returns the value of attribute date.



14
15
16
# File 'lib/whyvalidationssuckin96/macros/validates_date.rb', line 14

def date
  @date
end

Instance Method Details

#validateObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/whyvalidationssuckin96/macros/validates_date.rb', line 36

def validate
  super
  if parse_date
    pass
  else
    fail
  end
rescue => e
  fail
end