| 1 | package Dancer2::Controllers; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use MooseX::MethodAttributes; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 4 | our $VERSION = '1.1'; | |||||||||||||||||||||||||||
| 12 | 4 | our @EXPORT = qw(controllers); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 4 | my %dsl; | |||||||||||||||||||||||||||
| 15 | 4 | @dsl{qw(get post put patch del any)} = qw(1 1 1 1 1 1); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 8 | ▶ | sub controllers { | ||||||||||||||||||||||||||
|
Subroutine
controllers: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 8 | my $classes = shift; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 8 | ▶ | croak | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $classes and ref $classes eq "ARRAY"
MC/DC50%1/2
ref $classes and ref $classes eq "ARRAY"
ref $classes
ref $classes eq "ARRAY"
Truth table66%2/3
A and B
A= ref $classesB= ref $classes eq "ARRAY"
| |||||||||||||||||||||||||||||
| 21 | qq{Dancer2::Controllers::controllers expects a single, array-ref as it's argument, instead got $classes} | ||||||||||||||||||||||||||||
| 22 | unless ref($classes) && ref($classes) eq 'ARRAY'; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 7 | my ($pkg) = caller; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 7 | for (@$classes) { | |||||||||||||||||||||||||||
| 27 | 7 | my $class = $_; | |||||||||||||||||||||||||||
| 28 | 7 | my $meta = $class->meta; | |||||||||||||||||||||||||||
| 29 | 5 | my @methods = $meta->get_method_list; | |||||||||||||||||||||||||||
| 30 | 5 | for (@methods) { | |||||||||||||||||||||||||||
| 31 | 10 | my $method = $meta->get_method($_); | |||||||||||||||||||||||||||
| 32 | 10 | ▶ | next unless $method->can('attributes'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 6 | for ( @{ $method->attributes } ) { | |||||||||||||||||||||||||||
| 34 | 6 | my $method_name = $method->fully_qualified_name(); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 6 | ▶ | if (/^Route$/x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 1 | croak | |||||||||||||||||||||||||||
| 38 | qq{Empty route declared, $method_name, did you mean to provide arguments?}; | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 5 | ▶ | next unless /^Route\(.*/x; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 5 | my ( $action, $location ) = | |||||||||||||||||||||||||||
| 43 | /^Route\((get|put|post|patch|del|any)\s=>\s'?"?([\w\/:_\-\[\]{}?]+)'?"?\)/x; | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 5 | ▶ | croak | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | qq{Location is undefined for route attribute on method '$method_name'} | ||||||||||||||||||||||||||||
| 47 | unless $location; | ||||||||||||||||||||||||||||
| 48 | croak | ||||||||||||||||||||||||||||
| 49 | qq{Invalid action $action for route attribute on method '$method_name' should be get, put, post, patch, del, any} | ||||||||||||||||||||||||||||
| 50 | 3 | ▶ | unless exists $dsl{$action}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | $pkg->can($action) | ||||||||||||||||||||||||||||
| 53 | 3 | ▶ | ->( $location, sub { $class->can( $method->name() )->(@_) } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 4 | 1; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =encoding utf8 | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =head1 NAME | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | Dancer2::Controllers | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | Dancer2::Controllers is a Spring-Boot esq wrapper for defining Dancer2 routes, it allows you to define | ||||||||||||||||||||||||||||
| 70 | routes inside of modules using method attributes. | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =head1 EXAMPLE | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | package MyApp::Controller; | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | use Moose; | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | BEGIN { extends 'Dancer2::Controllers::Controller' } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | sub hello_world : Route(get => /) { | ||||||||||||||||||||||||||||
| 81 | "Hello World!"; | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 1; | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | package main; | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | use Dancer2; | ||||||||||||||||||||||||||||
| 89 | use Dancer2::Controllers; | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | controllers( ['MyApp::Controller'] ); | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | dance; | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =head1 API | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =head2 controllers | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | controllers( [ | ||||||||||||||||||||||||||||
| 100 | 'MyApp::Controller::Foo', | ||||||||||||||||||||||||||||
| 101 | 'MyApp::Controller::Bar' | ||||||||||||||||||||||||||||
| 102 | ] ); | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | A subroutine that takes a list of controller module names, and registers their routes methods, annotated by the C<Route> attribute. | ||||||||||||||||||||||||||||