Added value retrieval to package builder.
This patch adds getValues and getValue to the package builder library, to allow retrieval of package-level variables from an authoritative source during the various project configuraiton stages.
This commit is contained in:
		@@ -1,4 +1,4 @@
 | 
			
		||||
(function() {
 | 
			
		||||
(function () {
 | 
			
		||||
  'use strict';
 | 
			
		||||
 | 
			
		||||
  var pkgContent = {};
 | 
			
		||||
@@ -37,9 +37,33 @@
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Get the values of the current package.
 | 
			
		||||
   *
 | 
			
		||||
   * @returns {{}} A cloned map of the values.
 | 
			
		||||
   */
 | 
			
		||||
  function getValues () {
 | 
			
		||||
    return JSON.parse(JSON.stringify(pkgContent));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Get a specific value from the package.json file.
 | 
			
		||||
   *
 | 
			
		||||
   * @param {String} name The name of the value.
 | 
			
		||||
   * @returns {{}} A clone of the referenced value.
 | 
			
		||||
   */
 | 
			
		||||
  function getValue (name) {
 | 
			
		||||
    if (pkgContent.hasOwnProperty(name)) {
 | 
			
		||||
      return JSON.parse(JSON.stringify(pkgContent[name]));
 | 
			
		||||
    }
 | 
			
		||||
    return undefined;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  module.exports = {
 | 
			
		||||
    fromJSON: readPackage,
 | 
			
		||||
    toJSON: writePackage,
 | 
			
		||||
    setValues: setValues
 | 
			
		||||
    setValues: setValues,
 | 
			
		||||
    getValues: getValues,
 | 
			
		||||
    getValue: getValue
 | 
			
		||||
  };
 | 
			
		||||
})();
 | 
			
		||||
 
 | 
			
		||||
@@ -1,20 +1,20 @@
 | 
			
		||||
(function() {
 | 
			
		||||
(function () {
 | 
			
		||||
  'use strict';
 | 
			
		||||
  var builder = require('../../../generators/app/lib/pkg_builder');
 | 
			
		||||
 | 
			
		||||
  describe('generator-openstack:lib/pkg_builder', function() {
 | 
			
		||||
  describe('generator-openstack:lib/pkg_builder', function () {
 | 
			
		||||
 | 
			
		||||
    beforeEach(function() {
 | 
			
		||||
    beforeEach(function () {
 | 
			
		||||
      builder.fromJSON("{}"); // Clear
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    it('should start as an empty object',
 | 
			
		||||
      function() {
 | 
			
		||||
      function () {
 | 
			
		||||
        expect(builder.toJSON()).toBe("{}");
 | 
			
		||||
      });
 | 
			
		||||
 | 
			
		||||
    it('should honor and echo back any pre-loaded package file',
 | 
			
		||||
      function() {
 | 
			
		||||
      function () {
 | 
			
		||||
        var packageString = '{"name":"foo"}';
 | 
			
		||||
        builder.fromJSON(packageString);
 | 
			
		||||
 | 
			
		||||
@@ -23,7 +23,7 @@
 | 
			
		||||
      });
 | 
			
		||||
 | 
			
		||||
    it('should permit adding and overriding values.',
 | 
			
		||||
      function() {
 | 
			
		||||
      function () {
 | 
			
		||||
        builder.fromJSON('{"name":"foo"}');
 | 
			
		||||
        builder.setValues({name: "bar", lol: "cat"});
 | 
			
		||||
 | 
			
		||||
@@ -33,7 +33,7 @@
 | 
			
		||||
      });
 | 
			
		||||
 | 
			
		||||
    it('should not add parent prototype values.',
 | 
			
		||||
      function() {
 | 
			
		||||
      function () {
 | 
			
		||||
        function Thing () {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -51,5 +51,27 @@
 | 
			
		||||
        expect(parsedResult.lol).toBe("cat");
 | 
			
		||||
        expect(parsedResult.foo).toBeUndefined();
 | 
			
		||||
      });
 | 
			
		||||
 | 
			
		||||
    describe('getValues()', function () {
 | 
			
		||||
      it('should permit retrieving the entire package block.',
 | 
			
		||||
        function () {
 | 
			
		||||
          builder.fromJSON('{"name":"foo"}');
 | 
			
		||||
          expect(builder.getValues()).toEqual({name: 'foo'});
 | 
			
		||||
        });
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    describe('getValue()', function () {
 | 
			
		||||
      it('should permit retrieving values from the package.',
 | 
			
		||||
        function () {
 | 
			
		||||
          builder.fromJSON('{"name":"foo"}');
 | 
			
		||||
          expect(builder.getValue('name')).toBe('foo');
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
      it('should return undefined if the value is not set.',
 | 
			
		||||
        function () {
 | 
			
		||||
          builder.fromJSON('{"name":"foo"}');
 | 
			
		||||
          expect(builder.getValue('invalidname')).toBeUndefined();
 | 
			
		||||
        });
 | 
			
		||||
    });
 | 
			
		||||
  });
 | 
			
		||||
})();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user