Browsing articles from "April, 2011"
Apr
25

Red Dirt Ruby Conf and Using Core Data with MacRuby

By Rob Warner  //  Development, iOS, Mac, Rails, Ruby  //  1 Comment

I just got back from Red Dirt Ruby Conf in Norman, OK. The accommodations were terrific — The Embassy Suites in Norman looks new, and they served us free omelets-to-order every morning. The food was good, the Wi-Fi was blazing, and every table in every conference room had power outlets for every chair. The swag table had plenty for all, and I scored a black Stack Overflow T and a Red Dirt Ruby Conf Macbook Pro cover from Sticker Mule that dresses up my work laptop nicely:

Red Dirt Cover on Mac

I had submitted a proposal to speak at this conference on Core Data and MacRuby that wasn’t selected. Last Sunday, however, one of the presenters had to drop out, so I received an email asking if I’d be willing to speak after all. I quickly accepted, then got to work preparing a presentation. I decided to help ease the transition of Rails developers to desktop development using Core Data and MacRuby by drawing parallels between Rails/ActiveRecord and Core Data. I had seen the frustration of a coworker who was well-versed in Rails, trying to work with Core Data. The parallels I drew were:

  • database.yml = persistent store coordinator
  • migrations = managed object model
  • ActiveRecord = managed object context

The presentation (Keynote format) and sample project are available on github at http://github.com/hoop33/CoreDataWithMacRuby.

At Red Dirt, I also snagged some new stickers for my personal laptop, which looks more like a college student’s steamer trunk every day:

My Mac

I renewed some friendships, met some new folks, learned about CoffeeScript and JRuby and Rubinius and DataMapper and Fat Models and OmniAuth — lots of things to follow up on. I love the single-track format, which I guess appeases loss avoidance, and the day-of-speakers plus day-of-training format worked well. Looking forward to Red Dirt Ruby Conf the Third!

Apr
17

Distance of Time in Words for Objective-C

By Rob Warner  //  Development, iOS, Rails, Ruby  //  2 Comments

Rails developers have the coolest tools, from migrations RVM to Bundler to Pow. One cool Rails tool lets you explain approximately how long ago something happened in words, with phrases such as:

  • Less than 20 seconds ago
  • About 3 minutes ago
  • Over 3 years ago

You find this magic in date_helper.rb, in the distance_of_time_in_words method. I’m working on an iOS app that synchronizes with a Rails app that uses distance_of_time_in_words to describe how long ago certain events occurred, and I needed to mimic the web app’s description of how long ago things happened. I could have added the description to the JSON that the Rails app returns, but not only did that seem silly, but also it meant that iOS users would have to re-sync to get updated descriptions of how long ago things happened. Better to implement a “distance of time in words” capability for iOS.

After Google didn’t net me what I wanted, I decided to implement this in Objective-C, translating from the Ruby code. I opted to make this a category on NSDate, so you could get the distance of time in words using something like this:

NSDate *myDate;
...
// Code that fills myDate
...
NSString *words = [myDate distanceOfTimeInWords];

Along the way, I learned that Objective-C (and C) support ranges in switch statements, so I can do things like the Ruby code does, like this:

case 2 ... 44:
  number = minutes;
  measure = Minutes;
  break;

Notice the three dots between 2 and 44.

The result? A category called NSDate+Formatting, available on github at:

https://github.com/hoop33/DistanceOfTimeInWords

That repository includes not only the NSDate+Formatting category, but also a sample project that uses it. The sample project shows a date picker and a label. Spin the date picker to select a date, and the label shows the distance of time in words between now and the selected date. You can also toggle the date picker among Time, Date, and Date & Time modes. Finally, you click a button to reset the date picker to now. Here are some screenshots:

Screenshot in Time mode
Screenshot in Date mode
Screenshot in Date and Time mode

The code uses localized strings, so should be simple to localize into any language. It currently has both english and Spanish translations. I left Chile over 20 years ago, so feel free to correct my Spanish!

I’ve released this project under the Apache 2.0 license, which I think means that you can use it in your personal and commercial projects, and don’t have to put my name in the About box or strew petals in my path. Someone correct me if I’m wrong.

About

I'm Rob Warner, and I'm a software developer. I live in Jacksonville, Florida, and work for Availity, LLC. The postings on this site are my own and do not necessarily reflect the views of Availity.