| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WebAPI::DBIC::Route; | 
| 2 |  |  |  |  |  |  | $WebAPI::DBIC::Route::VERSION = '0.004001'; | 
| 3 |  |  |  |  |  |  |  | 
| 4 | 4 |  |  | 4 |  | 6366466 | use Moo; | 
|  | 4 |  |  |  |  | 32540 |  | 
|  | 4 |  |  |  |  | 7543 |  | 
| 5 | 4 |  |  | 4 |  | 5915 | use MooX::StrictConstructor; | 
|  | 4 |  |  |  |  | 35026 |  | 
|  | 4 |  |  |  |  | 26 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 4 |  |  | 4 |  | 48589 | use Module::Runtime qw(use_module); | 
|  | 4 |  |  |  |  | 39 |  | 
|  | 4 |  |  |  |  | 25 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | has path => ( | 
| 11 |  |  |  |  |  |  | is => 'ro', | 
| 12 |  |  |  |  |  |  | required => 1, | 
| 13 |  |  |  |  |  |  | ); | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | has resource_class => ( | 
| 16 |  |  |  |  |  |  | is => 'ro', | 
| 17 |  |  |  |  |  |  | required => 1, | 
| 18 |  |  |  |  |  |  | ); | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | has resource_args => ( | 
| 21 |  |  |  |  |  |  | is => 'ro', | 
| 22 |  |  |  |  |  |  | required => 1, | 
| 23 |  |  |  |  |  |  | ); | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | has route_defaults => ( | 
| 26 |  |  |  |  |  |  | is => 'ro', | 
| 27 |  |  |  |  |  |  | default => sub { {} }, | 
| 28 |  |  |  |  |  |  | ); | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | has validations => ( | 
| 31 |  |  |  |  |  |  | is => 'ro', | 
| 32 |  |  |  |  |  |  | default => sub { {} }, | 
| 33 |  |  |  |  |  |  | ); | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | sub BUILD { | 
| 37 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 38 |  |  |  |  |  |  |  | 
| 39 | 0 |  |  |  |  |  | my $resource_class = $self->resource_class; | 
| 40 | 0 |  |  |  |  |  | my $route_defaults = $self->route_defaults; | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 0 | 0 |  |  |  |  | if ($ENV{WEBAPI_DBIC_DEBUG}) { | 
| 43 | 0 |  |  |  |  |  | (my $class = $resource_class) =~ s/^WebAPI::DBIC::Resource//; | 
| 44 | 0 |  |  |  |  |  | warn sprintf "/%s => %s (%s)\n", | 
| 45 |  |  |  |  |  |  | $self->path, $class, | 
| 46 | 0 |  |  |  |  |  | join(' ', map { "$_=$route_defaults->{$_}" } keys %$route_defaults); | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  |  | 
| 49 | 0 |  |  |  |  |  | use_module $resource_class; | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 0 | 0 |  |  |  |  | if (my $set = $self->resource_args->{set}) { | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | # we use the 'result_class' key in the route_defaults to lookup the route | 
| 54 |  |  |  |  |  |  | # for a given result_class | 
| 55 | 0 |  | 0 |  |  |  | $route_defaults->{result_class} = $set && $set->result_class; | 
| 56 |  |  |  |  |  |  | } | 
| 57 |  |  |  |  |  |  | else { | 
| 58 | 0 | 0 |  |  |  |  | warn sprintf "/%s resource_class %s has 'set' method but resource_args does not include 'set'", | 
| 59 |  |  |  |  |  |  | $self->path, $resource_class | 
| 60 |  |  |  |  |  |  | if $resource_class->can('set'); | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 0 |  |  |  |  |  | return; | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | sub as_add_route_args { | 
| 68 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 0 |  |  |  |  |  | my $resource_class = $self->resource_class; | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | # introspect path to extract path param :names | 
| 73 | 0 |  |  |  |  |  | my $prr = Path::Router::Route->new(path => $self->path); | 
| 74 | 0 |  |  |  |  |  | my $path_var_names = [ | 
| 75 | 0 |  |  |  |  |  | map { $prr->get_component_name($_) } | 
| 76 | 0 |  |  |  |  |  | grep { $prr->is_component_variable($_) } | 
| 77 | 0 |  |  |  |  |  | @{ $prr->components } | 
| 78 |  |  |  |  |  |  | ]; | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | my $resource_args_from_route = sub { | 
| 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 | 0 |  |  | 0 |  |  | my $req = shift; | 
| 87 | 0 |  |  |  |  |  | my $args = shift; | 
| 88 | 0 |  |  |  |  |  | for (@$path_var_names) { #in path param name order | 
| 89 | 0 | 0 |  |  |  |  | if (m/^[0-9]+$/) { # an id field | 
| 90 | 0 |  |  |  |  |  | $args->{id}[$_-1] = shift @_; | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  | else { | 
| 93 | 0 |  |  |  |  |  | $args->{$_} = shift @_; | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  | } | 
| 96 | 0 |  |  |  |  |  | }; | 
| 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 |  |  |  |  |  |  | my $target = sub { | 
| 102 | 0 |  |  | 0 |  |  | my $request = shift; # URL args from router remain in @_ | 
| 103 |  |  |  |  |  |  |  | 
| 104 | 0 |  |  |  |  |  | my %resource_args_from_params; | 
| 105 |  |  |  |  |  |  | # perform any required setup for this request & params in @_ | 
| 106 | 0 |  |  |  |  |  | $resource_args_from_route->($request, \%resource_args_from_params, @_); | 
| 107 |  |  |  |  |  |  |  | 
| 108 | 0 |  |  |  |  |  | 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 | 0 |  |  |  |  | join(",", sort keys %{$self->resource_args}) | 
| 112 |  |  |  |  |  |  | if $ENV{WEBAPI_DBIC_DEBUG}; | 
| 113 |  |  |  |  |  |  |  | 
| 114 | 0 |  |  |  |  |  | my $app = Web::Machine->new( | 
| 115 |  |  |  |  |  |  | resource => $resource_class, | 
| 116 | 0 |  |  |  |  |  | resource_args => [ %{$self->resource_args}, %resource_args_from_params ], | 
| 117 |  |  |  |  |  |  | tracing => $ENV{WEBAPI_DBIC_DEBUG}, | 
| 118 |  |  |  |  |  |  | )->to_app; | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | #local $SIG{__DIE__} = \&Carp::confess; | 
| 121 |  |  |  |  |  |  | #Dwarn | 
| 122 | 0 |  |  |  |  |  | my $resp = $app->($request->env); | 
| 123 |  |  |  |  |  |  |  | 
| 124 | 0 |  |  |  |  |  | return $resp; | 
| 125 | 0 |  |  |  |  |  | }; | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | return ( | 
| 128 | 0 |  | 0 |  |  |  | path        => $self->path, | 
| 129 |  |  |  |  |  |  | validations => $self->validations || {}, | 
| 130 |  |  |  |  |  |  | defaults    => $self->route_defaults, | 
| 131 |  |  |  |  |  |  | target      => $target, | 
| 132 |  |  |  |  |  |  | ); | 
| 133 |  |  |  |  |  |  | } | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | 1; | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | __END__ | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | =pod | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | =encoding UTF-8 | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | =head1 NAME | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | WebAPI::DBIC::Route | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | =head1 VERSION | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | version 0.004001 | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | =head1 NAME | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | WebAPI::DBIC::Route - A URL path to a WebAPI::DBIC Resource | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | =head1 AUTHOR | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | Tim Bunce <Tim.Bunce@pobox.com> | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | This software is copyright (c) 2015 by Tim Bunce. | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | This is free software; you can redistribute it and/or modify it under | 
| 167 |  |  |  |  |  |  | the same terms as the Perl 5 programming language system itself. | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | =cut |