| 1 | package WebAPI::DBIC::Route; | ||||||||||||||||||||
| 2 | 25 | $WebAPI::DBIC::Route::VERSION = '0.004002'; | |||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 25 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 25 | ▶ | use MooX::StrictConstructor; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 25 | ▶ | use Module::Runtime qw(use_module); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 25 | has path => ( | |||||||||||||||||||
| 11 | is => 'ro', | ||||||||||||||||||||
| 12 | required => 1, | ||||||||||||||||||||
| 13 | ); | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 25 | has resource_class => ( | |||||||||||||||||||
| 16 | is => 'ro', | ||||||||||||||||||||
| 17 | required => 1, | ||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 25 | has resource_args => ( | |||||||||||||||||||
| 21 | is => 'ro', | ||||||||||||||||||||
| 22 | required => 1, | ||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | has route_defaults => ( | ||||||||||||||||||||
| 26 | is => 'ro', | ||||||||||||||||||||
| 27 | 223 | ▶ | default => sub { {} }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 28 | 25 | ); | |||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | has validations => ( | ||||||||||||||||||||
| 31 | is => 'ro', | ||||||||||||||||||||
| 32 | 185 | ▶ | default => sub { {} }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 33 | 25 | ); | |||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 223 | ▶ | sub BUILD { | ||||||||||||||||||
|
Subroutine
BUILD: called CC=5 SCAR=17.8
Pod
undocumented
| |||||||||||||||||||||
| 37 | 223 | my $self = shift; | |||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 223 | my $resource_class = $self->resource_class; | |||||||||||||||||||
| 40 | 223 | my $route_defaults = $self->route_defaults; | |||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 223 | ▶ | if ($ENV{WEBAPI_DBIC_DEBUG}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | 0 | (my $class = $resource_class) =~ s/^WebAPI::DBIC::Resource//; | |||||||||||||||||||
| 44 | warn sprintf "/%s => %s (%s)\n", | ||||||||||||||||||||
| 45 | $self->path, $class, | ||||||||||||||||||||
| 46 | 0 | join(' ', map { "$_=$route_defaults->{$_}" } keys %$route_defaults); | |||||||||||||||||||
| 47 | } | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 223 | use_module $resource_class; | |||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 223 | ▶ | if (my $set = $self->resource_args->{set}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | # we use the 'result_class' key in the route_defaults to lookup the route | ||||||||||||||||||||
| 54 | # for a given result_class | ||||||||||||||||||||
| 55 | 198 | ▶ | $route_defaults->{result_class} = $set && $set->result_class; | ||||||||||||||||||
|
Condition33%1/3
$set && $set->result_class
MC/DC0%0/2
$set && $set->result_class
$set
$set->result_class
Truth table33%1/3
A && B
A= $setB= $set->result_class
| |||||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | else { | ||||||||||||||||||||
| 58 | 25 | ▶ | warn sprintf "/%s resource_class %s has 'set' method but resource_args does not include 'set'", | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | $self->path, $resource_class | ||||||||||||||||||||
| 60 | if $resource_class->can('set'); | ||||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 223 | return; | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 223 | ▶ | sub as_add_route_args { | ||||||||||||||||||
|
Subroutine
as_add_route_args: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 68 | 223 | my $self = shift; | |||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 223 | my $resource_class = $self->resource_class; | |||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | # introspect path to extract path param :names | ||||||||||||||||||||
| 73 | 223 | my $prr = Path::Router::Route->new(path => $self->path); | |||||||||||||||||||
| 74 | my $path_var_names = [ | ||||||||||||||||||||
| 75 | 148 | map { $prr->get_component_name($_) } | |||||||||||||||||||
| 76 | 384 | grep { $prr->is_component_variable($_) } | |||||||||||||||||||
| 77 | 223 | @{ $prr->components } | |||||||||||||||||||
| 78 | ]; | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 153 | ▶ | my $resource_args_from_route = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 81 | # XXX we could try to generate more efficient code here | ||||||||||||||||||||
| 82 | # Something like this: | ||||||||||||||||||||
| 83 | # $args->{id} = [ @_[@indices_of_id_params] ]; | ||||||||||||||||||||
| 84 | # @{$args}{@names_of_non_id_params} = @_[@indices_of_non_id_params] | ||||||||||||||||||||
| 85 | # where the @arrays are pre-computed and closed-over | ||||||||||||||||||||
| 86 | 153 | my $req = shift; | |||||||||||||||||||
| 87 | 153 | my $args = shift; | |||||||||||||||||||
| 88 | 153 | for (@$path_var_names) { #in path param name order | |||||||||||||||||||
| 89 | 93 | ▶ | if (m/^[0-9]+$/) { # an id field | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 90 | 83 | $args->{id}[$_-1] = shift @_; | |||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | else { | ||||||||||||||||||||
| 93 | 10 | $args->{$_} = shift @_; | |||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | } | ||||||||||||||||||||
| 96 | 223 | }; | |||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | # this sub acts as the interface between the router and | ||||||||||||||||||||
| 100 | # the Web::Machine instance handling the resource for that url path | ||||||||||||||||||||
| 101 | 153 | ▶ | my $target = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 102 | 153 | my $request = shift; # URL args from router remain in @_ | |||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 153 | my %resource_args_from_params; | |||||||||||||||||||
| 105 | # perform any required setup for this request & params in @_ | ||||||||||||||||||||
| 106 | 153 | $resource_args_from_route->($request, \%resource_args_from_params, @_); | |||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | warn sprintf "/%s: running %s machine (extra args: %s; default args: %s)\n", | ||||||||||||||||||||
| 109 | $self->path, $resource_class, | ||||||||||||||||||||
| 110 | join(",", sort keys %resource_args_from_params), | ||||||||||||||||||||
| 111 | 0 | join(",", sort keys %{$self->resource_args}) | |||||||||||||||||||
| 112 | 153 | ▶ | if $ENV{WEBAPI_DBIC_DEBUG}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | my $app = Web::Machine->new( | ||||||||||||||||||||
| 115 | resource => $resource_class, | ||||||||||||||||||||
| 116 | 153 | resource_args => [ %{$self->resource_args}, %resource_args_from_params ], | |||||||||||||||||||
| 117 | tracing => $ENV{WEBAPI_DBIC_DEBUG}, | ||||||||||||||||||||
| 118 | 153 | )->to_app; | |||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | #local $SIG{__DIE__} = \&Carp::confess; | ||||||||||||||||||||
| 121 | #Dwarn | ||||||||||||||||||||
| 122 | 153 | my $resp = $app->($request->env); | |||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 153 | return $resp; | |||||||||||||||||||
| 125 | 223 | }; | |||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | return ( | ||||||||||||||||||||
| 128 | 223 | ▶ | path => $self->path, | ||||||||||||||||||
|
Condition50%1/2
$self->validations || {}
MC/DC0%0/1
$self->validations || {}
$self->validations
Truth table50%1/2
A
A= $self->validations
| |||||||||||||||||||||
| 129 | validations => $self->validations || {}, | ||||||||||||||||||||
| 130 | defaults => $self->route_defaults, | ||||||||||||||||||||
| 131 | target => $target, | ||||||||||||||||||||
| 132 | ); | ||||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 25 | 1; | |||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | __END__ | ||||||||||||||||||||