Module: MotionDataWrapper::Delegate

Defined in:
lib/motion_data_wrapper/delegate.rb

Instance Method Summary collapse

Instance Method Details

#managedObjectContextObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/motion_data_wrapper/delegate.rb', line 4

def managedObjectContext
  @managedObjectContext ||= begin
    documentsDirectory = NSFileManager.defaultManager.URLsForDirectory(NSDocumentDirectory, inDomains:NSUserDomainMask).lastObject;
    storeURL = documentsDirectory.URLByAppendingPathComponent("#{sqlite_store_name}.sqlite")

    error_ptr = Pointer.new(:object)
    unless persistentStoreCoordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration:nil, URL:storeURL, options:persistent_store_options, error:error_ptr)
      raise "Can't add persistent SQLite store: #{error_ptr[0].description}"
    end

    context = NSManagedObjectContext.alloc.init
    context.persistentStoreCoordinator = persistentStoreCoordinator

    context
  end
end

#managedObjectModelObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/motion_data_wrapper/delegate.rb', line 21

def managedObjectModel
  @managedObjectModel ||= begin
    model = NSManagedObjectModel.mergedModelFromBundles([NSBundle.mainBundle]).mutableCopy

    model.entities.each do |entity|
      begin
        Kernel.const_get(entity.name)
        entity.setManagedObjectClassName(entity.name)

      rescue NameError
        entity.setManagedObjectClassName("Model")
      end
    end

    model
  end
end

#persistent_store_optionsObject



47
48
49
# File 'lib/motion_data_wrapper/delegate.rb', line 47

def persistent_store_options
  { NSMigratePersistentStoresAutomaticallyOption => true, NSInferMappingModelAutomaticallyOption => true }
end

#persistentStoreCoordinatorObject



39
40
41
# File 'lib/motion_data_wrapper/delegate.rb', line 39

def persistentStoreCoordinator
  @coordinator ||= NSPersistentStoreCoordinator.alloc.initWithManagedObjectModel(managedObjectModel)
end

#sqlite_store_nameObject



43
44
45
# File 'lib/motion_data_wrapper/delegate.rb', line 43

def sqlite_store_name
  NSBundle.mainBundle.infoDictionary.objectForKey("CFBundleName")
end