Skip to content

Commit 6567604

Browse files
committed
Adding chapter 11 code
1 parent 4732563 commit 6567604

File tree

10 files changed

+201
-0
lines changed

10 files changed

+201
-0
lines changed

Chapter 11/AOP.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var GoldTransfer = (function () {
2+
function GoldTransfer() {
3+
}
4+
GoldTransfer.prototype.SendPaymentOfGold = function (amountOfGold, destination) {
5+
var user = Security.GetCurrentUser();
6+
if (Security.IsAuthorized(user, "SendPaymentOfGold")) {
7+
//send actual payment
8+
} else {
9+
return { success: 0, message: "Unauthorized" };
10+
}
11+
};
12+
return GoldTransfer;
13+
})();
14+
15+
var Security = (function () {
16+
function Security() {
17+
}
18+
Security.IsAuthorized = function (user, functionPoint) {
19+
};
20+
Security.GetCurrentUser = function () {
21+
};
22+
return Security;
23+
})();

Chapter 11/AOP.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class GoldTransfer{
2+
SendPaymentOfGold(amountOfGold, destination){
3+
var user = Security.GetCurrentUser();
4+
if(Security.IsAuthorized(user, "SendPaymentOfGold")){
5+
//send actual payment
6+
}
7+
else{
8+
return { success: 0, message:"Unauthorized"};
9+
}
10+
}
11+
}
12+
13+
class Security{
14+
static IsAuthorized(user, functionPoint){}
15+
static GetCurrentUser(){}
16+
}

Chapter 11/AspectWeaver.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var ToWeaveIn = (function () {
2+
function ToWeaveIn() {
3+
}
4+
ToWeaveIn.prototype.BeforeCall = function () {
5+
console.log("Before!");
6+
};
7+
ToWeaveIn.prototype.AfterCall = function () {
8+
console.log("After!");
9+
};
10+
return ToWeaveIn;
11+
})();
12+
13+
var GoldTransfer = (function () {
14+
function GoldTransfer() {
15+
}
16+
GoldTransfer.prototype.SendPaymentOfGold = function (amountOfGold, destination) {
17+
/* @aspect(Security)*/
18+
//send actual payment
19+
console.log("Payment sent");
20+
};
21+
return GoldTransfer;
22+
})();
23+
24+
function weave(toWeave, toWeaveIn, toWeaveInName) {
25+
for (var property in toWeave.prototype) {
26+
var stringRepresentation = toWeave.prototype[property].toString();
27+
28+
console.log(stringRepresentation);
29+
if (stringRepresentation.indexOf("@aspect(" + toWeaveInName + ")") >= 0) {
30+
toWeave.prototype[property + "_wrapped"] = toWeave.prototype[property];
31+
toWeave.prototype[property] = function () {
32+
toWeaveIn.BeforeCall();
33+
toWeave.prototype[property + "_wrapped"]();
34+
toWeaveIn.AfterCall();
35+
};
36+
}
37+
}
38+
}
39+
40+
weave(GoldTransfer, new ToWeaveIn(), "Security");
41+
var transfer = new GoldTransfer();
42+
transfer.SendPaymentOfGold(50, "Starks");

Chapter 11/AspectWeaver.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class ToWeaveIn{
2+
BeforeCall(){
3+
console.log("Before!");
4+
}
5+
AfterCall(){
6+
console.log("After!");
7+
}
8+
}
9+
10+
class GoldTransfer{
11+
12+
SendPaymentOfGold(amountOfGold, destination){
13+
/* @aspect(Security)*/
14+
//send actual payment
15+
console.log("Payment sent");
16+
}
17+
}
18+
19+
function weave(toWeave, toWeaveIn, toWeaveInName){
20+
for (var property in toWeave.prototype) {
21+
var stringRepresentation = toWeave.prototype[property].toString();
22+
23+
console.log(stringRepresentation);
24+
if(stringRepresentation.indexOf("@aspect(" + toWeaveInName + ")") >= 0)
25+
{
26+
toWeave.prototype[property + "_wrapped"] = toWeave.prototype[property];
27+
toWeave.prototype[property] = function(){ toWeaveIn.BeforeCall();
28+
toWeave.prototype[property + "_wrapped"]();
29+
toWeaveIn.AfterCall();
30+
}
31+
}
32+
}
33+
}
34+
35+
weave(GoldTransfer, new ToWeaveIn(), "Security");
36+
var transfer = new GoldTransfer();
37+
transfer.SendPaymentOfGold(50, "Starks");

Chapter 11/DependencyInjection.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var UserManager = (function () {
2+
function UserManager(database, userEmailer) {
3+
this.database = database;
4+
this.userEmailer = userEmailer;
5+
}
6+
return UserManager;
7+
})();
8+
9+
var Database = (function () {
10+
function Database() {
11+
}
12+
return Database;
13+
})();
14+
15+
var UserEmailer = (function () {
16+
function UserEmailer() {
17+
}
18+
return UserEmailer;
19+
})();

Chapter 11/DependencyInjection.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class UserManager{
2+
constructor(public database, public userEmailer){
3+
4+
}
5+
}
6+
7+
class Database{}
8+
9+
class UserEmailer{}

Chapter 11/dsl.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var Axe = (function () {
2+
function Axe(handleLength, /*public*/ headHeight) {
3+
}
4+
return Axe;
5+
})();
6+
7+
function publicParameters(func) {
8+
var stringRepresentation = func.toString();
9+
var matches = stringRepresentation.match(/^function .*\((.*)\)/);
10+
var parameterString = matches[1];
11+
var parameters = parameterString.split(",");
12+
var setterString = "";
13+
for (var i = 0; i < parameters.length; i++) {
14+
if (parameters[i].indexOf("public") >= 0) {
15+
var parameterName = parameters[i].split('/')[parameters[i].split('/').length - 1].trim();
16+
setterString += "this." + parameterName + " = " + parameterName + ";\n";
17+
}
18+
}
19+
var functionParts = stringRepresentation.match(/(^.*{)([\s\S]*)/);
20+
return functionParts[1] + setterString + functionParts[2];
21+
}
22+
23+
console.log(publicParameters(Axe));
24+
eval(publicParameters(Axe));

Chapter 11/dsl.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Axe{
2+
constructor(handleLength, /*public*/ headHeight){}
3+
}
4+
5+
function publicParameters(func){
6+
var stringRepresentation = func.toString();
7+
var parameterString = stringRepresentation.match(/^function .*\((.*)\)/)[1];
8+
var parameters = parameterString.split(",");
9+
var setterString = "";
10+
for(var i = 0; i < parameters.length; i++){
11+
if(parameters[i].indexOf("public") >= 0){
12+
var parameterName = parameters[i].split('/')[parameters[i].split('/').length-1].trim();
13+
setterString += "this." + parameterName + " = " + parameterName + ";\n";
14+
}
15+
}
16+
var functionParts = stringRepresentation.match(/(^.*{)([\s\S]*)/);
17+
return functionParts[1] + setterString + functionParts[2];
18+
}
19+
20+
console.log(publicParameters(Axe));
21+
eval (publicParameters(Axe));

Chapter 11/public_arguments.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var Axe = (function () {
2+
function Axe(handleLength, headHeight) {
3+
this.handleLength = handleLength;
4+
this.headHeight = headHeight;
5+
}
6+
return Axe;
7+
})();

Chapter 11/public_arguments.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Axe{
2+
constructor(public handleLength, public headHeight){}
3+
}

0 commit comments

Comments
 (0)