Sha256: 09d5beded4782d18fe1b4016921f7f527c5d2d0f1f7c578dfee984ebc319eaf4

Contents?: true

Size: 1.5 KB

Versions: 6

Compression:

Stored size: 1.5 KB

Contents

module CDQ

  class CDQStoreManager

    attr_writer :current

    def initialize(opts = {})
      @config = opts[:config] || CDQConfig.default
      @model_manager = opts[:model_manager]
    end

    def current
      @current ||= create_store
    end

    def reset!
      NSFileManager.defaultManager.removeItemAtURL(@config.database_url, error: nil)
    end

    def invalid?
      !@current && @model_manager.invalid?
    end

    private

    def create_store
      if invalid?
        raise "No model found.  Can't create a persistent store coordinator without it."
      else
        coordinator = NSPersistentStoreCoordinator.alloc.initWithManagedObjectModel(@model_manager.current)
        error = Pointer.new(:object)
        options = { NSMigratePersistentStoresAutomaticallyOption => true,
                    NSInferMappingModelAutomaticallyOption => true }
        url = @config.database_url
        store = coordinator.addPersistentStoreWithType(NSSQLiteStoreType,
                                                       configuration:nil,
                                                       URL:url,
                                                       options:options,
                                                       error:error)
        if store.nil?
          error[0].userInfo['metadata'] && error[0].userInfo['metadata'].each do |key, value|
            NSLog "#{key}: #{value}"
          end
          raise error[0].userInfo['reason']
        end
        coordinator
      end
    end
  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
cdq-0.1.8 motion/cdq/store.rb
cdq-0.1.7 motion/cdq/store.rb
cdq-0.1.6 motion/cdq/store.rb
cdq-0.1.5 motion/cdq/store.rb
cdq-0.1.2 motion/cdq/store.rb
cdq-0.1.1 motion/cdq/store.rb