|
1 /* |
|
2 Copyright (c) 2009, Yahoo! Inc. All rights reserved. |
|
3 Code licensed under the BSD License: |
|
4 http://developer.yahoo.net/yui/license.txt |
|
5 version: 3.0.0b1 |
|
6 build: 1163 |
|
7 */ |
|
8 YUI.add('dataschema-array', function(Y) { |
|
9 |
|
10 /** |
|
11 * Provides a DataSchema implementation which can be used to work with data stored in arrays. |
|
12 * |
|
13 * @module dataschema |
|
14 * @submodule dataschema-array |
|
15 */ |
|
16 |
|
17 /** |
|
18 * Array subclass for the DataSchema Utility. |
|
19 * @class DataSchema.Array |
|
20 * @extends DataSchema.Base |
|
21 * @static |
|
22 */ |
|
23 var LANG = Y.Lang, |
|
24 |
|
25 SchemaArray = { |
|
26 |
|
27 ///////////////////////////////////////////////////////////////////////////// |
|
28 // |
|
29 // DataSchema.Array static methods |
|
30 // |
|
31 ///////////////////////////////////////////////////////////////////////////// |
|
32 /** |
|
33 * Applies a given schema to given Array data. |
|
34 * |
|
35 * @method apply |
|
36 * @param schema {Object} Schema to apply. |
|
37 * @param data {Object} Array data. |
|
38 * @return {Object} Schema-parsed data. |
|
39 * @static |
|
40 */ |
|
41 apply: function(schema, data) { |
|
42 var data_in = data, |
|
43 data_out = {results:[],meta:{}}; |
|
44 |
|
45 if(LANG.isArray(data_in)) { |
|
46 if(LANG.isArray(schema.resultFields)) { |
|
47 // Parse results data |
|
48 data_out = SchemaArray._parseResults(schema.resultFields, data_in, data_out); |
|
49 } |
|
50 else { |
|
51 data_out.results = data_in; |
|
52 Y.log("Schema resultFields property not found: " + Y.dump(schema), "warn", "dataschema-array"); |
|
53 } |
|
54 } |
|
55 else { |
|
56 Y.log("Array data could not be schema-parsed: " + Y.dump(data) + " " + Y.dump(data), "error", "dataschema-array"); |
|
57 data_out.error = new Error("Array schema parse failure"); |
|
58 } |
|
59 |
|
60 return data_out; |
|
61 }, |
|
62 |
|
63 /** |
|
64 * Schema-parsed list of results from full data |
|
65 * |
|
66 * @method _parseResults |
|
67 * @param fields {Array} Schema to parse against. |
|
68 * @param array_in {Array} Array to parse. |
|
69 * @param data_out {Object} In-progress parsed data to update. |
|
70 * @return {Object} Parsed data object. |
|
71 * @static |
|
72 * @protected |
|
73 */ |
|
74 _parseResults: function(fields, array_in, data_out) { |
|
75 var results = [], |
|
76 result, item, type, field, key, value, i, j; |
|
77 |
|
78 for(i=array_in.length-1; i>-1; i--) { |
|
79 result = {}; |
|
80 item = array_in[i]; |
|
81 type = (LANG.isObject(item) && !LANG.isFunction(item)) ? 2 : (LANG.isArray(item)) ? 1 : (LANG.isString(item)) ? 0 : -1; |
|
82 if(type > 0) { |
|
83 for(j=fields.length-1; j>-1; j--) { |
|
84 field = fields[j]; |
|
85 key = (!LANG.isUndefined(field.key)) ? field.key : field; |
|
86 value = (!LANG.isUndefined(item[key])) ? item[key] : item[j]; |
|
87 result[key] = Y.DataSchema.Base.parse(value, field); |
|
88 } |
|
89 } |
|
90 else if(type === 0) { |
|
91 result = item; |
|
92 } |
|
93 else { |
|
94 //TODO: null or {}? |
|
95 result = null; |
|
96 Y.log("Unexpected type while parsing array: " + Y.dump(item), "warn", "dataschema-array"); |
|
97 } |
|
98 results[i] = result; |
|
99 } |
|
100 data_out.results = results; |
|
101 |
|
102 return data_out; |
|
103 } |
|
104 }; |
|
105 |
|
106 Y.DataSchema.Array = Y.mix(SchemaArray, Y.DataSchema.Base); |
|
107 |
|
108 |
|
109 |
|
110 }, '3.0.0b1' ,{requires:['dataschema-base']}); |