-
Notifications
You must be signed in to change notification settings - Fork 2
/
resourcy.min.js
1 lines (1 loc) · 3.29 KB
/
resourcy.min.js
1
(function(){var e,t,n,r,i,s,o,u,a,f=this;a={},o=["get","put","post","delete"],e=["index","create","new","edit","show","update","delete"],r={plural:[["get",""],["post",""],["get","/new"],["get","/:id/edit"],["get","/:id"],["put","/:id"],["delete","/:id"]],singular:[null,["post",""],["get","/new"],["get","/edit"],["get",""],["put",""],["delete",""]]},u=function(e){var t,n;return e=e.match(/^((http[s]?|ftp):\/\/)?(((.+)@)?([^:\/\?#\s]+)(:(\d+))?)?(\/?[^\?#\.]+)?(\.([^\?#]+))?(\?([^#]*))?(#(.*))?$/i)||[],t=((n=e[9])!=null?n.match(/(\/.*)\/+(\w+)$/i):void 0)||[],{scheme:e[2],credentials:e[5],host:e[6],port:e[8],path:e[9],action:t[2]||"",format:e[11],query:e[13],hash:e[15]}},n=function(n,i){return i==null&&(i=!1),a[n]?a[n]:a[n]={path:new RegExp("^"+(n[0]==="/"?n:"/"+n).replace(/:\w+/ig,"(\\w+)")+"/?(\\w+)?/?(\\w+)?/?($|\\?|\\.|#)","i"),pathvars:(n.match(/:(\w+)/ig)||[]).join("|").replace(/:/g,"").split("|"),actions:{},singular:i,name:n.substr(n.lastIndexOf("/")+1),add:function(e,n){var r,i,s;i={},typeof e=="string"?i[e]=n:i=e;for(e in i)n=i[e],s=e.split(":"),r=s[0],e=s[1],t.call(this,r,e,n);return this},remove:function(e){var t,n;return n=e.split(":"),t=n[0],e=n[1],e?delete this.actions[t][e]:delete this.actions[t],this},removeAll:function(){return this.actions={},this},describe:function(){var t,i,s,u,a,f,l,c,h,p,d;a=[];for(l=0,h=o.length;l<h;l++){u=o[l];for(t in this.actions[u])a.push(""+n+"/"+t+" "+u.toUpperCase()+" => "+this.name+"#"+t)}p=this.singular?[1,r.singular]:[0,r.plural],f=p[0],i=p[1];for(s=c=f,d=e.length-1;f<=d?c<=d:c>=d;s=f<=d?++c:--c)this.actions[e[s]]&&a.push(""+n+i[s][1]+" "+i[s][0].toUpperCase()+" => "+this.name+"#"+e[s]);return a}}},t=function(e,t,n){var r,i;r="The "+[e,t].join(":").replace(/:$/,"")+" action already exists on the '"+this.name+"' resource. Try removing it first.";if(t){(i=this.actions)[e]||(i[e]={});if(this.actions[e][t])throw r;return this.actions[e][t]=n}if(this.actions[e])throw r;if(this.singular&&e==="index")throw"Adding index to '"+this.name+"' isn't possible (singular resource).";return this.actions[e]=n},s=function(e,t,n,r,s){var o,f,l,c,h,p,d,v,m,g,y,b,w,E,S,x,T;e=e.toLowerCase(),x=b=u(t),p=x.path,o=x.action,m=!1,v=function(e){return m=!0,r(t,s(n||{},e||{}))};for(c in a){g=a[c];if(!(h=p.match(g.path)))continue;if(f=i(g,o,e,h[h.length-2],h[h.length-3])){w={},T=g.pathvars;for(l=E=0,S=T.length;E<S;l=++E)d=T[l],w[d]=h[l+1];y=f(v,w,b)}if(y!==!1&&!m)return v(y);return}return r(t,n)},i=function(e,t,n,r,i){var s,o,u,a;switch(n){case"get":if((s=e.actions.get)!=null?s[t]:void 0)return e.actions.get[t];switch(i?t:""){case"":return e.singular?e.actions.show:e.actions.index;case"new":return e.actions["new"];case"edit":return e.actions.edit;default:if(!r)return e.actions.show}break;case"put":return((o=e.actions.put)!=null?o[t]:void 0)||(r?void 0:e.actions.update);case"post":return((u=e.actions.post)!=null?u[t]:void 0)||(i?void 0:e.actions.create);case"delete":return((a=e.actions["delete"])!=null?a[t]:void 0)||(r?void 0:e.actions.destroy)}},this.Resourcy={removeAll:function(){return a={}},handleRequest:s,noConflict:Resourcy.noConflict||function(){return delete Resourcy},resources:function(e,t){return t==null&&(t={}),n(e).add(t)},resource:function(e,t){return t==null&&(t={}),n(e,!0).add(t)},routes:function(){var e,t,n;n={};for(e in a)t=a[e],n[t.name]=t.describe();return n}}}).call(this);