Class: CDQ::CDQConfig

Inherits:
Object
  • Object
show all
Defined in:
motion/cdq/config.rb

Overview

Configure the CDQ Stack

This class wraps the YAML configuration file that will allow you to override the names used when setting up the database file and finding the model file. This file is named cdq.yml and must be found at the root of your resources directory. It supports the following top-level keys:

[name]              The root name for both database and model
[database_dir]      The root name for the database directory (NSDocumentDirectory or NSApplicationSupportDirectory)
[database_name]     The root name for the database file (relative to the database_dir)
[model_name]        The root name for the model file (relative to the bundle directory)
[icloud]            If it's true, CDQ works with iCloud.
[icloud_container]  Set id of iCloud container if you use iCloud. If it's nil, use first container listed in the com.apple.developer.ubiquity-container-identifiers entitlement array.

Using the config file is not necessary. If you do not include it, the bundle display name will be used. For most people with a new app, this is what you want to do, especially if you are using ruby-xcdm schemas. The only case where using the config file is required is when you want to use CDQManagedObject-based models with a custom model or database, because class loading order of operations makes it impossible to configure from within your AppDelegate.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file) ⇒ CDQConfig

Returns a new instance of CDQConfig.


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'motion/cdq/config.rb', line 29

def initialize(config_file)
  h = nil
  case config_file
  when String
    @config_file = config_file
    h = nil
    if File.file?(config_file)
      h = File.open(config_file) { |f| YAML.load(f.read) }
      # If a file was consisted comments only, it may parse as an Array.
      h = nil unless h.is_a? Hash
    end
  when Hash
    h = config_file
  end
  h ||= {}

  @name = h['name'] || h[:name] || NSBundle.mainBundle.objectForInfoDictionaryKey("CFBundleExecutable")
  @database_dir = search_directory_for h['database_dir'] || h[:database_dir]
  @database_name = h['database_name'] || h[:database_name] || name
  @model_name = h['model_name'] || h[:model_name] || name
  @icloud = begin
    case h['icloud'] || h[:icloud]
    when true, 1
      true
    else
      false
    end
  end
  @icloud_container = h['icloud_container'] || h[:icloud_container]
end

Instance Attribute Details

#config_fileObject (readonly)

Returns the value of attribute config_file


27
28
29
# File 'motion/cdq/config.rb', line 27

def config_file
  @config_file
end

#database_dirObject (readonly)

Returns the value of attribute database_dir


27
28
29
# File 'motion/cdq/config.rb', line 27

def database_dir
  @database_dir
end

#database_nameObject (readonly)

Returns the value of attribute database_name


27
28
29
# File 'motion/cdq/config.rb', line 27

def database_name
  @database_name
end

#icloudObject (readonly)

Returns the value of attribute icloud


27
28
29
# File 'motion/cdq/config.rb', line 27

def icloud
  @icloud
end

#icloud_containerObject (readonly)

Returns the value of attribute icloud_container


27
28
29
# File 'motion/cdq/config.rb', line 27

def icloud_container
  @icloud_container
end

#model_nameObject (readonly)

Returns the value of attribute model_name


27
28
29
# File 'motion/cdq/config.rb', line 27

def model_name
  @model_name
end

#nameObject (readonly)

Returns the value of attribute name


27
28
29
# File 'motion/cdq/config.rb', line 27

def name
  @name
end

Class Method Details

.defaultObject


70
71
72
73
74
75
76
# File 'motion/cdq/config.rb', line 70

def self.default
  @default ||=
    begin
      cf_file = NSBundle.mainBundle.pathForResource("cdq", ofType: "yml");
      new(cf_file)
    end
end

Instance Method Details

#database_urlObject


60
61
62
63
64
# File 'motion/cdq/config.rb', line 60

def database_url
  dir = NSSearchPathForDirectoriesInDomains(database_dir, NSUserDomainMask, true).last
  path = File.join(dir, database_name + '.sqlite')
  NSURL.fileURLWithPath(path)
end

#model_urlObject


66
67
68
# File 'motion/cdq/config.rb', line 66

def model_url
  NSBundle.mainBundle.URLForResource(model_name, withExtension: "momd");
end