-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
module.exports.makeResource = require('./resource'); | ||
module.exports.makeGroup = require('./group'); |
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
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; | ||
}; |
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
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); | ||
}); | ||
}); | ||
}); |