Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Matt Scilipoti

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Verify Dates in Rails

From http://www.railtie.net/articles/2006/09/07/validate-dates-in-your-models:
>>>

The thing I find really nice is that if you pass it a malformed date, it returns nil. This means, in your model validations, you can add:
   1  
   2  require 'chronic'
   3  
   4  class Meeting < ActiveRecord::Base
   5  
   6    def validation
   7      errors.add :meeting_date, 'is not a valid date' if Chronic.parse(meeting_date.to_s).nil?
   8    end
   9  
  10  end

<<<
Other Chronic examples:
>>>
mojombo in #caboose, just released his first version of Chronic, a new Ruby Gem for natural language processing of Dates and Times.
   1  
   2  gem install chronic
   3  
   4  irb:>
   5  Chronic.parse('tomorrow')
   6      #=> Mon Aug 28 12:00:00 PDT 2006
   7  
   8  Chronic.parse('monday', :context => :past)
   9      #=> Mon Aug 21 12:00:00 PDT 2006
  10  
  11  Chronic.parse('this tuesday 5:00')
  12      #=> Tue Aug 29 17:00:00 PDT 2006
  13  
  14  Chronic.parse('this tuesday 5:00', :ambiguous_time_range => :none)
  15      #=> Tue Aug 29 05:00:00 PDT 2006
  16  
  17  Chronic.parse('may 27th', :now => Time.local(2000, 1, 1))
  18      #=> Sat May 27 12:00:00 PDT 2000
  19  
  20  Chronic.parse('may 27th', :guess => false)
  21      #=> Sun May 27 00:00:00 PDT 2007..Mon May 28 00:00:00 PDT 2007
  22  

<<<
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS