Route constructor

Route(
  1. String method,
  2. String pattern,
  3. Handler handler
)

Implementation

Route(String method, String pattern, Handler handler) {
  var regexp = new RegExp(r'^[0-9a-zA-Z()?:_/-]*$');
  if (!regexp.hasMatch(pattern)) {
    throw FormatException('Pattern contains illegal symbols. Pattern: ${pattern}.');
  }

  var chunksIn = pattern.split('/');
  var chunksOut = [];
  for (var chunkIn in chunksIn) {
    if (chunkIn.indexOf(':') == -1) {
      chunksOut.add(chunkIn);
      continue;
    }

    var parts = chunkIn.split(':');
    if (parts.length != 2) {
      throw FormatException('Invalid converter usage. Pattern: ${pattern}.');
    }

    var type = parts[0];
    var name = parts[1];

    if (type.length == 0) {
      type = 'str';
    }

    var converter = Route.converters[type];
    if (converter == null) {
      throw FormatException('Invalid converter id. Pattern: ${pattern}.');
    }

    chunksOut.add('(?<' + name + '>' + converter.regexp + ')');
    this.casts[name] = converter.cast;
  }

  this.method = method;
  this.regexp = new RegExp(r'^/' + chunksOut.join('/') + r'$');
  this.handler = handler;
}