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
# File 'lib/motion_data_wrapper/delegate.rb', line 4

def managedObjectContext
  @managedObjectContext ||= begin
    error_ptr = Pointer.new(:object)
    unless persistentStoreCoordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: sqlite_url, 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



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

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



66
67
68
# File 'lib/motion_data_wrapper/delegate.rb', line 66

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

#persistentStoreCoordinatorObject



36
37
38
# File 'lib/motion_data_wrapper/delegate.rb', line 36

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

#sqlite_pathObject



58
59
60
# File 'lib/motion_data_wrapper/delegate.rb', line 58

def sqlite_path
  @sqlite_path || File.join(App.documents_path, "#{sqlite_store_name}.sqlite")
end

#sqlite_path=(path) ⇒ Object



62
63
64
# File 'lib/motion_data_wrapper/delegate.rb', line 62

def sqlite_path= path
  @sqlite_path = path
end

#sqlite_store_nameObject



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

def sqlite_store_name
  App.name
end

#sqlite_urlObject



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/motion_data_wrapper/delegate.rb', line 44

def sqlite_url
  if Object.const_defined?("UIApplication")
    NSURL.fileURLWithPath(sqlite_path)
  else
    error_ptr = Pointer.new(:object)
    unless support_dir = NSFileManager.defaultManager.URLForDirectory(NSApplicationSupportDirectory, inDomain: NSUserDomainMask, appropriateForURL: nil, create: true, error: error_ptr)
      raise "error creating application support folder: #{error_ptr[0]}"
    end
    support_dir = support_dir.URLByAppendingPathComponent("/#{App.name}")
    Dir.mkdir(support_dir.path) unless Dir.exists?(support_dir.path)
    support_dir.URLByAppendingPathComponent("#{sqlite_store_name}.sqlite")
  end
end