<commit_before>/**
 * Imports
 */
var _     = require('lodash');
var utils = require('./utils');

/**
 * Controller constructor
 * @param  {Object} options
 */
var Controller = function(options) {
    options || (options = {});
    if (options.routes) this.routes = options.routes;
    if (options.app) this.app = options.app;
    this._parseFilters();
    this._parseRoutes();
    this.initialize.apply(this, arguments);
}

/**
 * Override Controller prototype
 */
_.extend(Controller.prototype, {

    initialize: function() {},

    acceptVerb: ['get', 'post', 'put', 'delete', 'patch'],

    _parseRoutes: function() {
        if (!this.routes) return;
        if (!this.app) return;

        this.routes = _.result(this, 'routes');
        this.basePath = _.result(this, 'basePath');

        for (var i = 0; i < this.routes.length; i++) {
            this._bindRoute(this.routes[i]);
        };
    },

    _bindRoute: function(route) {
        if (!route.path && !route.action) return;
        var verb = route.verb || 'get';
        if (_.contains(this.acceptVerb, verb)) {
            if (_.has(this.app, verb) && _.isFunction(this[route.action])) {

                var ignoreBasePath = route.ignoreBasePath || false,
                    routePath = route.path,
                    filters = _.result(route, 'filters') || [];

                if (this.basePath && !ignoreBasePath) {
                    routePath = this.basePath === '/' ? route.path : this.basePath + (route.path === '/' ? '' : route.path);
                }

                // if path === '/' map route with basePath too
                if (this.basePath && route.path === '/') {
                    this.app[verb](this.basePath, filters, this[route.action].bind(this));
                }

                this.app[verb](routePath, filters, this[route.action].bind(this));
            } else {
                console.log(route.action + " is not a controller action (function)");
            }
        };
    },

    /**
     * parse controller filter and add to global controller route.
     */<commit_msg>Support direct function for route action in controller<commit_after>/**
 * Imports
 */
var _     = require('lodash');
var utils = require('./utils');

/**
 * Controller constructor
 * @param  {Object} options
 */
var Controller = function(options) {
    options || (options = {});
    if (options.routes) this.routes = options.routes;
    if (options.app) this.app = options.app;
    this._parseFilters();
    this._parseRoutes();
    this.initialize.apply(this, arguments);
}

/**
 * Override Controller prototype
 */
_.extend(Controller.prototype, {

    initialize: function() {},

    acceptVerb: ['get', 'post', 'put', 'delete', 'patch'],

    _parseRoutes: function() {
        if (!this.routes) return;
        if (!this.app) return;

        this.routes = _.result(this, 'routes');
        this.basePath = _.result(this, 'basePath');

        for (var i = 0; i < this.routes.length; i++) {
            this._bindRoute(this.routes[i]);
        };
    },

    _bindRoute: function(route) {
        if (!route.path && !route.action) return;
        var verb = route.verb || 'get';
        if (_.contains(this.acceptVerb, verb)) {
            if (_.has(this.app, verb) && (_.isFunction(this[route.action]) || _.isFunction(route.action))) {

                var ignoreBasePath = route.ignoreBasePath || false,
                    routePath = route.path,
                    filters = _.result(route, 'filters') || []
                    action = this[route.action] || route.action;

                if (this.basePath && !ignoreBasePath) {
                    routePath = this.basePath === '/' ? route.path : this.basePath + (route.path === '/' ? '' : route.path);
                }

                // if path === '/' map route with basePath too
                if (this.basePath && route.path === '/') {
                    this.app[verb](this.basePath, filters, action.bind(this));
                }

                this.app[verb](routePath, filters, action.bind(this));
            } else {
                console.log(route.action + " is not a controller action (function)");
            }
        };
    },

    /**
     * parse controller filter and add to global controller route.
     */
{'fraction_non_alphanumeric': 0.15985997666277713, 'fraction_numerical': 0.0004667444574095683, 'mean_word_length': 1.999300209937019, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2596417', 'n_tokens_mistral': 1238, 'n_tokens_neox': 1208, 'n_words': 273}