Skip to content

Commit

Permalink
Merge branch 'group'
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaa123 committed Jun 12, 2014
2 parents dea13ab + 317511d commit 04f6f37
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/catnap.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
module.exports.makeResource = require('./resource');
module.exports.makeGroup = require('./group');
47 changes: 47 additions & 0 deletions lib/group.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// # Group
//
// Group related resources together; and lets you retrieve them.
//
// ## Create a group
//
// * makeGroup - Constructs a new group.
//
// ## Group methods
//
// * add - Adds resources to the group
// * makeResource - Creates a new resource
//

var util = require('./utils'),
makeResource = require('./resource'),
variadic = util.variadic,
fluent = util.fluent,
maybe = util.maybe;

var makeGroup = module.exports = function () {
var _resources = {};

var _group = function (r) {
var res = _resources[r];

if (!res) throw new Error('Resource ' + r + ' not found.');
return res;
};

// Adds one or many resources to the group
_group.add = fluent(maybe(variadic(function (resources) {
resources.forEach(function (r) {
if (!r._name) {
throw new Error("Can't add Resource: " + r);
}

_resources[r._name] = r;
});
})));

_group.makeResource = function (name, path) {
return (_resources[name] = makeResource(name, path));
};

return _group;
};
54 changes: 54 additions & 0 deletions test/group_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
var assert = require('assert'),
should = require('should'),
sinon = require('sinon'),
makeGroup = require('../').makeGroup,
makeResource = require('../').makeResource;

describe('Group', function () {
describe('Create a group', function () {
it('Should create a new group', function () {
makeGroup().should.be.ok;
});
});

describe('Adding resources', function () {
var group;

beforeEach(function () {
group = makeGroup();
});

it('Should allow adding one or many resources', function () {
var one = makeResource('one', '/one'),
two = makeResource('two', '/two'),
three = makeResource('three', '/three');

group.add(one);
group.add(two, three);

group('one').should.eql(one);
group('two').should.eql(two);
group('three').should.eql(three);
});

it("Should throw if the Resource isn't available", function () {
[undefined, null, 'coffee'].forEach(function (v) {
assert.throws(function () {
group(v);
});
});
});

it('Should allow to conveniently create a resource', function () {
var newResource = group.makeResource('test', '/test');

// Makes sure the created Resource is a resource
newResource.should.be.a.Function;
['attachTo', 'representation'].forEach(function(m) {
newResource[m].should.be.ok;
});

group('test').should.eql(newResource);
});
});
});

0 comments on commit 04f6f37

Please sign in to comment.