1 package CatalystX::Routes;
6 use CatalystX::Routes::Role::Class;
7 use CatalystX::Routes::Role::Controller;
8 use Moose::Util qw( apply_all_roles );
9 use Params::Util qw( _STRING _CODELIKE );
10 use Scalar::Util qw( blessed );
14 Moose::Exporter->setup_import_methods(
15 with_meta => [qw( get get_html post put del )],
16 as_is => [qw( chained args capture_args path_part action )],
18 class => ['CatalystX::Routes::Role::Class'],
23 _add_route( 'GET', @_ );
27 _add_route( 'GET_html', @_ );
31 _add_route( 'POST', @_ );
35 _add_route( 'PUT', @_ );
39 _add_route( 'DELETE', @_ );
45 my ( $name, $attrs, $sub ) = _process_args( $meta, @_ );
47 my $meth_base = '__route__' . $name;
49 _maybe_add_rest_route( $meta, $meth_base, $attrs );
51 my $meth_name = $meth_base . q{_} . $rest;
53 $meta->add_method( $meth_name => sub { goto &$sub } );
59 return ( Chained => [ $_[0] ] );
63 return ( Args => [ $_[0] ] );
66 sub capture_args ($) {
67 return ( CaptureArgs => [ $_[0] ] );
71 return ( PathPart => [ $_[0] ] );
75 return ( ActionClass => [ $_[0] ] );
83 my $caller = ( caller(2) )[3];
86 "The $caller keyword expects a path string or regex as its first argument"
87 unless _STRINGLIKE0($path) || _REGEX($path);
89 die "The $caller keyword expects a sub ref as its final argument"
90 unless _CODELIKE($sub);
94 $p{ActionClass} ||= 'REST::ForBrowsers';
96 unless ( exists $p{Chained} ) {
99 unless ( $p{PathPart} ) {
101 unless ( $part =~ s{^/}{} ) {
103 $meta->name()->action_namespace('FakeConfig'), . $part;
106 $p{PathPart} = [$part];
110 unless ( $p{Args} ) {
114 ( my $name = $path ) =~ s/(\W)/'X' . sprintf( '%x', ord($1) )/eg;
116 return $name, \%p, $sub;
119 sub _maybe_add_rest_route {
124 return if $meta->has_method($name);
126 # This could be done by Moose::Exporter, but that would require that the
127 # module has already inherited from Cat::Controller when it calls "use
128 # CatalystX::Routes".
129 unless ( $meta->does_role('CatalystX::Routes::Role::Controller') ) {
132 'CatalystX::Routes::Role::Controller'
136 $meta->add_method( $name => sub { } );
138 $meta->add_route( $name => [ $attrs, $meta->get_method($name) ] );
143 # XXX - this should be added to Params::Util
144 sub _STRINGLIKE0 ($) {
145 return _STRING( $_[0] )
149 && overload::Method( $_[0], q{""} )
155 # This is a nasty hack around some weird back compat code in
156 # Catalyst::Controller->action_namespace
160 return { case_sensitive => 0 };