Last active
December 27, 2015 02:29
-
-
Save yreynhout/7252444 to your computer and use it in GitHub Desktop.
Parking
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
<html> | |
<head> | |
<script src="http://cdnjs.cloudflare.com/ajax/libs/json2/20121008/json2.js"></script> | |
<script> | |
var AggregateRootEntity = | |
function AggregateRootEntityConstructor(options, my) { | |
var that = {}, | |
handlers = options.handlers || {}, | |
changes = []; | |
my.applyChange = function applyChange(event) { | |
if (event === null || event === undefined) | |
throw { message: 'The event argument cannot be null or undefined.' }; | |
if (event.name === null || event.name === undefined) | |
throw { message: 'The event name cannot be null or undefined.', event: event }; | |
event.data = event.data || {}; | |
var handler = handlers[event.name]; | |
if (handler !== undefined) { | |
handler(event.data); | |
} | |
changes.push(event); | |
}; | |
that.initialize = function initialize(events) { | |
if (events === null || events === undefined) | |
throw { message: 'The events argument cannot be null or undefined.' }; | |
if (that.hasChanges()) | |
throw { message: 'A changed instance cannot be initialized.' }; | |
for (var index = 0, length = events.length; index < length; index++) { | |
var event = events[index]; | |
if (event.name === null || event.name === undefined) | |
throw { message: 'The event name cannot be null or undefined.', event: event, index: index, events: events }; | |
event.data = event.data || {}; | |
var handler = handlers[event.name]; | |
if (handler !== undefined) { | |
handler(event.data); | |
} | |
} | |
}; | |
that.hasChanges = function hasChanges() { | |
return changes.length !== 0; | |
}; | |
that.clearChanges = function clearChanges() { | |
changes.length = 0; | |
}; | |
that.getChanges = function getChanges() { | |
return changes.slice(); | |
}; | |
return that; | |
}; | |
var PrivateInventoryItemConstructor = | |
function PrivateInventoryItemConstructor(my) { | |
var my = my || { }, | |
state = { activated: false, id: '' }; | |
var that = | |
new AggregateRootEntity( | |
{ | |
handlers: { | |
'InventoryItemCreated': function InventoryItemCreated(event) { | |
state.id = event.Id; | |
state.activated = true; | |
}, | |
'InventoryItemDeactivated': function InventoryItemDeactivated(event) { | |
state.activated = false; | |
} | |
} | |
}, my); | |
that.changeName = function changeName(newName) { | |
if (newName === null || newName === undefined || newName === '') | |
throw { message: 'The newName cannot be null, undefined or empty.' }; | |
my.applyChange({ name: 'InventoryItemRenamed', data: { Id: state.id, NewName: newName } }); | |
}; | |
that.remove = function remove(count) { | |
if (count <= 0) | |
throw { message: 'Cannot remove negative count from inventory.' }; | |
my.applyChange({ name: 'ItemsRemovedFromInventory', data: { Id: state.id, Count: count } }); | |
} | |
that.checkIn = function checkIn(count) { | |
if (count <= 0) | |
throw { message: 'Must have a count greater than 0 to add to inventory.' }; | |
my.applyChange({ name: 'ItemsCheckedInToInventory', data: { Id: state.id, Count: count } }); | |
}; | |
that.deactivate = function deactivate() { | |
if (state.activated === false) | |
throw { message: 'Already deactivated.' }; | |
my.applyChange({ name: 'InventoryItemDeactivated', data: { Id: state.id } }); | |
}; | |
return that; | |
}; | |
var InventoryItem = | |
function PublicInventoryItemConstructor(id, name) { | |
if (name === null || name === undefined || name === '') | |
throw { message: 'The name cannot be null, undefined or empty.' }; | |
var my = {}; | |
var that = PrivateInventoryItemConstructor(my); | |
my.applyChange({ name: 'InventoryItemCreated', data: { Id: id, Name: name } }); | |
return that; | |
}; | |
var item1 = InventoryItem('item/1', 'Boxer shorts'); | |
item1.checkIn(5); | |
item1.checkIn(2); | |
item1.deactivate(); | |
var changes = item1.getChanges(); | |
for(var index = 0, length = changes.length; index < length; index++) { | |
console.info(JSON.stringify(changes[index])); | |
} | |
var item2 = InventoryItem('item/2', 'Shoes'); | |
item2.checkIn(3); | |
item2.checkIn(1); | |
item2.deactivate(); | |
var changes = item2.getChanges(); | |
for(var index = 0, length = changes.length; index < length; index++) { | |
console.info(JSON.stringify(changes[index])); | |
} | |
</script> | |
</head> | |
<body></body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment