<commit_before>var Url = Iron.Url;

/**
 * XXX handle case where name is the path and the path is in the options
 * XXX what if second parameter is an object like what we have for current
 * routes?
 * TODO clone
 *
 */
Handler = function (path, fn, options) {
  if (_.isFunction(path)) {
    options = fn || {};
    fn = path;
    path = '/';
    options.mount = true;
  }

  options = options || {};
  this.options = options;

  this.mount = options.mount;

  // if we're mounting at path '/foo' then this handler should also handle
  // '/foo/bar' and '/foo/bar/baz'
  if (this.mount)
    options.end = false;

  this.path = Url.normalize(path);
  this.compiledUrl = new Url(this.path, options);
  this.method = (options.method && options.method.toLowerCase()) || false;

  // should the handler be on the 'client', 'server' or 'both'?
  this.where = options.where || 'client';

  // name provided explicitly as an option
  if (options.name)
    this.name = options.name;

  // or the function is actually a string
  else if (_.isString(fn))
    this.name = fn;

  // or maybe you named the function myFuncName () {}
  else if (fn.name)
    this.name = fn.name;

  // or maybe you'll be nameless :(
  else
    this.name = null;


  if (_.isString(fn)) {
    this.handle = function () {
      // try to find a method on the current thisArg which might be a Controller
      // for example.
      var func = this[fn];
      return func.apply(this, arguments);
    };
  } else if (_.isFunction(fn)) {
    // or just a regular old function
    this.handle = fn;
  }
};

Handler.prototype.test = function (path, options) {
  return this.compiledUrl.test(path);
};

Handler.prototype.params = function (path) {
  return this.compiledUrl.params(path);
};

/**
 * Returns a new cloned Handler.
 */<commit_msg>throw error if fn is a string and the method isn't found.<commit_after>var Url = Iron.Url;

/**
 * XXX handle case where name is the path and the path is in the options
 * XXX what if second parameter is an object like what we have for current
 * routes?
 * TODO clone
 *
 */
Handler = function (path, fn, options) {
  if (_.isFunction(path)) {
    options = fn || {};
    fn = path;
    path = '/';
    options.mount = true;
  }

  options = options || {};
  this.options = options;

  this.mount = options.mount;

  // if we're mounting at path '/foo' then this handler should also handle
  // '/foo/bar' and '/foo/bar/baz'
  if (this.mount)
    options.end = false;

  this.path = Url.normalize(path);
  this.compiledUrl = new Url(this.path, options);
  this.method = (options.method && options.method.toLowerCase()) || false;

  // should the handler be on the 'client', 'server' or 'both'?
  this.where = options.where || 'client';

  // name provided explicitly as an option
  if (options.name)
    this.name = options.name;

  // or the function is actually a string
  else if (_.isString(fn))
    this.name = fn;

  // or maybe you named the function myFuncName () {}
  else if (fn.name)
    this.name = fn.name;

  // or maybe you'll be nameless :(
  else
    this.name = null;


  if (_.isString(fn)) {
    this.handle = function () {
      // try to find a method on the current thisArg which might be a Controller
      // for example.
      var func = this[fn];

      if (typeof func !== 'function')
        throw new Error("No method named '" + fn + "' found on handler.");

      return func.apply(this, arguments);
    };
  } else if (_.isFunction(fn)) {
    // or just a regular old function
    this.handle = fn;
  }
};

Handler.prototype.test = function (path, options) {
  return this.compiledUrl.test(path);
};

Handler.prototype.params = function (path) {
  return this.compiledUrl.params(path);
};

/**
 * Returns a new cloned Handler.
 */
{'fraction_non_alphanumeric': 0.13458985930448633, 'fraction_numerical': 0.0, 'mean_word_length': 2.7086614173228347, 'pattern_counts': {'":': 0, '<': 3, '<?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': '1594494', 'n_tokens_mistral': 1202, 'n_tokens_neox': 1158, 'n_words': 408}