Skip to content

Commit

Permalink
No longer expose properties directly as part of the API
Browse files Browse the repository at this point in the history
See previous commit message for details.
  • Loading branch information
cpettitt committed Sep 24, 2014
1 parent bf6bca0 commit c617bc5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
23 changes: 18 additions & 5 deletions lib/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ function Graph(opts) {
this._isCompound = opts ? opts.compound: false;

// Attributes for the graph itself
this.attrs = {};
this._attrs = {};

// Defaults to be applied when creating a new node
this.nodeAttrDefs = {};
this._nodeAttrDefs = {};

// Defaults to be applied when creating a new edge
this.edgeAttrDefs = {};
this._edgeAttrDefs = {};

// v -> attrs
this._nodes = {};
Expand Down Expand Up @@ -86,8 +86,17 @@ Graph.prototype.isCompound = function() {
return this._isCompound;
};

Graph.prototype.graph = function() {
return this._attrs;
};


/* === Node functions ========== */

Graph.prototype.nodeAttrDefs = function() {
return this._nodeAttrDefs;
};

Graph.prototype.nodeCount = function() {
return this._nodeCount;
};
Expand All @@ -112,7 +121,7 @@ Graph.prototype.node = function(v) {
var nodes = this._nodes,
node = nodes[v];
if (!node) {
node = nodes[v] = _.merge({}, this.nodeAttrDefs);
node = nodes[v] = _.merge({}, this._nodeAttrDefs);
if (this._isCompound) {
this._parent[v] = GRAPH_NODE;
this._children[v] = {};
Expand Down Expand Up @@ -225,6 +234,10 @@ Graph.prototype.neighbors = function(v) {

/* === Edge functions ========== */

Graph.prototype.edgeAttrDefs = function() {
return this._edgeAttrDefs;
};

Graph.prototype.edgeCount = function() {
return this._edgeCount;
};
Expand Down Expand Up @@ -268,7 +281,7 @@ Graph.prototype.edge = function(v, w, name) {
this.node(v);
this.node(w);

edge = this._edgeAttrs[e] = _.merge({}, this.edgeAttrDefs);
edge = this._edgeAttrs[e] = _.merge({}, this._edgeAttrDefs);

var edgeObj = edgeIdToObj(e);
Object.freeze(edgeObj);
Expand Down
13 changes: 10 additions & 3 deletions test/graph-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe("Graph", function() {
});

it("has no attributes", function() {
expect(g.attrs).to.eql({});
expect(g.graph()).to.eql({});
});

it("defaults to a simple directed graph", function() {
Expand All @@ -44,6 +44,13 @@ describe("Graph", function() {
});
});

describe("graph", function() {
it("can be used to get and set properties for the graph", function() {
g.graph().foo = 1;
expect(g.graph().foo).to.equal(1);
});
});

describe("nodes", function() {
it("is empty if there are no nodes in the graph", function() {
expect(g.nodes()).to.eql([]);
Expand Down Expand Up @@ -86,7 +93,7 @@ describe("Graph", function() {
});

it("creates the node with default attributes, if set", function() {
g.nodeAttrDefs.foo = 1;
g.nodeAttrDefs().foo = 1;
expect(g.node("a").foo).to.equal(1);
});

Expand Down Expand Up @@ -355,7 +362,7 @@ describe("Graph", function() {
});

it("creates the node with default attributes, if set", function() {
g.edgeAttrDefs.foo = 1;
g.edgeAttrDefs().foo = 1;
expect(g.edge("a", "b").foo).to.equal(1);
});

Expand Down

0 comments on commit c617bc5

Please sign in to comment.