December 4th, 2009 |
Published in
MacRuby, Ruby, iPhonedev
Lately I’ve been working to optimize a Core Data based iPhone app. Core Data has generally been a pleasure to work (along with great developer support from Apple), but as soon as I’ve needed to dig in to work with more complex (read: slow) queries, a few cracks appeared. For example, improper predicates don’t crash or return errors: they just never return. Factor in compile / run / get the app to the right spot, and the debug cycles are painful.
So eyeing (and looking for a productive reason to play/learn…) the latest MacRuby build I thought it might be useful to see if I couldn’t iterate with Ruby when testing more complex predicates to eliminate the entire compile/run phase. After a bit of fiddling, I was able to get this to work quite nicely.
Here is some basic Ruby for use with macirb to illustrate this technique…
framework 'cocoa'
modelURL = NSURL.fileURLWithPath('MyCoreData.mom')
storeURL = NSURL.fileURLWithPath('MyCoreData.sqlite')
mom = NSManagedObjectModel.alloc.initWithContentsOfURL(modelURL)
psc = NSPersistentStoreCoordinator.alloc.initWithManagedObjectModel(mom)
ps = psc.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: nil, error: nil)
moc = NSManagedObjectContext.alloc.init
moc.setPersistentStoreCoordinator(psc)
fetch = NSFetchRequest.alloc.init
fetch.setEntity(NSEntityDescription.entityForName('Employee', inManagedObjectContext: moc))
fetch.setFetchLimit(25)
results = moc.executeFetchRequest(fetch, error: nil)
November 22nd, 2009 |
Published in
Dev, Ruby
I’ve not done much web development over the last few years, other than some random PHP and old school ASP back in the day. I’ve been doing desktop and more recently mobile development, which I like quite a bit.
However, I’ve been playing around with web stuff recently, under the tutelage and influence of a few solid web devs I respect, and I’m having a lot of fun with it. At my new gig, we don’t yet have a CIT/nightly builds server setup yet, so I decided to toss together a web service to do on-demand builds for our project manager. So it’s a win:win – a useful down-and-dirty in-house utility and I get a chance to play around with some interesting stuff.
Since I really like ruby, I decided to write this web service using Sinatra. I took a bit of time to learn Sinatra and took a slight detour into HAML for formatting HTML output. Even with the learning curve associated with these items (I’m an intermediate with the Ruby language itself) this side project was trivial. As a matter of fact, without these technologies, I probably wouldn’t have even messed with doing such a throw-away task – I would have waited until we had a ‘real’ build system in place.
Here’s a simplified version of the code…
require 'rubygems'
require 'sinatra'
require 'haml'
IPHONE_ROUTE = '/latest-iphone.zip'
ANDROID_ROUTE = '/latest-android.zip'
IPHONE_PROJ_DIR = 'zagat-iphone'
ANDROID_PROJ_DIR = 'zagat-android'
BUILD_LOG_FNAME = 'build.txt'
get '/' do
@entries = Array.new
Struct.new("Entry", :href, :description)
@entries << Struct::Entry.new(IPHONE_ROUTE,'Latest iPhone')
@entries << Struct::Entry.new(ANDROID_ROUTE, 'Latest Android')
haml :index
end
get IPHONE_ROUTE do
File.delete(BUILD_LOG_FNAME) if File.exists?(BUILD_LOG_FNAME)
log = File.new(BUILD_LOG_FNAME, 'w+')
# get source - if we've never got the source before, let's get it now, otherwise, update
if !File.exists?(IPHONE_PROJ_DIR)
log << `hg clone foo`
else
log << `hg pull bar`
end
# build
if File.exists?(IPHONE_PROJ_DIR)
Dir.chdir(IPHONE_PROJ_DIR)
log << `xcodebuild -sdk iphoneos3.0 -configuration "Ad Hoc"`
# create the zip file
Dir.chdir('build/Ad Hoc-iphoneos')
log << `zip -r ../../..#{IPHONE_ROUTE} *.app`
Dir.chdir('../../..')
else
puts 'Build failed, go bug someone for support'
log.close
return
end
log.close
# add the log file to the zip
`zip .#{IPHONE_ROUTE} #{BUILD_LOG_FNAME}`
# return the file
send_file( ".#{IPHONE_ROUTE}", :type => 'application/zip' )
end
∞