Created
April 13, 2010 23:00
-
-
Save mbbx6spp/365215 to your computer and use it in GitHub Desktop.
Balance sheet object model example to demonstrate CoffeeScript and translated Javascript.
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
# Creative Commons Attribution-Share Alike 3.0 United States License | |
# http://creativecommons.org/licenses/by-sa/3.0/us/ | |
# By Susan Potter <http://geek.susanpotter.net> | |
class BalanceSheet | |
constructor: (companyName, date) -> | |
@companyName: companyName | |
@date: date | |
@assets: [] | |
@liabilities: [] | |
entries: -> | |
(@assets + @liabilities) | |
assets: -> | |
@assets | |
liabilities: -> | |
@liabilities | |
addAsset: (asset) -> | |
@assets.push(asset) | |
addLiability: (liability) -> | |
@liabilities.push(liability) | |
class Term | |
class CurrentTerm extends Term | |
class LongTerm extends Term | |
class BalanceSheetEntry | |
constructor: (type, subtype, term, amount) -> | |
@type: type | |
@subtype: subtype | |
@term: term | |
@amount: amount | |
amount: -> | |
@amount | |
type: -> | |
@type | |
class StatedEntry extends BalanceSheetEntry | |
class AnalyzedEntry extends BalanceSheetEntry | |
balSheet: new BalanceSheet('XYZ Inc', new Date()) | |
balSheet.addAsset(new StatedEntry('asset', 'cash', CurrentTerm, 700000)) | |
balSheet.addAsset(new StatedEntry('asset', 'receivables', CurrentTerm, 2600000)) | |
balSheet.addAsset(new StatedEntry('asset', 'ppe', LongTerm, 60000000)) | |
balSheet.addLiability(new StatedEntry('liability', 'payables', CurrentTerm, 1500000)) | |
balSheet.addLiability(new StatedEntry('liability', 'mortgage', LongTerm, 10000000)) |
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
// Creative Commons Attribution-Share Alike 3.0 United States License | |
// http://creativecommons.org/licenses/by-sa/3.0/us/ | |
// Generated from CoffeeScript translator of previous CS code. | |
var AnalyzedEntry, BalanceSheet, BalanceSheetEntry, CurrentTerm, LongTerm, StatedEntry, Term, balSheet; | |
var __extends = function(child, parent) { | |
var ctor = function(){ }; | |
ctor.prototype = parent.prototype; | |
child.__superClass__ = parent.prototype; | |
child.prototype = new ctor(); | |
child.prototype.constructor = child; | |
}; | |
BalanceSheet = function BalanceSheet(companyName, date) { | |
this.companyName = companyName; | |
this.date = date; | |
this.assets = []; | |
this.liabilities = []; | |
return this; | |
}; | |
BalanceSheet.prototype.entries = function entries() { | |
return this.assets + this.liabilities; | |
}; | |
BalanceSheet.prototype.assets = function assets() { | |
return this.assets; | |
}; | |
BalanceSheet.prototype.liabilities = function liabilities() { | |
return this.liabilities; | |
}; | |
BalanceSheet.prototype.addAsset = function addAsset(asset) { | |
return this.assets.push(asset); | |
}; | |
BalanceSheet.prototype.addLiability = function addLiability(liability) { | |
return this.liabilities.push(liability); | |
}; | |
Term = function Term() {}; | |
CurrentTerm = function CurrentTerm() { | |
return Term.apply(this, arguments); | |
}; | |
__extends(CurrentTerm, Term); | |
LongTerm = function LongTerm() { | |
return Term.apply(this, arguments); | |
}; | |
__extends(LongTerm, Term); | |
BalanceSheetEntry = function BalanceSheetEntry(type, subtype, term, amount) { | |
this.type = type; | |
this.subtype = subtype; | |
this.term = term; | |
this.amount = amount; | |
return this; | |
}; | |
BalanceSheetEntry.prototype.amount = function amount() { | |
return this.amount; | |
}; | |
BalanceSheetEntry.prototype.type = function type() { | |
return this.type; | |
}; | |
StatedEntry = function StatedEntry() { | |
return BalanceSheetEntry.apply(this, arguments); | |
}; | |
__extends(StatedEntry, BalanceSheetEntry); | |
AnalyzedEntry = function AnalyzedEntry() { | |
return BalanceSheetEntry.apply(this, arguments); | |
}; | |
__extends(AnalyzedEntry, BalanceSheetEntry); | |
balSheet = new BalanceSheet('XYZ Inc', new Date()); | |
balSheet.addAsset(new StatedEntry('asset', 'cash', CurrentTerm, 700000)); | |
balSheet.addAsset(new StatedEntry('asset', 'receivables', CurrentTerm, 2600000)); | |
balSheet.addAsset(new StatedEntry('asset', 'ppe', LongTerm, 60000000)); | |
balSheet.addLiability(new StatedEntry('liability', 'payables', CurrentTerm, 1500000)); | |
balSheet.addLiability(new StatedEntry('liability', 'mortgage', LongTerm, 10000000)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment