PassiveRecord
October 2nd, 2008
Ever need to use structured data that doesn’t need its own database model? Ryan Bates shows you how in a recent Railscast: Non Active Record Model.
If you have more than one non ActiveRecord model, you’ll find yourself starting to duplicate a lot of code. I did recently, so I extracted a bunch of functionality into a plugin called PassiveRecord.
There's more! Read the rest of this entry
Date Parsing Strangeness
February 22nd, 2008
Calculating dates is arguably one of the most complicated parts of programming. Ruby eases the stress with some great tools to help parse and calculate dates.
But lately I’ve run into some really strange behavior, and I’m kind of at a loss. I’m posting it here in the hopes that somebody can shed some light on why this is happening.
Basically I’m getting inconsistent results with the different parsing libraries. I’d like to be able to pass dates like “2/22”, “2/22/08”. Parsing a date like “2/22/08” should return February 2, 2008. But here’s what happens in the different parsing libraries:
>> Time.parse("2/22/08").to_date
=> Fri, 22 Feb 2008
>> Chronic.parse("2/22/08").to_date
=> Fri, 22 Feb 2008
>> Date.parse("2/22/08")
=> Wed, 22 Feb 0008
But as you can see, passing “2/22/08” to Date.parse we get the Year of our Lord, uh… 8. That’s obviously not right.
What about a date with no year, like “2/22”? I’d like it to return the date in the current year.
>> Date.parse("2/22")
=> Fri, 22 Feb 2008
>> Time.parse("2/22").to_date
=> Fri, 22 Feb 2008
>> Chronic.parse("2/22", :context => :past).to_date
=> Tue, 15 Feb 2022
Date and Time do pretty well, but even with :context => :past Chronic returns the year 2022.
What’s going on here? I’ll keep playing with it, but any insight would be appreciated.