Created
June 16, 2010 07:58
-
-
Save jballanc/440303 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
framework 'CoreData' | |
a = NSAttributeDescription.alloc.init | |
a.setName "EmployeeName" | |
a.attributeType = NSStringAttributeType | |
puts "---\nMade an attribute: #{a.description}" | |
e = NSEntityDescription.alloc.init | |
e.setName "Employee" | |
e.setProperties [a] | |
puts "---\nMade an entity: #{e.description}" | |
mom = NSManagedObjectModel.alloc.init | |
mom.setEntities [e] | |
puts "---\nMade a managed object model #{mom.description}" | |
p = Pointer.new('@') | |
test_url = NSURL.fileURLWithPath(File.expand_path('~/Desktop/test.sqlite')) | |
psc = NSPersistentStoreCoordinator.alloc.initWithManagedObjectModel(mom) | |
psc.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: test_url, options: nil, error: p) | |
puts "---\nMade a persistent store coordinator: #{psc.description}. Errors? #{p[0]}" | |
moc = NSManagedObjectContext.alloc.init | |
moc.setUndoManager(nil) | |
moc.setPersistentStoreCoordinator(psc) | |
puts "---\nMade a managed object context: #{moc.description}" | |
mo = NSManagedObject.alloc.initWithEntity(e, insertIntoManagedObjectContext: moc) | |
mo.EmployeeName = "Jeff" | |
puts "---\nMade a managed object: #{mo.description}" | |
moc.save(p) | |
puts "---\nSaved it! Errors? #{p[0]}" | |
mo = NSManagedObject.alloc.initWithEntity(e, insertIntoManagedObjectContext: moc) | |
mo.EmployeeName = "Joe" | |
puts "---\nMade another managed object: #{mo.description}" | |
moc.save(p) | |
puts "---\nSaved it! Errors? #{p[0]}" | |
mo = NSManagedObject.alloc.initWithEntity(e, insertIntoManagedObjectContext: moc) | |
mo.EmployeeName = "Jim" | |
puts "---\nMade another managed object: #{mo.description}" | |
moc.save(p) | |
puts "---\nSaved it! Errors? #{p[0]}" | |
puts "---\nNow for the serious work..." | |
mom_copy = mom.copy | |
puts "---\nCopied the managed object model: #{mom_copy.description}" | |
b = NSAttributeDescription.alloc.init | |
b.setName "BossName" | |
b.attributeType = NSStringAttributeType | |
puts "---\nNew attribute: #{b.description}" | |
f = NSEntityDescription.alloc.init | |
f.setName "Boss" | |
f.setProperties [b] | |
puts "---\nNew entity: #{f.description}" | |
mom_copy.setEntities mom_copy.entities.map { |e| e.copy } + [f] | |
puts "---\nAdded the entity, now our copy has entities: #{mom_copy.entities.map { |e| e.description }}" | |
map = NSMappingModel.inferredMappingModelForSourceModel(mom, destinationModel: mom_copy, error: p) | |
puts "---\nMade a mapping model: #{map.description}. Errors? #{p[0]}" | |
manager = NSMigrationManager.alloc.initWithSourceModel(mom, destinationModel: mom_copy) | |
puts "---\nMade a migration manager: #{manager.description}" | |
#mom_copy.setEntities map.entityMappings.map { |m| manager.destinationEntityForEntityMapping(m) } | |
#pust "---\nUhh...set the entities again. Now our copy has entities: #{mom_copy.entities.map { |e| e.description }}" | |
puts "---\nHmm...stores? #{psc.persistentStores.map { |s| s.description }}" | |
puts "---\nWith metadata? #{psc.metadataForPersistentStore(psc.persistentStores.first).description}" | |
puts "---\n---\nHere we go...\n---\n!" | |
#require 'fileutils' | |
#FileUtils.mv(File.expand_path('~/Desktop/test.sqlite'), File.expand_path('~/Desktop/test.sqlite.tmp')) | |
tmp_url = NSURL.fileURLWithPath(File.expand_path('~/Desktop/test.sqlite.tmp')) | |
store = psc.persistentStores.first | |
psc.migratePersistentStore(store, toURL: tmp_url, options: nil, withType: NSSQLiteStoreType, error: p) | |
puts "Moved the store...#{p[0]}" | |
File.unlink(File.expand_path("~/Desktop/test.sqlite")) | |
manager.migrateStoreFromURL(tmp_url, type: NSSQLiteStoreType, options: nil, withMappingModel: map, toDestinationURL: test_url, destinationType: NSSQLiteStoreType, destinationOptions: nil, error: p) | |
puts "Success?" | |
if p[0] | |
puts "No! :(\nErrors: #{p[0].description}" | |
else | |
puts "YES!!!" | |
puts "---\nHooking back up..." | |
mom = mom_copy | |
psc = NSPersistentStoreCoordinator.alloc.initWithManagedObjectModel(mom) | |
psc.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: test_url, options: nil, error: p) | |
puts "---\nNew persistent store coordinator: #{psc.description}. Errors? #{p[0]}" | |
puts "---\nHmm...stores? #{psc.persistentStores.map { |s| s.description }}" | |
puts "---\nWith metadata? #{psc.metadataForPersistentStore(psc.persistentStores.first).description}" | |
puts "---\n---\nStatus check!" | |
puts "Entities in our managed object model: #{mom.entities.map { |e| e.description }}" | |
puts "Managed object model configurations: #{mom.configurations.map { |c| c.description }}" | |
puts "Entities in the mom in the psc: #{psc.managedObjectModel.entities.map { |e| e.description }}" | |
puts "Managed object model configurations of the mom in the psc: #{mom.configurations.map { |c| c.description }}" | |
puts "---\n---\nWhat? Compatible? #{mom.isConfiguration(mom.configurations.first, compatibleWithStoreMetadata: psc.persistentStores.first.metadata)}" | |
moc = NSManagedObjectContext.alloc.init | |
moc.setUndoManager(nil) | |
moc.setPersistentStoreCoordinator(psc) | |
puts "---\nMade a new managed object context: #{moc.description}" | |
mo = NSManagedObject.alloc.initWithEntity(mom.entitiesByName['Employee'], insertIntoManagedObjectContext: moc) | |
mo.EmployeeName = "Jake" | |
puts "---\nMade a new managed object: #{mo.description}" | |
puts "---\nJake's mom: #{e.managedObjectModel.description}\nOur mom: #{mom.description}" | |
moc.save(p) | |
puts "Saved it!" | |
if p[0] | |
puts "Uh-oh! #{p[0].userInfo.description}" | |
end | |
mo = NSManagedObject.alloc.initWithEntity(f, insertIntoManagedObjectContext: moc) | |
mo.BossName = "DaveO" | |
puts "---\nMade a new managed object with the new entity: #{mo.description}" | |
moc.save(p) | |
puts "Saved it!" | |
if p[0] | |
puts "Uh-oh! #{p[0].description}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment