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{^/}{} ) {
102 $part = $meta->name()->action_namespace('FakeConfig') . q{/} . $part;
105 $p{PathPart} = [$part];
109 unless ( $p{Args} || $p{Local} ) {
113 ( my $name = $path ) =~ s/(\W)/'X' . sprintf( '%x', ord($1) )/eg;
115 return $name, \%p, $sub;
118 sub _maybe_add_rest_route {
123 return if $meta->has_method($name);
125 # This could be done by Moose::Exporter, but that would require that the
126 # module has already inherited from Cat::Controller when it calls "use
127 # CatalystX::Routes".
128 unless ( $meta->does_role('CatalystX::Routes::Role::Controller') ) {
131 'CatalystX::Routes::Role::Controller'
135 $meta->add_method( $name => sub { } );
137 $meta->add_route( $name => [ $attrs, $meta->get_method($name) ] );
142 # XXX - this should be added to Params::Util
143 sub _STRINGLIKE0 ($) {
144 return _STRING( $_[0] )
148 && overload::Method( $_[0], q{""} )
153 # This is a nasty hack around some weird back compat code in
154 # Catalyst::Controller->action_namespace
158 return { case_sensitive => 0 };