const _ = require('lodash');

const configureController = require('./lib/configure_controller');
const validateController = require('./lib/validate_controller');

const METHODS = ['create', 'read', 'readRelation', 'update', 'destroy'];

function Controller(opts) {
  if (!opts) {
    opts = {};
  }
  if (!opts.source) {
    throw new Error('No source specified.');
  }
  _.extend(this, opts);
}

Controller.prototype._requestHandler = require('./lib/request_handler');

METHODS.forEach(function (method) {
  Controller.prototype[method] = function (config) {
    var controller = configureController(method, this.source, config);
    var validationFailures = validateController(method, this.source, controller);
    if (validationFailures.length) {
      throw new Error(validationFailures.join('\n'));
    }
    return this._requestHandler(controller);
  };
}, this);

module.exports = Controller;

@@ -159,80 +159,63 @@
 ');%0A
-%0A
 const 
-METHODS = %5B'create', 'read', 'readRelation', 'update', 'destroy'%5D
+requestHandler = require('./lib/request_handler')
 ;%0A%0Af
@@ -388,159 +388,95 @@
 ler.
-prototype._requestHandler = require('./lib/request_handler');%0A%0AMETHODS.forEach(function (method) %7B%0A  Controller.prototype%5Bmethod%5D = function (config) %7B
+method = function (method) %7B%0A  return function (config) %7B%0A    var source = this.source;
 %0A   
@@ -513,37 +513,32 @@
 troller(method, 
-this.
 source, config);
@@ -594,21 +594,16 @@
 method, 
-this.
 source, 
@@ -727,14 +727,8 @@
 urn 
-this._
 requ
@@ -761,14 +761,313 @@
 %7D;%0A%7D
-, this
+;%0A%0AController.prototype.create = Controller.method('create');%0AController.prototype.read = Controller.method('read');%0AController.prototype.readRelation = Controller.method('readRelation');%0AController.prototype.update = Controller.method('update');%0AController.prototype.destroy = Controller.method('destroy'
 );%0A%0A

change method creation abstraction