Sha256: df70b98a25b051fbdce6d99369651f69e7f1f58d5be35ef5d029939504c85a01

Contents?: true

Size: 1.54 KB

Versions: 6

Compression:

Stored size: 1.54 KB

Contents

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

module WhyValidationsSuckIn96

  # Checks the validity of an date attribute against a regular expression.
  #
  # @example Default usage
  #   setup_validations do
  #     validates_as_date :start_date
  #   end
  class ValidatesDate < Validation
    attr_reader :date

    include WhyValidationsSuckIn96::SkippableValidation
    include WhyValidationsSuckIn96::AttributeBasedValidation
    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
    }

    # @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
    end

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

  private

    def parse_date
      parsed = options[:parser].call(attribute_value.to_s)
      @date = Date.civil(parsed[:year].to_i, parsed[:month].to_i, parsed[:day].to_i)
    end
  end # Validation

  ValidationBuilder.register_macro :validates_as_date, WhyValidationsSuckIn96::ValidatesDate
end   # WhyValidationsSuckIn96

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
whyvalidationssuckin96-1.6.1 lib/whyvalidationssuckin96/macros/validates_date.rb
whyvalidationssuckin96-1.6.0 lib/whyvalidationssuckin96/macros/validates_date.rb
whyvalidationssuckin96-1.5.5 lib/whyvalidationssuckin96/macros/validates_date.rb
whyvalidationssuckin96-1.5.4 lib/whyvalidationssuckin96/macros/validates_date.rb
whyvalidationssuckin96-1.5.3 lib/whyvalidationssuckin96/macros/validates_date.rb
whyvalidationssuckin96-1.5.2 lib/whyvalidationssuckin96/macros/validates_date.rb