PONAPI/Builder/Role/HasPagination.pm

filter: Statementstmt 90.4% 38 / 42 Branchbran 55.5% 10 / 18 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 1 totaltotal 69.5% 57 / 82 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
« PONAPI/CLI/Command/gen.pm ↑ Summary PONAPI/DAO/Request.pm »
1 # ABSTRACT: document builder - role - pagination
2 package PONAPI::Builder::Role::HasPagination;
3  
4 23 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 23 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 23 ▶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 23 my %allowed_page_keys = map +($_=>1), qw/
14 first
15 last
16 next
17 prev
18 self
19 /;
20  
21 sub add_pagination_links {
22 10 ▶ my ($self, %page_links) = @_;
Subroutine add_pagination_links: called CC=6 SCAR=18.1
Pod undocumented
23  
24 $page_links{self} ||= delete $page_links{current}
25 10 ▶ if exists $page_links{current};
Branch: 1 branch
BranchTrueFalseTotal
if exists $page_links{"current"} 1 9 10
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 10 foreach my $link_name ( keys %page_links ) {
28 die "Tried to add pagination link `$link_name`, not allowed by the spec"
29 31 ▶ unless exists $allowed_page_keys{ $link_name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $allowed_page_keys{$link_name} 1 30 31
30 }
31  
32 9 my $link = $self->req_path;
33  
34 9 my $uri = URI->new($link);
35 9 my $path = $uri->path;
36  
37 $self->links_builder->add_links(
38 map {
39 my $query = $self->_hash_to_uri_query( {
40 26 page => $page_links{$_}
41 }, $uri );
42 26 ( $_ => $path . '?' . $query )
43 }
44 9 ▶ grep scalar keys %{ $page_links{$_} || {} },
Branch: 1 branch
BranchTrueFalseTotal
$page_links{$_} or {} 0 30 30
45 keys %page_links
46 );
47 }
48  
49 sub _hash_to_uri_query {
50 26 ▶ my ($self, $data, $u) = @_;
Subroutine _hash_to_uri_query: called CC=11 SCAR=30.2
51 26 ▶ $u ||= URI->new("", "http");
Condition33%1/3
$u ||= "URI"->new("", "http")
l!l&&r!l&&!r
2600
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 26 for my $d_k ( sort keys %$data ) {
54 26 my $d_v = $data->{$d_k};
55 26 ▶ defined($d_v) or next;
Branch: 1 branch
BranchTrueFalseTotal
defined $d_v or next 0 26 26
56  
57 26 ▶ if ( ref $d_v ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $d_v ne "HASH") 0 26 26
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 26 for my $k ( sort keys %{$d_v} ) {
65 52 my $v = $d_v->{$k};
66 52 ▶ defined($v) or next;
Branch: 1 branch
BranchTrueFalseTotal
defined $v or next 0 52 52
67  
68 52 ▶ 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 52 52
Condition33%1/3
not ref $v or ref $v eq "ARRAY"
l!l&&r!l&&!r
5200
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 52 ▶ join ',' => ( ref $v eq 'ARRAY' ? @{$v} : $v ) );
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'ARRAY' ? : 0 52 52
73 }
74 }
75  
76 26 return $u->query;
77 }
78  
79  
80  
81 23 ▶no Moose::Role; 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 __END__
84  
85 =pod
86  
87 =encoding UTF-8
88  
89 =head1 NAME
90  
91 PONAPI::Builder::Role::HasPagination - document builder - role - pagination
92  
93 =head1 VERSION
94  
95 version 0.002012
96  
97 =head1 AUTHORS
98  
99 =over 4
100  
101 =item *
102  
103 Mickey Nasriachi <mickey@cpan.org>
104  
105 =item *
106  
107 Stevan Little <stevan@cpan.org>
108  
109 =item *
110  
111 Brian Fraser <hugmeir@cpan.org>
112  
113 =back
114  
115 =head1 COPYRIGHT AND LICENSE
116  
117 This software is copyright (c) 2016 by Mickey Nasriachi, Stevan Little, Brian Fraser.
118  
119 This is free software; you can redistribute it and/or modify it under
120 the same terms as the Perl 5 programming language system itself.
121  
122 =cut
« PONAPI/CLI/Command/gen.pm ↑ Summary PONAPI/DAO/Request.pm »