But when testing in the console I was always having to lookup a Person record to get the id, or sometimes I’d remember the ID. Wouldn’t it be nice if I could pass anything(within reason) and be able to ensure that I have a Person record?

[granted…this is Rails specific]

module Ensurable
extend ActiveSupport::Concern## ---# Allow you to ensure you have the class you expect... it's similar to# result = value.is_a?(Person) ? value : Person.find(value)## You can add fields to "ensure_by" (:id is included always)# e.g.# if you add `ensure_by :token` to the User class# User.ensure() works# User.ensure(:user_id) works# User.ensure(:token) works## .ensure() returns nil if the record is not found# .ensure!() throws an exception if the record is not found
class_methods dodef ensure_by(*args)@_ensure_by||= [:id]@_ensure_by+= [args].flattenenddefensure(thing = nil)returnnilunless thing.present?
return thing if thing.is_a?(self)
found = nil@_ensure_by||= [:id]@_ensure_by.eachdo|ensure_field|
value = thing.try(:fetch, ensure_field.to_sym, nil)|| thing.try(:fetch, ensure_field.to_s, nil)|| thing
found = self.find_by(ensure_field =&gt; value)breakif found.is_a?(self)end
found
enddefensure!(thing = nil)
result = self.ensure(thing)raiseActiveRecord::RecordNotFoundunless result
result
endendend

I have loved green-fielding apps in Rails for years. I think my first Rails app, a timesheet for my wife’s employees, was in sub 1.0 Rails. I ended up migrating that app through every major version of Rails up to 3.0. Then, she sold her business.

I used to work for a really great consultancy in Seattle, called Ubermind. I was one of two Rails specialists in a sea of iOS devs. It was so much fun to crank our api’s or backend services for client’s iOS apps. My longest term project was a legacy-maintenance gig that eventually turned into a very successful rewrite. Ah, the good times.

Along the way I’ve learned a few tricks of the trade when dealing with legacy apps or migrating to new Rails versions. I hope to add a few of those tips here.

One of the most common mistakes in programming is typos. Your brain wants to type one thing and your fingers another.

In Corona (and Lua in general), variables aren’t declared explicitly before you use them. When Lua sees a variable name that you haven’t declared as local, it starts looking up the chain of scopes until it reaches the Global namespace (_G). If it gets there, it creates a key in the _G table with your variable name.

This little trick (shown to me by my friend Chris Blackwell) makes Corona tell you whenever this happens. Sometimes it’s fine, but most of the time you’ll find a typo (as I did in the Particle Candy library). I even found that loading the built-in JSON library causes it to check for a global called “Chipmunk.” Granted, Chipmunk isn’t a typo, but it is accessing a variable that doesn’t exist. This can happen if you’re check to see if something has been loaded or not. In those cases this warning can be ignored.

So, put this in your app’s main.lua, run your app in the simulator and watch the console output. You might just find a problem you didn’t know you had.

Note: You only want this in debug builds. You could wrap it with a check for running on the simulator, but the best bet is to just remove it before building your final version.

I’ve recently been trying out different editors as I can see the end of the road for TextMate from where I’m sitting. I really wanted to be super-awesome and finally learn vi or emacs, but despite several attempts, my brain hasn’t really latched onto them like it did when TM came onto the scene.

Now there’s SublimeText 2 (in beta) which is starting to be the editor that TM wanted to be when it grew up. There’s still a lot of funky things with it, but I can see the potential. Not to mention it’s cross platform like the ancient gods vi and emacs. I suppose that’s really just icing on the cake because I rarely leave my home platform for reasons other than duress (Windows) or curiosity (Linux).

The one thing I really missed from TM was the ability to launch the CoronaSDK simulator from the editor. It’s fantastic to be able to edit a file and hit a button and have it fire up the simulator and then start reloading the simulator each time the file changes. (actually the simulator does that work for you) I took a look at the Corona bundle for TM and found quite a few lines of AppleScript that are doing the work (i kid not).

By setting the working_dir to the project folder and specifying ‘main.lua’ as the file, you can launch the simulator for a project no matter what file you happen to be looking at (or in what subfolder).

Save the file as “CoronaSDK.sublime-build”

Go to your Corona project

Open a file to edit

Select Tools > Build System > CoronaSDK

Press Command-B

profit!

For Reference, custom build system files are usually stored in the following locations:

Update: Dejay was correct, you have to try and open the file to know for sure if it’s there or not.

Want to know if a file exists before you use it?

Lately, I’ve been wanting something a little more dynamic in my projects for opening various Scenes (think Director, but not). I needed a way of looking to see if a file existed or not before I tried to load it up. Here’s the result: