Skip to content

Commit 49aba51

Browse files
feat(Module): add info() method
The new `info()` method lets developers store arbitrary information about their module for consumption later. Closes angular#15225
1 parent e269c14 commit 49aba51

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

docs/content/error/ng/aobj.ngdoc

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@ngdoc error
2+
@name ng:aobj
3+
@fullName Invalid Argument
4+
@description
5+
6+
The argument passed should be an object. Check the value that was passed to the function where
7+
this error was thrown.

src/loader.js

+42
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ function setupModuleLoader(window) {
7979
* @returns {angular.Module} new module with the {@link angular.Module} api.
8080
*/
8181
return function module(name, requires, configFn) {
82+
83+
var info = {};
84+
8285
var assertNotHasOwnProperty = function(name, context) {
8386
if (name === 'hasOwnProperty') {
8487
throw ngMinErr('badname', 'hasOwnProperty is not a valid {0} name', context);
@@ -114,6 +117,45 @@ function setupModuleLoader(window) {
114117
_configBlocks: configBlocks,
115118
_runBlocks: runBlocks,
116119

120+
/**
121+
* @ngdoc method
122+
* @name angular.Module#info
123+
* @module ng
124+
*
125+
* @param {Object=} info Information about the module
126+
* @returns {Object|Module} The current info object for this module if called as a getter,
127+
* or `this` if called as a setter.
128+
*
129+
* @description
130+
* Read and write custom information about this module.
131+
* For example you could put the version of the module in here.
132+
*
133+
* ```js
134+
* angular.module('myModule', []).info({ version: '1.0.0' });
135+
* ```
136+
*
137+
* The version could then be read back out by accessing the module elsewhere:
138+
*
139+
* ```
140+
* var version = angular.module('myModule').info().version;
141+
* ```
142+
*
143+
* You can also retrieve this information during runtime via the
144+
* {@link $injector#modules `$injector.modules`} property:
145+
*
146+
* ```js
147+
* var version = $injector.modules['myModule'].info().version;
148+
* ```
149+
*/
150+
info: function(value) {
151+
if (isDefined(value)) {
152+
if (!isObject(value)) throw ngMinErr('aobj', 'Argument \'{0}\' must be an object', 'value');
153+
info = value;
154+
return this;
155+
}
156+
return info;
157+
},
158+
117159
/**
118160
* @ngdoc property
119161
* @name angular.Module#requires

test/loaderSpec.js

+31
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,35 @@ describe('module loader', function() {
156156
it('should expose `$$minErr` on the `angular` object', function() {
157157
expect(window.angular.$$minErr).toEqual(jasmine.any(Function));
158158
});
159+
160+
describe('Module', function() {
161+
describe('info()', function() {
162+
var theModule;
163+
164+
beforeEach(function() {
165+
theModule = angular.module('theModule', []);
166+
});
167+
168+
it('should default to an empty object', function() {
169+
expect(theModule.info()).toEqual({});
170+
});
171+
172+
it('should store the object passed as a param', function() {
173+
theModule.info({ version: '1.2' });
174+
expect(theModule.info()).toEqual({ version: '1.2' });
175+
});
176+
177+
it('should throw if the parameter is not an object', function() {
178+
expect(function() {
179+
theModule.info('some text');
180+
}).toThrowMinErr('ng', 'aobj');
181+
});
182+
183+
it('should completely replace the previous info object', function() {
184+
theModule.info({ value: 'X' });
185+
theModule.info({ newValue: 'Y' });
186+
expect(theModule.info()).toEqual({ newValue: 'Y' });
187+
});
188+
});
189+
});
159190
});

0 commit comments

Comments
 (0)