|
| 1 | +'use strict' |
| 2 | + |
| 3 | +/** |
| 4 | + * adonis-framework |
| 5 | + * Copyright(c) 2015-2015 Harminder Virk |
| 6 | + * MIT Licensed |
| 7 | +*/ |
| 8 | + |
| 9 | +const _ = require('lodash') |
| 10 | + |
| 11 | +class Resource { |
| 12 | + |
| 13 | + constructor (RouteHelper, pattern, handler) { |
| 14 | + this.RouteHelper = RouteHelper |
| 15 | + this.routes = [] |
| 16 | + this.basename = pattern.replace(/\\/g, '') |
| 17 | + this._buildRoutes(pattern, handler) |
| 18 | + return this |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * @description register a route to the routes store |
| 23 | + * and pushes it to local array to reference it |
| 24 | + * later |
| 25 | + * @method _registerRoute |
| 26 | + * @param {String} verb |
| 27 | + * @param {String} route |
| 28 | + * @param {String} handler |
| 29 | + * @param {String} name |
| 30 | + * @return {void} |
| 31 | + * @public |
| 32 | + */ |
| 33 | + _registerRoute (verb, route, handler, name) { |
| 34 | + this.RouteHelper[verb](route, `${handler}.${name}`).as(`${this.basename}.${name}`) |
| 35 | + this.routes.push(this.RouteHelper.lastRoute()) |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @description builds all routes for a given pattern |
| 40 | + * @method _buildRoutes |
| 41 | + * @param {String} pattern |
| 42 | + * @param {String} handler |
| 43 | + * @return {void} |
| 44 | + * @public |
| 45 | + */ |
| 46 | + _buildRoutes (pattern, handler) { |
| 47 | + pattern = pattern.replace(/(\w+)\./g, function (index, group) { |
| 48 | + return `${group}/:${group}_id/` |
| 49 | + }) |
| 50 | + const seperator = pattern.endsWith('/') ? '' : '/' |
| 51 | + |
| 52 | + this._registerRoute('get', pattern, handler, 'index') |
| 53 | + this._registerRoute('get', `${pattern}${seperator}create`, handler, 'create') |
| 54 | + this._registerRoute('post', `${pattern}`, handler, 'store') |
| 55 | + this._registerRoute('get', `${pattern}${seperator}:id`, handler, 'show') |
| 56 | + this._registerRoute('get', `${pattern}${seperator}:id/edit`, handler, 'edit') |
| 57 | + this._registerRoute('put', `${pattern}${seperator}:id`, handler, 'update') |
| 58 | + this._registerRoute('patch', `${pattern}${seperator}:id`, handler, 'update') |
| 59 | + this._registerRoute('delete', `${pattern}${seperator}:id`, handler, 'destroy') |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @description transform methods keys to resource route names |
| 64 | + * @method _transformKeys |
| 65 | + * @param {Array} pairKeys |
| 66 | + * @return {Array} |
| 67 | + */ |
| 68 | + _transformKeys (pairKeys) { |
| 69 | + return pairKeys.map((item) => { |
| 70 | + return `${this.basename}.${item}` |
| 71 | + }) |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @description change names for defined routes mapped |
| 76 | + * next to actions |
| 77 | + * @method as |
| 78 | + * @param {Object} pairs |
| 79 | + * @return {Object} |
| 80 | + */ |
| 81 | + as (pairs) { |
| 82 | + const pairKeys = _.keys(pairs) |
| 83 | + const pairTransformedKeys = this._transformKeys(pairKeys) |
| 84 | + _.each(this.routes, function (route) { |
| 85 | + const pairIndex = pairTransformedKeys.indexOf(route.name) |
| 86 | + if (pairIndex > -1) { |
| 87 | + route.name = pairs[pairKeys[pairIndex]] |
| 88 | + } |
| 89 | + }) |
| 90 | + return this |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @description removes all other actions from routes |
| 95 | + * resources except the given array |
| 96 | + * @method only |
| 97 | + * @param {Array} methods |
| 98 | + * @return {Object} |
| 99 | + * @public |
| 100 | + */ |
| 101 | + only (methods) { |
| 102 | + const transformedMethods = this._transformKeys(methods) |
| 103 | + this.routes = _.filter(this.routes, (route) => { |
| 104 | + if (transformedMethods.indexOf(route.name) <= -1) { |
| 105 | + this.RouteHelper.remove(route.name) |
| 106 | + } else { |
| 107 | + return true |
| 108 | + } |
| 109 | + }) |
| 110 | + return this |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @description removes actions defined inside given array |
| 115 | + * from all resources routes |
| 116 | + * @method except |
| 117 | + * @param {Array} methods |
| 118 | + * @return {Object} |
| 119 | + * @public |
| 120 | + */ |
| 121 | + except (methods) { |
| 122 | + const transformedMethods = this._transformKeys(methods) |
| 123 | + this.routes = _.filter(this.routes, (route) => { |
| 124 | + if (transformedMethods.indexOf(route.name) > -1) { |
| 125 | + this.RouteHelper.remove(route.name) |
| 126 | + } else { |
| 127 | + return true |
| 128 | + } |
| 129 | + }) |
| 130 | + return this |
| 131 | + } |
| 132 | + |
| 133 | +} |
| 134 | + |
| 135 | +module.exports = Resource |
0 commit comments