Document/Builder/Role/HasPagination.pm

filter: Statementstmt 90.4% 38 / 42 Branchbran 56.2% 9 / 16 Conditioncond 36.3% 4 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 1 totaltotal 68.6% 57 / 83 CC 16 SCAR 25.4
CC
16
Cov
74%
_hash_to_uri_query
20.5
add_pagination_links
6.1
BEGIN
1.0
CRAP 12.7
« Names.pm ↑ Summary Document/Builder/Document.pm »
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
BranchTrueFalseTotal
if exists $page_links{"current"} 1 2 3
Condition33%1/3
$page_links{"self"} ||= delete $page_links{"current"}
l!l&&r!l&&!r
010
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"}
ABresult
1X1
011
000
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
BranchTrueFalseTotal
unless exists $allowed_page_keys{$link_name} 1 2 3
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{$_} || {}
l!l
20
MC/DC0%0/1
$page_links{$_} || {}
$page_links{$_}
Truth table50%1/2
A
A= $page_links{$_}
Aresult
11
00
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")
l!l&&r!l&&!r
200
MC/DC0%0/2
$u ||= "URI"->new("", "http")
$u "URI"->new("", "http")
Truth table33%1/3
A ||= B
A= $uB= "URI"->new("", "http")
ABresult
1X1
011
000
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
BranchTrueFalseTotal
defined $d_v or next 2 0 2
56  
57 2 ▶ if ( ref $d_v ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $d_v ne "HASH") 0 2 2
58 $u->query_param( $d_k =>
59 0 ▶ join ',' => ( ref $d_v eq 'ARRAY' ? @{$d_v} : $d_v ) );
Branch: 1 branch
BranchTrueFalseTotal
ref $d_v eq 'ARRAY' ? : 0 0 0
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
BranchTrueFalseTotal
defined $v or next 4 0 4
67  
68 4 ▶ die "_hash_to_uri_query: nested value can be scalar/arrayref only"
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $v or ref $v eq "ARRAY" 0 4 4
Condition33%1/3
not ref $v or ref $v eq "ARRAY"
l!l&&r!l&&!r
400
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"
ABresult
1X1
011
000
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
BranchTrueFalseTotal
ref $v eq 'ARRAY' ? : 0 4 4
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__
« Names.pm ↑ Summary Document/Builder/Document.pm »