Handle schema loading

This commit is contained in:
Ben Ogle
2014-09-23 15:02:22 -07:00
parent a84dd69f55
commit 02e87555f4
2 changed files with 128 additions and 0 deletions

View File

@@ -31,6 +31,9 @@ class Config
# Created during initialization, available as `atom.config`
constructor: ({@configDirPath, @resourcePath}={}) ->
@emitter = new Emitter
@schema =
type: 'object'
properties: {}
@defaultSettings = {}
@settings = {}
@configFileHasErrors = false
@@ -162,6 +165,13 @@ class Config
isDefault: (keyPath) ->
not _.valueForKeyPath(@settings, keyPath)?
getSchema: (keyPath) ->
keys = keyPath.split('.')
schema = @schema
for key in keys
schema = schema.properties[key]
schema
###
Section: To Deprecate
###
@@ -300,6 +310,9 @@ class Config
@watchSubscription = null
setDefaults: (keyPath, defaults) ->
if typeof defaults isnt 'object'
return _.setValueForKeyPath(@defaultSettings, keyPath, defaults)
keys = keyPath.split('.')
hash = @defaultSettings
for key in keys
@@ -310,6 +323,34 @@ class Config
@emit 'updated'
@emitter.emit 'did-change'
setSchema: (keyPath, schema) ->
unless typeof schema is "object"
throw new Error("Schemas can only be objects!")
unless typeof schema.type?
throw new Error("Schema object's must have a type attribute")
keys = keyPath.split('.')
rootSchema = @schema
for key in keys
rootSchema.type = 'object'
rootSchema.properties ?= {}
properties = rootSchema.properties
properties[key] ?= {}
rootSchema = properties[key]
_.extend rootSchema, schema
@setDefaults(keyPath, @extractDefaultsFromSchema(schema))
extractDefaultsFromSchema: (schema) ->
if schema.default?
schema.default
else if schema.type is 'object' and schema.properties? and typeof schema.properties is "object"
defaults = {}
properties = schema.properties or {}
defaults[key] = @extractDefaultsFromSchema(value) for key, value of properties
defaults
update: ->
return if @configFileHasErrors
@save()