Skip to content

Instantly share code, notes, and snippets.

@proger
Created October 17, 2011 14:18
Show Gist options
  • Select an option

  • Save proger/1292682 to your computer and use it in GitHub Desktop.

Select an option

Save proger/1292682 to your computer and use it in GitHub Desktop.
#!/usr/bin/env macruby
framework 'Cocoa'
framework 'CoreData'
def path(p) NSURL.fileURLWithPath p end
def moc_init(mompath, sqlitedbpath)
mom = NSManagedObjectModel.alloc.initWithContentsOfURL mompath
psc = NSPersistentStoreCoordinator.alloc.initWithManagedObjectModel mom
psc.addPersistentStoreWithType NSSQLiteStoreType, configuration:nil, URL:sqlitedbpath, options:nil, error:nil
moc = NSManagedObjectContext.alloc.init
moc.setPersistentStoreCoordinator psc
moc
end
def request(moc, entity_name, sort_descriptors=[], predicate_fmt=[], distinct=false)
entity = NSEntityDescription.entityForName entity_name, inManagedObjectContext:moc
fetchrq = NSFetchRequest.alloc.init
fetchrq.setEntity entity
fetchrq.setFetchBatchSize 20
fetchrq.setSortDescriptors(
sort_descriptors.map {|sd|
NSSortDescriptor.alloc.initWithKey(sd[:key], ascending:sd[:ascending])}
) if sort_descriptors.count != 0
fetchrq.setPredicate(NSPredicate.predicateWithFormat *predicate_fmt) if predicate_fmt.count != 0
fetchrq.setReturnsDistinctResults distinct
moc.executeFetchRequest fetchrq, error:nil
end
moc = moc_init(path("MobileCiklum.app/MobileCiklum.momd"), path("Documents/MobileCiklum.sqlite"))
cities = request(moc, "City", [{:key => "name", :ascending => "false"}])
pois = Hash[cities.map {|city|
poitypes = request(moc, "POI", [], ["city == %@", city]).map {|poi| poi.type}.inject([]) {|result, type|
result << type unless result.include?(type)
result
}
categories = poitypes.inject([]) {|result, type|
result << type.category unless result.include?(type.category)
result
}
[city, [poitypes, categories]]
}]
pois.each {|city, args|
ptlist, catlist = args
tynames = ptlist.map(&:name)
catnames = catlist.map(&:name)
if tynames.count != 0
puts "#{city.name}: #{tynames}"
puts " categories: #{catnames}"
end
}
# kai% ./traverse.rb 4.3.2/Applications/4142AEC7-045F-412F-9395-F594DCD24E59
# Donetsk: ["Pub", "Restaurant", "Skiing", "Paintball"]
# categories: ["Eating Out", "Entertainment"]
# Kiev: ["Paintball", "Carting", "Bar", "Pub", "Cafe"]
# categories: ["Entertainment", nil, "Eating Out"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment