Adding dereferencing on construct to Enterprises service - this is MAGIC

This commit is contained in:
Will Marshall
2014-06-18 15:48:24 +10:00
parent a2965696da
commit 35d5dde5aa
2 changed files with 18 additions and 2 deletions

View File

@@ -5,3 +5,14 @@ Darkswarm.factory 'Enterprises', (enterprises)->
@enterprises = enterprises
for enterprise in enterprises
@enterprises_by_id[enterprise.id] = enterprise
@dereference()
dereference: ->
for enterprise in @enterprises
if enterprise.hubs
for hub, i in enterprise.hubs
enterprise.hubs[i] = @enterprises_by_id[hub.id]
if enterprise.producers
for producer, i in enterprise.producers
enterprise.producers[i] = @enterprises_by_id[producer.id]

View File

@@ -1,8 +1,9 @@
describe "Enterprises service", ->
Enterprises = null
enterprises = [
{id: 1, type: "hub"},
{id: 2, type: "producer"}
{id: 1, type: "hub", producers: [{id: 2}]},
{id: 2, type: "producer", hubs: [{id: 1}]},
{id: 3, type: "producer", hubs: [{id: 1}]}
]
beforeEach ->
module 'Darkswarm'
@@ -20,3 +21,7 @@ describe "Enterprises service", ->
it "puts the same objects in enterprises and enterprises_by_id", ->
expect(Enterprises.enterprises[0]).toBe Enterprises.enterprises_by_id["1"]
it "dereferences references to other enterprises", ->
expect(Enterprises.enterprises_by_id["1"].producers[0]).toBe enterprises[1]
expect(Enterprises.enterprises_by_id["3"].hubs[0]).toBe enterprises[0]