Created
January 20, 2016 05:07
-
-
Save gregorynicholas/57fe1aca17828c5c0a20 to your computer and use it in GitHub Desktop.
Fixtures factory service for Meteor
This file contains hidden or 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
/* globals Fixtures: true, Assets: false */ | |
/** | |
* Fixtures service for creating new fixture factories. | |
*/ | |
Fixtures = { | |
/** | |
* Creates and registers a new fixture factory. | |
* @param name The name of the factory. | |
* @param path The path to the fixture file relative to private/fixtures/. | |
* @returns {*} The new factory. | |
*/ | |
factory: function (name, path) { | |
var factory = Fixtures.createFactory(path); | |
Fixtures.registerFactory(name, factory); | |
return factory; | |
}, | |
/** | |
* Creates a new factory. | |
* @param path The path to the fixture file relative to private/fixtures/. | |
* @returns {{build: Function}} The factory object has a build method to get a new | |
* fixture. You can also pass customData to build, to extend the default fixture. | |
*/ | |
createFactory: function (path) { | |
return { | |
build: function (customData) { | |
return _.extend({}, Fixtures.getFixtureData(path), customData); | |
} | |
}; | |
}, | |
/** | |
* Registers a factory | |
* @param name The name that should be used. It is available under Fixtures[name]. | |
* @param factory The existing factory object that should be registered. | |
*/ | |
registerFactory: function (name, factory) { | |
Fixtures[name] = factory; | |
}, | |
/** | |
* Get the data of a fixture JSON file. | |
* @param path The path relative to private/fixtures/. | |
* @returns {*} The fixture object. | |
*/ | |
getFixtureData: function (path) { | |
var json = Assets.getText('fixtures/' + path); | |
return JSON.parse(json); | |
} | |
}; | |
// Our concrete fixture factories | |
Fixtures.factory('Event', 'event.json'); | |
Fixtures.factory('Profile', 'profile.json'); | |
Fixtures.factory('PublisherProfile', 'publisher-profile.json'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment