Sha256: 55f76fee32abcda8c7fb1da5b5576fe7501a32e40dc76b8898f43b4025902969

Contents?: true

Size: 543 Bytes

Versions: 4

Compression:

Stored size: 543 Bytes

Contents

require 'date'

module Ohm
  module DateValidations
    DATE_REGEX = /\A([0-9]{4})-([01]?[0-9])-([0123]?[0-9])\z/
    
    def assert_date(att, error = [att, :not_date])
      if assert_format att, DATE_REGEX, error
        m = send(att).to_s.match(DATE_REGEX)
        assert is_date_parseable?(m[1], m[2], m[3]), error
      end
    end

  private
    def is_date_parseable?(year, month, day)
      Date.new(Integer(year), Integer(month), Integer(day))
    rescue ArgumentError
      return false
    else
      return true
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ohm-contrib-0.0.31 lib/ohm/contrib/date_validations.rb
ohm-contrib-0.0.30 lib/ohm/contrib/date_validations.rb
ohm-contrib-0.0.29 lib/ohm/contrib/date_validations.rb
ohm-contrib-0.0.28 lib/ohm/contrib/date_validations.rb