1"use strict"; 2(function( helper, Apperyio, undefined ){ 3 4var $extend = null; 5if ( helper.extend != undefined ) { 6 $extend = (helper instanceof jQuery) ? helper.extend.bind(helper, true) : helper.extend; 7} 8 9function EntityError( message ){ 10 this.name = "EntityError"; 11 this.message = message; 12}; 13 14EntityError.prototype = new Error(); 15EntityError.prototype.constructor = EntityError; 16 17function TypeNotFoundError( message ){ 18 this.name = "TypeNotFoundError"; 19 this.message = message; 20} 21 22TypeNotFoundError.prototype = new EntityError(); 23TypeNotFoundError.prototype.constructor = TypeNotFoundError; 24 25function NoModelError( message ){ 26 this.name = "NoModelError"; 27 this.message = message; 28 console.log( this.name + ": " + message ); 29}; 30 31NoModelError.prototype = new TypeNotFoundError(); 32NoModelError.prototype.constructor = NoModelError; 33 34function EntityFactory( models ){ 35 this.models = models || []; 36 this._types = { 37 "string": function(model){ 38 return (model && model["default"]) || ""; 39 }, 40 41 "number": function(model){ 42 return parseInt( (model && model["default"]) || 0, 10 ); 43 }, 44 45 "boolean": function(model){ 46 var result = (model && model["default"]) || false; 47 if ( typeof result == "string" ) { 48 result = ( result.toLowerCase === "true" ) || ( result === "1" ) || ( parseInt( result, 10 ) > 0 ); 49 } else if ( typeof result == "number" ) { 50 result = result > 0; 51 } 52 return result; 53 }, 54 55 "object": function(model){ 56 var result = {}; 57 if ( model && model.properties ){ 58 for( var key in model.properties ){ 59 result[key] = this._get(model.properties[key].type, model.properties[key]); 60 } 61 } 62 return result; 63 }, 64 "array": function(model){ 65 var result; 66 if ( model && model.items ){ 67 result = this._get(model.items.type, model.items); 68 } 69 return (model && model["default"]) || []; 70 } 71 }; 72} 73 74EntityFactory.prototype = { 75 get: function( name, defaults ){ 76 var result = null; 77 if ( typeof defaults == typeof undefined ){ 78 result = this._get( name ); 79 } else { 80 if ( Object.prototype.toString.call(defaults) == "[object Object]" ){ 81 result = this._get( name ); 82 result = $extend( {}, result, defaults ); 83 } else { 84 result = defaults; 85 } 86 } 87 return result; 88 }, 89 90 __get_type: function( name ){ 91 return this._types[name].apply( this, Array.prototype.slice.call(arguments, 1) ); 92 }, 93 94 _get: function( name ){ 95 if ( typeof this._types[name] == "function" ) { 96 return this.__get_type.apply(this, arguments); 97 } else { 98 try { 99 this._add( name ); 100 } catch (e) { 101 if ( e instanceof NoModelError) { 102 throw new TypeNotFoundError( e.message ); 103 } 104 throw e; 105 } 106 return this.__get_type.apply(this, arguments); 107 } 108 }, 109 110 _add: function( name ){ 111 if ( this.models[name] == undefined ){ 112 throw new NoModelError( "Can't found `" + name + "` model" ); 113 } 114 115 this._types[name] = (function(self, key, md){ 116 var result = null, 117 res = null; 118 try { 119 result = (function(v){ 120 return function(){ 121 return v; 122 }; 123 })( self._get(md.type, md) ); 124 } catch (e) { 125 if ( e instanceof NoModelError ) { 126 result = self._add( key ); 127 } else { 128 throw e; 129 } 130 } 131 return result; 132 })(this, name, this.models[name]); 133 } 134} 135 136Apperyio.EntityFactory = EntityFactory; 137 138Apperyio.EntityError = EntityError; 139Apperyio.NoModelError = NoModelError; 140Apperyio.TypeNotFoundError = TypeNotFoundError; 141 142}
142)( jQuery, Apperyio );
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.