| 1 | # ABSTRACT: document builder - role - pagination | ||||||||||||||||||||||||||||
| 2 | package PONAPI::Document::Builder::Role::HasPagination; | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 15 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 15 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 15 | ▶ | use URI::QueryParam; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # requires 'links_builder'; | ||||||||||||||||||||||||||||
| 10 | # requires 'req_path'; | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | # self isn't really part of pagination, but it can be overriden here | ||||||||||||||||||||||||||||
| 13 | 15 | my %allowed_page_keys = map +($_=>1), qw/ | |||||||||||||||||||||||||||
| 14 | first | ||||||||||||||||||||||||||||
| 15 | last | ||||||||||||||||||||||||||||
| 16 | next | ||||||||||||||||||||||||||||
| 17 | prev | ||||||||||||||||||||||||||||
| 18 | self | ||||||||||||||||||||||||||||
| 19 | /; | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 3 | ▶ | sub add_pagination_links { | ||||||||||||||||||||||||||
|
Subroutine
add_pagination_links: called CC=6 SCAR=18.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 22 | 3 | my ($self, %page_links) = @_; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | $page_links{self} ||= delete $page_links{current} | ||||||||||||||||||||||||||||
| 25 | 3 | ▶ | if exists $page_links{current}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$page_links{"self"} ||= delete $page_links{"current"}
MC/DC0%0/2
$page_links{"self"} ||= delete $page_links{"current"}
$page_links{"self"}
delete $page_links{"current"}
Truth table33%1/3
A ||= B
A= $page_links{"self"}B= delete $page_links{"current"}
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 3 | foreach my $link_name ( keys %page_links ) { | |||||||||||||||||||||||||||
| 28 | die "Tried to add pagination link `$link_name`, not allowed by the spec" | ||||||||||||||||||||||||||||
| 29 | 3 | ▶ | unless exists $allowed_page_keys{ $link_name }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 2 | my $link = $self->req_path; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 2 | my $uri = URI->new($link); | |||||||||||||||||||||||||||
| 35 | 2 | my $path = $uri->path; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | $self->links_builder->add_links( | ||||||||||||||||||||||||||||
| 38 | map { | ||||||||||||||||||||||||||||
| 39 | my $query = $self->_hash_to_uri_query( { | ||||||||||||||||||||||||||||
| 40 | 2 | page => $page_links{$_} | |||||||||||||||||||||||||||
| 41 | }, $uri ); | ||||||||||||||||||||||||||||
| 42 | 2 | ( $_ => $path . '?' . $query ) | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 2 | ▶ | grep scalar keys %{ $page_links{$_} || {} }, | ||||||||||||||||||||||||||
|
Condition50%1/2
$page_links{$_} || {}
MC/DC0%0/1
$page_links{$_} || {}
$page_links{$_}
Truth table50%1/2
A
A= $page_links{$_}
| |||||||||||||||||||||||||||||
| 45 | keys %page_links | ||||||||||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 2 | ▶ | sub _hash_to_uri_query { | ||||||||||||||||||||||||||
|
Subroutine
_hash_to_uri_query: called CC=11 SCAR=30.2
| |||||||||||||||||||||||||||||
| 50 | 2 | my ($self, $data, $u) = @_; | |||||||||||||||||||||||||||
| 51 | 2 | ▶ | $u ||= URI->new("", "http"); | ||||||||||||||||||||||||||
|
Condition33%1/3
$u ||= "URI"->new("", "http")
MC/DC0%0/2
$u ||= "URI"->new("", "http")
$u
"URI"->new("", "http")
Truth table33%1/3
A ||= B
A= $uB= "URI"->new("", "http")
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 2 | for my $d_k ( sort keys %$data ) { | |||||||||||||||||||||||||||
| 54 | 2 | my $d_v = $data->{$d_k}; | |||||||||||||||||||||||||||
| 55 | 2 | ▶ | defined($d_v) or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 2 | ▶ | if ( ref $d_v ne 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | $u->query_param( $d_k => | ||||||||||||||||||||||||||||
| 59 | 0 | ▶ | join ',' => ( ref $d_v eq 'ARRAY' ? @{$d_v} : $d_v ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 0 | next; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | # HASH | ||||||||||||||||||||||||||||
| 64 | 2 | for my $k ( sort keys %{$d_v} ) { | |||||||||||||||||||||||||||
| 65 | 4 | my $v = $d_v->{$k}; | |||||||||||||||||||||||||||
| 66 | 4 | ▶ | defined($v) or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 4 | ▶ | die "_hash_to_uri_query: nested value can be scalar/arrayref only" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not ref $v or ref $v eq "ARRAY"
MC/DC0%0/2
not ref $v or ref $v eq "ARRAY"
not ref $v
ref $v eq "ARRAY"
Truth table33%1/3
not(A) or B
A= not ref $vB= ref $v eq "ARRAY"
| |||||||||||||||||||||||||||||
| 69 | unless !ref $v or ref $v eq 'ARRAY'; | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | $u->query_param( $d_k . '[' . $k . ']' => | ||||||||||||||||||||||||||||
| 72 | 0 | ▶ | join ',' => ( ref $v eq 'ARRAY' ? @{$v} : $v ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 2 | return $u->query; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 15 | ▶ | no Moose::Role; 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | __END__ | ||||||||||||||||||||||||||||