Skip to content

Commit

Permalink
Merge pull request #6 from abiyasa/simplify-node
Browse files Browse the repository at this point in the history
Simplify node definition
  • Loading branch information
BrettJephson committed Jun 10, 2013
2 parents 2440fc1 + 20291b7 commit 5369737
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 47 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ash-framework",
"version": "0.1.0",
"version": "0.2.0",
"description": "JavaScript port of Ash framework",
"private": true,
"devDependencies" : {
Expand Down
2 changes: 1 addition & 1 deletion src/ash/ash-framework.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
define(function (require) {
var core = {
VERSION: '0.1.0'
VERSION: '0.2.0'
};

core.Engine = require('ash-core/engine');
Expand Down
44 changes: 43 additions & 1 deletion src/ash/core/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,51 @@ define([
entity: null,
previous: null,
next: null,

constructor: function () { }
});

/**
* A simpler way to create a node.
*
* Example: creating a node for component classes Point & energy:
*
* var PlayerNode = Ash.Node.create({
* point: Point,
* energy: Energy
* });
*
* This is the simpler version from:
*
* var PlayerNode = Ash.Node.extend({
* point: null,
* energy: null,
*
* types: {
* point: Point,
* energy: Energy
* }
* });
*/
Node.create = function (schema) {
var processedSchema = {
types: {},
constructor: function () { }
};

// process schema
for (var propertyName in schema) {
if (schema.hasOwnProperty(propertyName)) {
var propertyType = schema[propertyName];
if (propertyType) {
processedSchema.types[propertyName] = propertyType;
}
processedSchema[propertyName] = null;
}
}

return Node.extend(processedSchema);
};

return Node;
});
8 changes: 2 additions & 6 deletions test/spec/componentmatchingfamily.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@ define ([
var engine, family;

// prepare MockNode
var MockNode = Ash.Node.extend({
point: null,
types: {
point: Point
},
constructor: function () { }
var MockNode = Ash.Node.create({
point: Point
});

module("Test Component Matching Family", {
Expand Down
17 changes: 15 additions & 2 deletions test/spec/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,25 @@ define ([
// prepare MockNodes
var MockNode = Ash.Node.extend({
point: null,
constructor: function () { }
types: {
point: Point
},
constructor: function (x, y) {
x = x || 0;
y = y || 0;
this.point = new Point(x, y);
}
});

var MockNode2 = MockNode.extend({
point: null,
matrix: null,
constructor: function () { }
types: {
point: Point
},
constructor: function () {
MockNode.super.constructor.call(this);
}
});

var MockFamily = Ash.Family.extend({
Expand Down
80 changes: 44 additions & 36 deletions test/spec/nodelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@
* Testing NodeList
*/
define ([
'ash-framework'
], function(Ash) {
'ash-framework',
'point'
], function(Ash, Point) {
'use strict';

var nodes, tempNode;

// prepare mock node
var MockNode = Ash.Node.extend({
constructor: function (value) {
this.pos = value || 0;
point: null,
types: {
point: Point
},
constructor: function (x, y) {
x = x || 0;
y = y || 0;
this.point = new Point(x, y);
}
});

Expand Down Expand Up @@ -198,10 +205,10 @@ define ([
});

test("insertionSortCorrectlySortsSortedNodes", function() {
var node1 = new MockNode( 1 ),
node2 = new MockNode( 2 ),
node3 = new MockNode( 3 ),
node4 = new MockNode( 4 );
var node1 = new MockNode(1, 0),
node2 = new MockNode(2, 0),
node3 = new MockNode(3, 0),
node4 = new MockNode(4, 0);
nodes.add( node1 );
nodes.add( node2 );
nodes.add( node3 );
Expand All @@ -211,10 +218,10 @@ define ([
});

test("insertionSortCorrectlySortsReversedNodes", function() {
var node1 = new MockNode( 1 ),
node2 = new MockNode( 2 ),
node3 = new MockNode( 3 ),
node4 = new MockNode( 4 );
var node1 = new MockNode(1, 0),
node2 = new MockNode(2, 0),
node3 = new MockNode(3, 0),
node4 = new MockNode(4, 0);
nodes.add( node4 );
nodes.add( node3 );
nodes.add( node2 );
Expand All @@ -224,11 +231,11 @@ define ([
});

test("insertionSortCorrectlySortsMixedNodes", function() {
var node1 = new MockNode( 1 ),
node2 = new MockNode( 2 ),
node3 = new MockNode( 3 ),
node4 = new MockNode( 4 ),
node5 = new MockNode( 5 );
var node1 = new MockNode(1, 0),
node2 = new MockNode(2, 0),
node3 = new MockNode(3, 0),
node4 = new MockNode(4, 0),
node5 = new MockNode(5, 0);
nodes.add( node3 );
nodes.add( node4 );
nodes.add( node1 );
Expand All @@ -239,11 +246,11 @@ define ([
});

test("insertionSortRetainsTheOrderOfEquivalentNodes", function() {
var node1 = new MockNode( 1 ),
node2 = new MockNode( 2 ),
node3 = new MockNode( 3 ),
node4 = new MockNode( 4 ),
node5 = new MockNode( 4 );
var node1 = new MockNode(1, 0),
node2 = new MockNode(2, 0),
node3 = new MockNode(3, 0),
node4 = new MockNode(4, 0),
node5 = new MockNode(4, 0);
nodes.add( node3 );
nodes.add( node4 );
nodes.add( node1 );
Expand All @@ -254,10 +261,10 @@ define ([
});

test("mergeSortCorrectlySortsSortedNodes", function() {
var node1 = new MockNode( 1 ),
node2 = new MockNode( 2 ),
node3 = new MockNode( 3 ),
node4 = new MockNode( 4 );
var node1 = new MockNode(1, 0),
node2 = new MockNode(2, 0),
node3 = new MockNode(3, 0),
node4 = new MockNode(4, 0);
nodes.add( node1 );
nodes.add( node2 );
nodes.add( node3 );
Expand All @@ -267,10 +274,10 @@ define ([
});

test("mergeSortCorrectlySortsReversedNodes", function() {
var node1 = new MockNode( 1 ),
node2 = new MockNode( 2 ),
node3 = new MockNode( 3 ),
node4 = new MockNode( 4 );
var node1 = new MockNode(1, 0),
node2 = new MockNode(2, 0),
node3 = new MockNode(3, 0),
node4 = new MockNode(4, 0);
nodes.add( node4 );
nodes.add( node3 );
nodes.add( node2 );
Expand All @@ -280,11 +287,11 @@ define ([
});

test("mergeSortCorrectlySortsMixedNodes", function() {
var node1 = new MockNode( 1 ),
node2 = new MockNode( 2 ),
node3 = new MockNode( 3 ),
node4 = new MockNode( 4 ),
node5 = new MockNode( 5 );
var node1 = new MockNode(1, 0),
node2 = new MockNode(2, 0),
node3 = new MockNode(3, 0),
node4 = new MockNode(4, 0),
node5 = new MockNode(5, 0);
nodes.add( node3 );
nodes.add( node4 );
nodes.add( node1 );
Expand All @@ -295,7 +302,8 @@ define ([
});

function sortFunction( node1, node2 ) {
return node1.pos - node2.pos;
// sort based on x value
return node1.point.x - node2.point.x;
}

function testNodeOrder( nodes, nodeArray ) {
Expand Down

0 comments on commit 5369737

Please sign in to comment.