/** * A resource factory inspired by $resource from AngularJS * @version v0.2.3 - 2013-09-28 * @link https://github.com/FineLinePrototyping/angularjs-rails-resource.git * @author */ !function(){angular.module("rails",["ng"])}(),function(){angular.module("rails").factory("RailsInflector",function(){function a(a){return angular.isString(a)?a.replace(/_[\w\d]/g,function(a,b,c){return 0===b?a:c.charAt(b+1).toUpperCase()}):a}function b(a){return angular.isString(a)?a.replace(/[A-Z]/g,function(a,b){return 0===b?a:"_"+a.toLowerCase()}):a}function c(a){return a+"s"}return{camelize:a,underscore:b,pluralize:c}})}(),function(a){angular.module("rails").factory("RailsResourceInjector",["$injector",function(b){function c(c){return c?angular.isString(c)?b.get(c):c:a}function d(d){return d?b.instantiate(c(d)):a}return{createService:d,getDependency:c}}])}(),function(){angular.module("rails").factory("railsUrlBuilder",["$interpolate",function(a){return function(b){var c;return angular.isFunction(b)?b:(-1===b.indexOf(a.startSymbol())&&(b=b+"/"+a.startSymbol()+"id"+a.endSymbol()),c=a(b),function(a){return b=c(a),"/"===b.charAt(b.length-1)&&(b=b.substr(0,b.length-1)),b})}}])}(),function(a){angular.module("rails").provider("railsSerializer",function(){var b={underscore:a,camelize:a,pluralize:a,exclusionMatchers:[]};this.underscore=function(a){return b.underscore=a,this},this.camelize=function(a){return b.camelize=a,this},this.pluralize=function(a){return b.pluralize=a,this},this.exclusionMatchers=function(a){return b.exclusionMatchers=a,this},this.$get=["$injector","RailsInflector","RailsResourceInjector",function(c,d,e){function f(c,d){function f(){angular.isFunction(c)&&(d=c,c={}),this.exclusions={},this.inclusions={},this.serializeMappings={},this.deserializeMappings={},this.customSerializedAttributes={},this.preservedAttributes={},this.customSerializers={},this.nestedResources={},this.options=angular.extend({excludeByDefault:!1},b,c||{}),d&&d.call(this,this)}return f.prototype.exclude=function(){var a=this.exclusions;return angular.forEach(arguments,function(b){a[b]=!1}),this},f.prototype.only=function(){var a=this.inclusions;return this.options.excludeByDefault=!0,angular.forEach(arguments,function(b){a[b]=!0}),this},f.prototype.nestedAttribute=function(){var a=this;return angular.forEach(arguments,function(b){a.rename(b,b+"_attributes")}),this},f.prototype.resource=function(a,b,c){return this.nestedResources[a]=b,c&&this.serializeWith(a,c),this},f.prototype.rename=function(b,c,d){return this.serializeMappings[b]=c,(d||d===a)&&(this.deserializeMappings[c]=b),this},f.prototype.add=function(a,b){return this.customSerializedAttributes[a]=b,this},f.prototype.preserve=function(a){return this.preservedAttributes[a]=!0,this},f.prototype.serializeWith=function(a,b){return this.customSerializers[a]=b,this},f.prototype.isExcludedFromSerialization=function(b){if(this.options.excludeByDefault&&!this.inclusions.hasOwnProperty(b)||this.exclusions.hasOwnProperty(b))return!0;if(this.options.exclusionMatchers){var c=!1;return angular.forEach(this.options.exclusionMatchers,function(d){angular.isString(d)?c=c||0===b.indexOf(d):angular.isFunction(d)?c=c||d.call(a,b):d instanceof RegExp&&(c=c||d.test(b))}),c}return!1},f.prototype.getSerializedAttributeName=function(b){var c=this.serializeMappings[b]||b;return this.isExcludedFromSerialization(b)||this.isExcludedFromSerialization(c)?a:this.underscore(c)},f.prototype.isExcludedFromDeserialization=function(){return!1},f.prototype.getDeserializedAttributeName=function(b){var c=this.camelize(b);return c=this.deserializeMappings[b]||this.deserializeMappings[c]||c,this.isExcludedFromDeserialization(b)||this.isExcludedFromDeserialization(c)?a:c},f.prototype.getNestedResource=function(a){return e.getDependency(this.nestedResources[a])},f.prototype.getAttributeSerializer=function(b){var c=this.getNestedResource(b),d=this.customSerializers[b];return d?e.createService(d):c?c.serializer:a},f.prototype.serializeValue=function(a){var b=a,c=this;if(angular.isArray(a))b=[],angular.forEach(a,function(a){b.push(c.serializeValue(a))});else if(angular.isObject(a)){if(angular.isDate(a))return a;b={},angular.forEach(a,function(a,d){angular.isFunction(a)||c.serializeAttribute(b,d,a)})}return b},f.prototype.serializeAttribute=function(b,c,d){var e=this.getAttributeSerializer(c),f=this.getSerializedAttributeName(c);f!==a&&(b[f]=e?e.serialize(d):this.serializeValue(d))},f.prototype.serialize=function(a){var b=this.serializeValue(a),c=this;return angular.isObject(b)&&angular.forEach(this.customSerializedAttributes,function(d,e){angular.isFunction(d)&&(d=d.call(a,a)),c.serializeAttribute(b,e,d)}),b},f.prototype.deserializeValue=function(a,b){var c=a,d=this;if(angular.isArray(a))c=[],angular.forEach(a,function(a){c.push(d.deserializeValue(a,b))});else if(angular.isObject(a)){if(angular.isDate(a))return a;c={},b&&(c=new b),angular.forEach(a,function(a,b){d.deserializeAttribute(c,b,a)})}return c},f.prototype.deserializeAttribute=function(b,c,d){var e,f,g=this.getDeserializedAttributeName(c);g!==a&&(e=this.getAttributeSerializer(g),f=this.getNestedResource(g),b[g]=this.preservedAttributes[g]?d:e?e.deserialize(d,f):this.deserializeValue(d,f))},f.prototype.deserialize=function(a,b){return this.deserializeValue(a,b)},f.prototype.pluralize=function(a){return this.options.pluralize?this.options.pluralize(a):a},f.prototype.underscore=function(a){return this.options.underscore?this.options.underscore(a):a},f.prototype.camelize=function(a){return this.options.camelize?this.options.camelize(a):a},f}return b.underscore=b.underscore||d.underscore,b.camelize=b.camelize||d.camelize,b.pluralize=b.pluralize||d.pluralize,f.defaultOptions=b,f}]})}(),function(a){angular.module("rails").factory("railsRootWrappingTransformer",function(){return function(a,b){var c={};return c[angular.isArray(a)?b.rootPluralName:b.rootName]=a,c}}),angular.module("rails").factory("railsRootWrappingInterceptor",function(){return function(a){var b=a.resource;return b?a.then(function(a){return a.data&&a.data.hasOwnProperty(b.rootName)?a.data=a.data[b.rootName]:a.data&&a.data.hasOwnProperty(b.rootPluralName)&&(a.data=a.data[b.rootPluralName]),a}):a}}),angular.module("rails").provider("railsResourceFactory",function(){var b={enableRootWrapping:!0,updateMethod:"put",httpConfig:{},defaultParams:a};this.enableRootWrapping=function(a){return b.enableRootWrapping=a,this},this.updateMethod=function(a){return b.updateMethod=a,this},this.httpConfig=function(a){return b.httpConfig=a,this},this.defaultParams=function(a){return b.defaultParams=a,this},this.$get=["$http","$q","railsUrlBuilder","railsSerializer","railsRootWrappingTransformer","railsRootWrappingInterceptor","RailsResourceInjector",function(c,d,e,f,g,h,i){function j(d){function j(a,b){return b&&("/"!==b[0]&&(a+="/"),a+=b),a}function k(a){var b=this;if(a){var c=function(a){return{resource:k,context:b,response:a,then:function(a){return this.response=a(this.response,this.resource,this.context),c(this.response)}}},d=k.callInterceptors(c({data:a}),this).response.data;angular.extend(this,d)}}var l=d.requestTransformers,m=d.responseInterceptors,n=d.afterResponseInterceptors;return k.setUrl=function(a){k.url=e(a)},k.setUrl(d.url),k.enableRootWrapping=d.wrapData===a?b.enableRootWrapping:d.wrapData,k.httpConfig=d.httpConfig||b.httpConfig,k.httpConfig.headers=angular.extend({Accept:"application/json","Content-Type":"application/json"},k.httpConfig.headers||{}),k.defaultParams=d.defaultParams||b.defaultParams,k.updateMethod=(d.updateMethod||b.updateMethod).toLowerCase(),k.requestTransformers=[],k.responseInterceptors=[],k.afterResponseInterceptors=[],k.serializer=i.createService(d.serializer||f()),k.rootName=k.serializer.underscore(d.name),k.rootPluralName=k.serializer.underscore(d.pluralName||k.serializer.pluralize(d.name)),k.beforeResponse=function(a){a=i.getDependency(a),k.responseInterceptors.push(function(b){return b.then(function(c){return a(c.data,b.resource,b.context),c})})},k.afterResponse=function(a){a=i.getDependency(a),k.afterResponseInterceptors.push(function(b){return b.then(function(c){return a(c,b.resource),c})})},k.beforeRequest=function(a){a=i.getDependency(a),k.requestTransformers.push(function(b,c){return a(b,c)||b})},angular.forEach(m,function(a){k.responseInterceptors.push(i.getDependency(a))}),angular.forEach(n,function(a){k.afterResponseInterceptors.push(i.getDependency(a))}),angular.forEach(l,function(a){k.requestTransformers.push(i.getDependency(a))}),k.transformData=function(a){return a=k.serializer.serialize(a),angular.forEach(k.requestTransformers,function(b){a=b(a,k)}),k.enableRootWrapping&&(a=g(a,k)),a},k.callInterceptors=function(a,b){return a=a.then(function(a){return a.originalData=a.data,a}),k.enableRootWrapping&&(a.resource=k,a=h(a)),a.then(function(a){return a.data=k.serializer.deserialize(a.data,k),a}),angular.forEach(k.responseInterceptors,function(c){a.resource=k,a.context=b,a=c(a)}),a},k.callAfterInterceptors=function(a){return angular.forEach(k.afterResponseInterceptors,function(b){a.resource=k,a=b(a)}),a},k.processResponse=function(a){return a=k.callInterceptors(a).then(function(a){return a.data}),k.callAfterInterceptors(a)},k.getParameters=function(a){var b;return k.defaultParams&&(b=k.defaultParams),angular.isObject(a)&&(b=angular.extend(b||{},a)),b},k.getHttpConfig=function(a){var b=k.getParameters(a);return b?angular.extend({params:b},k.httpConfig):angular.copy(k.httpConfig)},k.$url=k.resourceUrl=function(a,b){return angular.isObject(a)||(a={id:a}),j(k.url(a||{}),b)},k.$get=function(a,b){return k.processResponse(c.get(a,k.getHttpConfig(b)))},k.query=function(a,b){return k.$get(k.resourceUrl(b),a)},k.get=function(a,b){return k.$get(k.resourceUrl(a),b)},k.prototype.$url=function(a){return j(k.resourceUrl(this),a)},k.prototype.processResponse=function(a){return a=k.callInterceptors(a,this),a=a.then(angular.bind(this,function(a){return a.hasOwnProperty("data")&&angular.isObject(a.data)&&angular.extend(this,a.data),this})),k.callAfterInterceptors(a)},angular.forEach(["post","put","patch"],function(a){k["$"+a]=function(b,d){var e;return d=k.transformData(angular.copy(d,{})),e=angular.extend({method:a,url:b,data:d},k.getHttpConfig()),k.processResponse(c(e))},k.prototype["$"+a]=function(b){var d,e;return d=k.transformData(angular.copy(this,{})),e=angular.extend({method:a,url:b,data:d},k.getHttpConfig()),this.processResponse(c(e))}}),k.prototype.create=function(){return this.$post(this.$url(),this)},k.prototype.update=function(){return this["$"+k.updateMethod](this.$url(),this)},k.prototype.isNew=function(){return null==this.id},k.prototype.save=function(){return this.isNew()?this.create():this.update()},k.$delete=function(a){return k.processResponse(c["delete"](a,k.getHttpConfig()))},k.prototype.$delete=function(a){return this.processResponse(c["delete"](a,k.getHttpConfig()))},k.prototype.remove=k.prototype["delete"]=function(){return this.$delete(this.$url())},k}return j}]})}();