Henning Snater has submitted this change and it was merged.

Change subject: DataType constructor taking DataValue constructors as 2nd 
argument now
......................................................................


DataType constructor taking DataValue constructors as 2nd argument now

If a DataValue is given as second parameter to the constructor, then the data 
value type will be
taken from the constructor. This should be the preferred way of creating a data 
value type.

Change-Id: I61cdcabd4112ae1ea69dcd22d23ed24e0c69dfeb
---
M DataTypes/Resources.php
M DataTypes/resources/dataTypes.js
M DataTypes/tests/qunit/dataTypes.DataType.tests.js
3 files changed, 25 insertions(+), 15 deletions(-)

Approvals:
  Henning Snater: Verified; Looks good to me, approved



diff --git a/DataTypes/Resources.php b/DataTypes/Resources.php
index 9896879..710ac96 100644
--- a/DataTypes/Resources.php
+++ b/DataTypes/Resources.php
@@ -40,7 +40,7 @@
                        'scripts' => 'dataTypes.js', // also contains 
dataType.DataType constructor
                        'dependencies' => array(
                                'dataTypes.dataTypesModule',
-                               'dataValues',
+                               'dataValues.DataValue',
                                'valueParsers'
                        ),
                ),
diff --git a/DataTypes/resources/dataTypes.js b/DataTypes/resources/dataTypes.js
index 5ab6bcb..1b8502e 100644
--- a/DataTypes/resources/dataTypes.js
+++ b/DataTypes/resources/dataTypes.js
@@ -15,7 +15,7 @@
  * @since 0.1
  * @type Object
  */
-var dataTypes = new ( function Dt( $, mw ) {
+var dataTypes = new ( function Dt( $, mw, DataValue ) {
        'use strict';
 
        // TODO: the whole structure of this is a little weird, perhaps there 
should be a
@@ -32,18 +32,22 @@
         * @since 0.1
         *
         * @param {String} typeId
-        * @param {String} dataValueType
-        * @param {vp.Parser} parser
+        * @param {String|Function} dataValueType Can be a DataValue 
constructor whose type will then be taken.
+        * @param {vp.ValueParser} parser
         * @param {Object} formatter
         * @param {Object} validators
         */
        var SELF = dt.DataType = function DtDataType( typeId, dataValueType, 
parser, formatter, validators ) {
-               // TODO: enforce the requirement or remove it after we 
implemented and use all of the parts
-               if( dataValueType === undefined ) {
-                       throw new Error( 'All arguments must be provided for 
creating a new DataType object' );
+               if( dataValueType && dataValueType.prototype instanceof 
DataValue
+               ) { // DataValue constructor
+                       dataValueType = dataValueType.TYPE;
                }
+               if( typeof dataValueType !== 'string' ) {
+                       throw new Error( 'A data value type has to be given in 
form of a string or DataValue constructor' );
+               }
+
                if( !typeId || typeof typeId !== 'string' ) {
-                       throw new Error( 'A dataType.DataType\'s ID has to be a 
string' );
+                       throw new Error( 'A data type\'s ID has to be a string' 
);
                }
 
                this._typeId = typeId;
@@ -78,7 +82,7 @@
                 * Returns the ValueParser used by this data type.
                 * @since 0.1
                 *
-                * @return dv.ValueParser
+                * @return vp.ValueParser
                 */
                getParser: function() {
                        return this._parser;
@@ -180,6 +184,6 @@
                dts[ dataType.getId() ] = dataType;
        };
 
-} )( jQuery, mediaWiki );
+} )( jQuery, mediaWiki, dataValues.DataValue );
 
 window.dataTypes = dataTypes; // global alias
diff --git a/DataTypes/tests/qunit/dataTypes.DataType.tests.js 
b/DataTypes/tests/qunit/dataTypes.DataType.tests.js
index 59d3bce..ef96bcf 100644
--- a/DataTypes/tests/qunit/dataTypes.DataType.tests.js
+++ b/DataTypes/tests/qunit/dataTypes.DataType.tests.js
@@ -9,7 +9,7 @@
  * @author Daniel Werner < [email protected] >
  */
 
-( function( dt, $, QUnit ) {
+( function( dt, dv, $, QUnit ) {
        'use strict';
 
        var DataType = dt.DataType;
@@ -22,9 +22,9 @@
                        constructorParams: [ 'foo', 'string' ],
                        valueType: 'string'
                }, {
-                       title: 'another simple datatype',
-                       constructorParams: [ 'bar', 'bool' ],
-                       valueType: 'bool'
+                       title: 'DataType constructed with DataValue as 2nd 
argument',
+                       constructorParams: [ 'bar', dv.BoolValue ],
+                       valueType: dv.BoolValue.TYPE
                }
        ];
 
@@ -76,6 +76,12 @@
                }, {
                        title: 'missing data value type',
                        constructorParams: [ 'foo' ]
+               }, {
+                       title: 'wrong type for data value type',
+                       constructorParams: [ 'foo', {} ]
+               }, {
+                       title: 'wrong type for ID',
+                       constructorParams: [ null, 'xxx' ]
                }
        ] )
                .test( 'invalid construtor arguments', function( params, assert 
) {
@@ -87,4 +93,4 @@
                        );
                } );
 
-}( dataTypes, jQuery, QUnit ) );
+}( dataTypes, dataValues, jQuery, QUnit ) );

-- 
To view, visit https://gerrit.wikimedia.org/r/60810
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I61cdcabd4112ae1ea69dcd22d23ed24e0c69dfeb
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/DataValues
Gerrit-Branch: master
Gerrit-Owner: Daniel Werner <[email protected]>
Gerrit-Reviewer: Henning Snater <[email protected]>
Gerrit-Reviewer: Tobias Gritschacher <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to