-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
358 additions
and
315 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
forSchema: require('./initialization'), | ||
initialize: require('./initialize') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const { ATTRIBUTES } = require('../symbols'); | ||
const initializationOrderFor = require('./initializationOrder'); | ||
|
||
function initializedValue(attrPassedValue, attrInitializer, attrDescriptor, instance) { | ||
if(attrPassedValue !== undefined) { | ||
return attrPassedValue; | ||
} | ||
|
||
return attrInitializer(attrDescriptor, instance); | ||
} | ||
|
||
module.exports = function forSchema(schema) { | ||
const initializationOrder = initializationOrderFor(schema); | ||
|
||
return { | ||
initialize(attributes, instance) { | ||
instance[ATTRIBUTES] = Object.create(null); | ||
|
||
for(let i = 0; i < initializationOrder.length; i++) { | ||
const [ attrName, attrInitializer ] = initializationOrder[i]; | ||
const attrDescriptor = schema[attrName]; | ||
const attrPassedValue = attributes[attrName]; | ||
|
||
instance[attrName] = initializedValue(attrPassedValue, attrInitializer, attrDescriptor, instance); | ||
} | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const { isFunction } = require('lodash'); | ||
|
||
function isStaticInitialization(attrDescriptor) { | ||
return !isFunction(attrDescriptor.default); | ||
} | ||
|
||
function staticInitialization(attrDescriptor) { | ||
return attrDescriptor.default; | ||
} | ||
|
||
function derivedInitialization(attrDescriptor, instance) { | ||
return attrDescriptor.default(instance); | ||
} | ||
|
||
module.exports = function initializationOrderFor(schema) { | ||
const staticInitializations = []; | ||
const derivedInitializations = []; | ||
|
||
for(let attrName in schema) { | ||
const attributeDescriptor = schema[attrName]; | ||
|
||
if(isStaticInitialization(attributeDescriptor)) { | ||
staticInitializations.push([attrName, staticInitialization]); | ||
} else { | ||
derivedInitializations.push([attrName, derivedInitialization]); | ||
} | ||
} | ||
|
||
return [...staticInitializations, ...derivedInitializations]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const { INITIALIZE } = require('../symbols'); | ||
|
||
module.exports = function initialize(schema, attributes, instance) { | ||
schema[INITIALIZE].initialize(attributes, instance); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
module.exports = { | ||
SCHEMA: Symbol('schema'), | ||
ATTRIBUTES: Symbol('attributes'), | ||
VALIDATE: Symbol('validate') | ||
VALIDATE: Symbol('validate'), | ||
INITIALIZE: Symbol('initialize') | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters