Skip to content

Commit

Permalink
feat(Module): add info() method
Browse files Browse the repository at this point in the history
The new `info()` method lets developers store arbitrary information about
their module for consumption later.

Closes angular#15225
  • Loading branch information
petebacondarwin committed Mar 2, 2017
1 parent e269c14 commit 49aba51
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docs/content/error/ng/aobj.ngdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@ngdoc error
@name ng:aobj
@fullName Invalid Argument
@description

The argument passed should be an object. Check the value that was passed to the function where
this error was thrown.
42 changes: 42 additions & 0 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ function setupModuleLoader(window) {
* @returns {angular.Module} new module with the {@link angular.Module} api.
*/
return function module(name, requires, configFn) {

var info = {};

var assertNotHasOwnProperty = function(name, context) {
if (name === 'hasOwnProperty') {
throw ngMinErr('badname', 'hasOwnProperty is not a valid {0} name', context);
Expand Down Expand Up @@ -114,6 +117,45 @@ function setupModuleLoader(window) {
_configBlocks: configBlocks,
_runBlocks: runBlocks,

/**
* @ngdoc method
* @name angular.Module#info
* @module ng
*
* @param {Object=} info Information about the module
* @returns {Object|Module} The current info object for this module if called as a getter,
* or `this` if called as a setter.
*
* @description
* Read and write custom information about this module.
* For example you could put the version of the module in here.
*
* ```js
* angular.module('myModule', []).info({ version: '1.0.0' });
* ```
*
* The version could then be read back out by accessing the module elsewhere:
*
* ```
* var version = angular.module('myModule').info().version;
* ```
*
* You can also retrieve this information during runtime via the
* {@link $injector#modules `$injector.modules`} property:
*
* ```js
* var version = $injector.modules['myModule'].info().version;
* ```
*/
info: function(value) {
if (isDefined(value)) {
if (!isObject(value)) throw ngMinErr('aobj', 'Argument \'{0}\' must be an object', 'value');
info = value;
return this;
}
return info;
},

/**
* @ngdoc property
* @name angular.Module#requires
Expand Down
31 changes: 31 additions & 0 deletions test/loaderSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,35 @@ describe('module loader', function() {
it('should expose `$$minErr` on the `angular` object', function() {
expect(window.angular.$$minErr).toEqual(jasmine.any(Function));
});

describe('Module', function() {
describe('info()', function() {
var theModule;

beforeEach(function() {
theModule = angular.module('theModule', []);
});

it('should default to an empty object', function() {
expect(theModule.info()).toEqual({});
});

it('should store the object passed as a param', function() {
theModule.info({ version: '1.2' });
expect(theModule.info()).toEqual({ version: '1.2' });
});

it('should throw if the parameter is not an object', function() {
expect(function() {
theModule.info('some text');
}).toThrowMinErr('ng', 'aobj');
});

it('should completely replace the previous info object', function() {
theModule.info({ value: 'X' });
theModule.info({ newValue: 'Y' });
expect(theModule.info()).toEqual({ newValue: 'Y' });
});
});
});
});

0 comments on commit 49aba51

Please sign in to comment.