Added a function for denormalizing levels, extending level thang components and configs with ones from thang types.

This commit is contained in:
Scott Erickson 2014-08-06 15:18:22 -07:00
parent 4d4e321265
commit 29770bd220
5 changed files with 101 additions and 1 deletions

View file

@ -25,6 +25,28 @@ module.exports = class Level extends CocoModel
o.thangTypes = (original: tt.get('original'), name: tt.get('name') for tt in supermodel.getModels ThangType)
o
denormalize: (supermodel) ->
o = $.extend true, {}, @attributes
for levelThang in o.thangs
@denormalizeThang(levelThang, supermodel)
o
denormalizeThang: (levelThang, supermodel) ->
thangType = supermodel.getModelByOriginal(ThangType, levelThang.thangType)
configs = {}
for thangComponent in levelThang.components
configs[thangComponent.original] = thangComponent
for defaultThangComponent in thangType.get('components')
if levelThangComponent = configs[defaultThangComponent.original]
# take the thang type default components and merge level-specific component config into it
copy = $.extend true, {}, defaultThangComponent.config
levelThangComponent.config = _.merge copy, levelThangComponent.config
else
# just add the component as is
levelThang.components.push $.extend true, {}, defaultThangComponent
sortSystems: (levelSystems, systemModels) ->
[sorted, originalsSeen] = [[], {}]
visit = (system) ->

View file

@ -79,6 +79,10 @@ module.exports = class SuperModel extends Backbone.Model
modelURL = modelURL() if _.isFunction(modelURL)
return @models[modelURL] or null
getModelByOriginal: (ModelClass, original) ->
_.find @models, (m) ->
m.get('original') is original and m.constructor.className is ModelClass.className
getModelByOriginalAndMajorVersion: (ModelClass, original, majorVersion=0) ->
_.find @models, (m) ->
m.get('original') is original and m.get('version').major is majorVersion and m.constructor.className is ModelClass.className

View file

@ -0,0 +1,52 @@
SuperModel = require 'models/SuperModel'
Level = require 'models/Level'
ThangType = require 'models/ThangType'
describe 'Level', ->
describe 'denormalize', ->
level = new Level({
thangs: [
{
"thangType": "A"
"id": "Tharin"
"components": [
{"original": "a", "majorVersion": 0}
{"original": "b", "majorVersion": 0, "config": {i: 2}}
{"original": "c", "majorVersion": 0, "config": {i: 1, ii: 2, nest: {iii: 3}}}
# should add one more
]
}
]
})
thangType = new ThangType({
original: 'A'
version: {major: 0, minor: 0}
components: [
{"original": "a", "majorVersion": 0, "config": {i: 1}}
{"original": "c", "majorVersion": 0, "config": {i: 3, nest: {iv: 4}}}
{"original": "d", "majorVersion": 0, "config": {i: 1}}
]
})
supermodel = new SuperModel()
supermodel.registerModel(thangType)
result = level.denormalize(supermodel)
tharinThangComponents = result.thangs[0].components
it 'adds default configs to thangs without any config', ->
aComp = _.find tharinThangComponents, {original:'a'}
expect(_.isEqual(aComp.config, {i:1})).toBeTruthy()
it 'leaves alone configs for components the level thang has but the thang type does not', ->
bComp = _.find tharinThangComponents, {original:'b'}
expect(_.isEqual(bComp.config, {i:2})).toBeTruthy()
it 'merges configs where both the level thang and thang type have one, giving priority to the level thang', ->
cComp = _.find tharinThangComponents, {original:'c'}
expect(_.isEqual(cComp.config, {i: 1, ii: 2, nest: {iii: 3, iv: 4}})).toBeTruthy()
it 'adds components from the thang type that do not exist in the level thang', ->
dComp = _.find tharinThangComponents, {original:'d'}
expect(_.isEqual(dComp.config, {i: 1})).toBeTruthy()

23
test/app/vendor/lodash.spec.coffee vendored Normal file
View file

@ -0,0 +1,23 @@
describe 'merge', ->
it 'combines nested objects recursively', ->
a = { i: 0, nest: { iii: 0 }}
b = { ii: 0, nest: { iv: 0 }}
res = _.merge(a, b)
expect(_.isEqual(res, { i: 0, ii: 0, nest: {iii:0, iv:0}})).toBeTruthy()
it 'overwrites values from source to object', ->
a = { i: 0 }
b = { i: 1 }
res = _.merge(a, b)
expect(_.isEqual(res, b)).toBeTruthy()
it 'treats arrays as atomic', ->
a = { i: 0 }
b = { i: [1,2,3] }
res = _.merge(a, b)
expect(_.isEqual(res, b)).toBeTruthy()
a = { i: [5,4,3] }
b = { i: [1,2,3] }
res = _.merge(a, b)
expect(_.isEqual(res, b)).toBeTruthy()

View file

@ -1 +0,0 @@
massivelyUsefulTestExample = 'test...'