diff --git a/lib/src/route.dart b/lib/src/route.dart index 38301c8d..3b91587c 100644 --- a/lib/src/route.dart +++ b/lib/src/route.dart @@ -48,6 +48,7 @@ class Route { final List _children = []; final List _handlers = []; RegExp _matcher; + String _method; String _name; Route _parent; String _path; @@ -56,7 +57,7 @@ class Route { List get children => new List.unmodifiable(_children); List get handlers => new List.unmodifiable(_handlers); RegExp get matcher => _matcher; - final String method; + String get method => _method; String get name => _name; Route get parent => _parent; String get path => _path; @@ -94,10 +95,11 @@ class Route { Route(Pattern path, {Iterable children: const [], Iterable handlers: const [], - this.method: "GET", + method: "GET", String name: null}) { if (children != null) _children.addAll(children); if (handlers != null) _handlers.addAll(handlers); + _method = method; _name = name; if (path is RegExp) { @@ -113,6 +115,43 @@ class Route { } } + /// Splits a route path into a list of segments, and then + /// builds a hierarchy of off that. + /// + /// This should generally be used instead of the original + /// Route constructor. + /// + /// All children and handlers, as well as the method, will be + /// assigned to the last child route created. + /// + /// The final child route is returned. + factory Route.build(Pattern path, + {Iterable children: const [], + Iterable handlers: const [], + method: "GET", + String name: null}) { + final segments = path.toString().split('/').where((str) => str.isNotEmpty); + print('Seg: $segments'); + Route result; + + for (String segment in segments) { + print('SEGGG: $segment'); + if (result == null) + result = new Route(segment); + else + result = result.child(segment); + } + + print('result: ${result.path}'); + + result._children.addAll(children); + result._handlers.addAll(handlers); + result._method = method; + result._name = name; + + return result; + } + factory Route.join(Route parent, Route child) { final String path1 = parent.path .replaceAll(_rgxStart, '') @@ -137,9 +176,11 @@ class Route { String separator = (pattern1.isEmpty || pattern1 == '^') ? '' : '\\/'; - return route + parent._children.add(route .._matcher = new RegExp('$pattern1$separator$pattern2') - .._parent = parent; + .._parent = parent); + + return route; } List addAll(Iterable routes, {bool join: true}) { @@ -160,7 +201,7 @@ class Route { Iterable handlers: const [], String method: "GET", String name: null}) { - final route = new Route(path, + final route = new Route.build(path, children: children, handlers: handlers, method: method, name: name); return addChild(route); } diff --git a/test/route/no_params.dart b/test/route/no_params.dart index f14b8cd7..cef4fe87 100644 --- a/test/route/no_params.dart +++ b/test/route/no_params.dart @@ -2,7 +2,7 @@ import 'package:angel_route/angel_route.dart'; import 'package:test/test.dart'; main() { - final foo = new Route('/foo', handlers: ['bar']); + final foo = new Route.build('/foo', handlers: ['bar']); final bar = foo.child('/bar'); final baz = bar.child('//////baz//////', handlers: ['hello', 'world']); diff --git a/test/route/with_params.dart b/test/route/with_params.dart index 1a8d0609..e0de1de8 100644 --- a/test/route/with_params.dart +++ b/test/route/with_params.dart @@ -2,10 +2,10 @@ import 'package:angel_route/angel_route.dart'; import 'package:test/test.dart'; main() { - final base = new Route('foo'); - final foo = base.child(':id([0-9]+)', handlers: ['bar']); + final foo = new Route.build('/foo/:id([0-9]+)', handlers: ['bar']); final bar = foo.child('/bar'); final baz = bar.child('//////baz//////', handlers: ['hello', 'world']); + new Router(foo).dumpTree(); test('matching', () { expect(foo.children.length, equals(1));