blib/lib/Kelp/Util.pm

filter: Statementstmt 93.6% 118 / 126 Branchbran 79.4% 54 / 68 Conditioncond 81.8% 18 / 22 MC/DCmcdc 76.9% 10 / 13 Subroutinesub 100% 22 / 22 Podpod 77.7% 7 / 9 totaltotal 88.0% 229 / 260 CC 44 SCAR 20.0
CC
44
Cov
88%
merge
15.4
_DEBUG
10.3
camelize
9.0
CRAP 7.4
« blib/lib/Kelp/Routes.pm ↑ Summary blib/lib/Kelp/Request.pm »
1 package Kelp::Util;
2  
3 # No Kelp::Base here, because Kelp::Base uses Kelp::Util;
4 56 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 56 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 56 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 56 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 56 ▶use Data::Dumper qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 56 ▶use Encode qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 56 ▶use Class::Inspector;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 56 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 56 ▶use Test::Deep::NoTest qw(eq_deeply);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 56 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # improve error locations of croak
17 56 our @CARP_NOT = (
18 qw(
19 Kelp
20 Kelp::Base
21 Kelp::Routes
22 Kelp::Context
23 )
24 );
25  
26 sub _DEBUG
27 909 ▶{
Subroutine _DEBUG: called CC=4 SCAR=23.3
28 909 my ($stage, @messages) = @_;
29 909 my $env = $ENV{KELP_DEBUG};
30 909 ▶ return if !$env;
Branch: 1 branch
BranchTrueFalseTotal
unless $env 909 0 909
31 0 ▶ return if !grep { lc $env eq $_ } '1', 'all', lc $stage;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {lc $env eq $_;} 1, "all", lc $stage 0 0 0
32  
33 0 local $Data::Dumper::Sortkeys = 1;
34 my $message = join ' ', map {
35 0 ▶ ref $_ ? Data::Dumper::Dumper($_) : $_
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
36 } @messages;
37  
38 0 print "DEBUG: $message\n";
39 }
40  
41 sub camelize
42 202 ▶{
Subroutine camelize: called CC=9 SCAR=22.0
Pod documented
43 202 my ($string, $base, $class_only) = @_;
44 202 ▶ return $string unless $string;
Branch: 1 branch
BranchTrueFalseTotal
unless $string 4 198 202
45  
46 198 ▶ my $sigil = defined $string && $string =~ s/^(\+)// ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $string && $string =~ s/^(\+)// ? : 6 192 198
Condition66%2/3
defined $string and $string =~ s/^(\+)//
!ll&&!rl&&r
01926
MC/DC50%1/2
defined $string and $string =~ s/^(\+)//
defined $string $string =~ s/^(\+)//
Truth table66%2/3
A and B
A= defined $stringB= $string =~ s/^(\+)//
ABresult
0X0
100
111
47 198 ▶ $base = undef if $sigil;
Branch: 1 branch
BranchTrueFalseTotal
if $sigil 6 192 198
48  
49 198 my @parts;
50 198 ▶ if ($string !~ /#/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $string =~ /#/) { } 71 127 198
51  
52 # do not camelize if it doesn't look like a camelize string
53 71 @parts = ($string);
54 }
55 else {
56 127 @parts = split /\#/, $string;
57 127 my $sub = pop @parts;
58  
59 127 ▶ push @parts, $sub
Branch: 1 branch
BranchTrueFalseTotal
if $class_only 24 103 127
60 if $class_only;
61  
62 @parts = map {
63 127 join '', map { ucfirst lc } split /\_/
64 } @parts;
65  
66 127 ▶ push @parts, $sub
Branch: 1 branch
BranchTrueFalseTotal
unless $class_only 103 24 127
67 if !$class_only;
68 }
69  
70 198 ▶ unshift @parts, $base if $base;
Branch: 1 branch
BranchTrueFalseTotal
if $base 81 117 198
71 198 return join('::', @parts);
72 }
73  
74 sub extract_class
75 128 ▶{
Subroutine extract_class: called CC=4 SCAR=13.9
Pod documented
76 128 my ($string) = @_;
77 128 ▶ return undef unless $string;
Branch: 1 branch
BranchTrueFalseTotal
unless $string 1 127 128
78  
79 127 ▶ if ($string =~ /^(.+)::(\w+)$/ && $1 ne 'main') {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^(.+)::(\w+)$/ and $1 ne "main") 109 18 127
Condition100%3/3
$string =~ /^(.+)::(\w+)$/ and $1 ne "main"
!ll&&!rl&&r
711109
MC/DC100%2/2
$string =~ /^(.+)::(\w+)$/ and $1 ne "main"
$string =~ /^(.+)::(\w+)$/ $1 ne "main"
Truth table100%3/3
A and B
A= $string =~ /^(.+)::(\w+)$/B= $1 ne "main"
ABresult
0X0
100
111
80 109 return $1;
81 }
82  
83 18 return undef;
84 }
85  
86 sub extract_function
87 108 ▶{
Subroutine extract_function: called CC=3 SCAR=11.0
Pod documented
88 108 my ($string) = @_;
89 108 ▶ return undef unless $string;
Branch: 1 branch
BranchTrueFalseTotal
unless $string 1 107 108
90  
91 107 ▶ if ($string =~ /^(.+)::(\w+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^(.+)::(\w+)$/) 106 1 107
92 106 return $2;
93 }
94  
95 1 return $string;
96 }
97  
98 sub effective_charset
99 304 ▶{
Subroutine effective_charset: called CC=2 SCAR=6.9
Pod documented
100 304 my $this_charset = shift;
101 304 ▶ return Encode::find_encoding($this_charset) ? $this_charset : undef;
Branch: 1 branch
BranchTrueFalseTotal
&Encode::find_encoding($this_charset) ? : 236 68 304
102 }
103  
104 sub charset_encode
105 242 ▶{
Subroutine charset_encode: called CC=2 SCAR=6.9
Pod undocumented
106 242 my ($charset, $string) = @_;
107  
108 242 ▶ return $string unless $charset;
Branch: 1 branch
BranchTrueFalseTotal
unless $charset 1 241 242
109 241 return Encode::encode $charset, $string;
110 }
111  
112 sub charset_decode
113 617 ▶{
Subroutine charset_decode: called CC=2 SCAR=6.9
Pod undocumented
114 617 my ($charset, $string) = @_;
115  
116 617 ▶ return $string unless $charset;
Branch: 1 branch
BranchTrueFalseTotal
unless $charset 1 616 617
117 616 return Encode::decode $charset, $string;
118 }
119  
120 sub adapt_psgi
121 5 ▶{
Subroutine adapt_psgi: called CC=2 SCAR=7.1
Pod documented
122 5 my ($app) = @_;
123  
124 5 ▶ croak 'Cannot adapt_psgi, unknown destination type - must be a coderef'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $app eq "CODE" 0 5 5
125 unless ref $app eq 'CODE';
126  
127 11 ▶ return sub {
Subroutine __ANON__: called CC=8 SCAR=21.3
128 11 my $context = shift->context;
129 11 ▶ my $path = charset_encode($context->app->request_charset, pop() // '');
Condition100%2/2
pop() // ""
l!l
65
MC/DC100%1/1
pop() // ""
pop()
Truth table100%2/2
A
A= pop()
Aresult
11
00
130 11 my $env = $context->req->env;
131  
132 # remember script and path
133 11 my $orig_script = $env->{SCRIPT_NAME};
134 11 my $orig_path = $env->{PATH_INFO};
135  
136 # adjust slashes in paths
137 11 ▶ my $trailing_slash = $orig_path =~ m{/$} ? '/' : '';
Branch: 1 branch
BranchTrueFalseTotal
$orig_path =~ m[/$] ? : 4 7 11
138 11 $path =~ s{^/?}{/};
139 11 $path =~ s{/?$}{$trailing_slash};
140  
141 11 my $result = do {
142  
143 # adjust script and path
144 11 local $env->{SCRIPT_NAME} = $orig_path;
145 11 $env->{SCRIPT_NAME} =~ s{\Q$path\E$}{};
146 11 local $env->{PATH_INFO} = $path;
147  
148 # run the callback
149 11 $app->($env);
150 };
151  
152 # produce a response
153 11 ▶ if (ref $result eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $result eq 'ARRAY') { } 11 0 11
elsif (ref $result eq 'CODE') { } 0 0 0
154 11 my ($status, $headers, $body) = @{$result};
155  
156 11 my $res = $context->res;
157 11 ▶ $res->status($status) if $status;
Branch: 1 branch
BranchTrueFalseTotal
if $status 11 0 11
158 11 ▶ $res->headers($headers) if $headers;
Branch: 1 branch
BranchTrueFalseTotal
if $headers 11 0 11
159 11 ▶ $res->body($body) if $body;
Branch: 1 branch
BranchTrueFalseTotal
if $body 11 0 11
160 11 $res->rendered(1);
161 }
162 elsif (ref $result eq 'CODE') {
163 0 return $result;
164 }
165  
166 # this should be an error unless already rendered
167 11 return;
168 5 };
169 }
170  
171 sub load_package
172 1752 ▶{
Subroutine load_package: called CC=3 SCAR=11.0
Pod documented
173 1752 my $package = shift;
174 1752 state $loaded = {};
175  
176 # only load package once for a given class name
177 1752 ▶ return $loaded->{$package} //= do {
Condition100%3/3
$loaded->{$package} //= do {
l!l&&r!l&&!r
13853661
MC/DC100%2/2
$loaded->{$package} //= do { Plack::Util::load_class($package) unless "Class::Inspector"->loaded($package); $package }
$loaded->{$package} do { Plack::Util::load_class($package) unless "Class::Inspector"->loaded($package); $package }
Truth table100%3/3
A //= B
A= $loaded->{$package}B= do { Plack::Util::load_class($package) unless "Class::Inspector"->loaded($package); $package }
ABresult
1X1
011
000
178 367 ▶ Plack::Util::load_class($package)
Branch: 1 branch
BranchTrueFalseTotal
unless "Class::Inspector"->loaded($package) 293 74 367
179 unless Class::Inspector->loaded($package);
180 364 $package;
181 };
182 }
183  
184 sub merge
185 160 ▶{
Subroutine merge: called CC=15 SCAR=27.3
Pod documented
186 160 my ($a, $b, $allow_blessed, $sigil) = @_;
187 160 ▶ my $ref = $allow_blessed ? sub { reftype $_[0] // '' } : sub { ref $_[0] };
Subroutine __ANON__: called CC=1 SCAR=0.8
Branch: 1 branch
BranchTrueFalseTotal
$allow_blessed ? : 0 160 160
Condition0%0/2
builtin::reftype($_[0]) // ""
l!l
00
MC/DC0%0/1
builtin::reftype($_[0]) // ""
builtin::reftype($_[0])
Truth table0%0/2
A
A= builtin::reftype($_[0])
Aresult
11
00
188  
189 160 ▶ return $b
Branch: 1 branch
BranchTrueFalseTotal
if not &$ref($a) && &$ref($b) or &$ref($a) ne &$ref($b) 17 143 160
Condition83%5/6
not &$ref($a) && &$ref($b) or &$ref($a) ne &$ref($b)
l!l&&r!l&&!r
152143
&$ref($a) && &$ref($b)
!ll&&!rl&&r
150145
MC/DC66%2/3
not &$ref($a) && &$ref($b) or &$ref($a) ne &$ref($b)
&$ref($a) &$ref($b) &$ref($a) ne &$ref($b)
Truth table75%3/4
not(A && B) or C
A= &$ref($a)B= &$ref($b)C= &$ref($a) ne &$ref($b)
ABCresult
0XX1
10X1
1111
1100
190 if !$ref->($a)
191 || !$ref->($b)
192 || $ref->($a) ne $ref->($b);
193  
194 143 ▶ if ($ref->($a) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (&$ref($a) eq 'ARRAY') { } 44 99 143
elsif (&$ref($a) eq 'HASH') { } 99 0 99
195 44 ▶ return $b unless $sigil;
Branch: 1 branch
BranchTrueFalseTotal
unless $sigil 11 33 44
196 33 ▶ if ($sigil eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sigil eq '+') { } 5 28 33
197 5 for my $e (@$b) {
198 11 ▶ push @$a, $e unless grep { eq_deeply($_, $e) } @$a;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {eq_deeply($_, $e);} @$a 6 5 11
199 }
200 }
201 else {
202 $a = [
203 grep {
204 28 my $e = $_;
205 57 !grep { eq_deeply($_, $e) } @$b
206 } @$a
207 ];
208 }
209 33 return $a;
210 }
211 elsif ($ref->($a) eq 'HASH') {
212 99 for my $k (keys %$b) {
213  
214 # If the key is an array then look for a merge sigil
215 110 ▶ my $s = $ref->($b->{$k}) eq 'ARRAY' && $k =~ s/^(\+|\-)// ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
&$ref($b->{$k}) eq 'ARRAY' && $k =~ s/^(\+|\-)// ? : 33 77 110
Condition100%3/3
&$ref($b->{$k}) eq "ARRAY" and $k =~ s/^(\+|\-)//
!ll&&!rl&&r
641333
MC/DC100%2/2
&$ref($b->{$k}) eq "ARRAY" and $k =~ s/^(\+|\-)//
&$ref($b->{$k}) eq "ARRAY" $k =~ s/^(\+|\-)//
Truth table100%3/3
A and B
A= &$ref($b->{$k}) eq "ARRAY"B= $k =~ s/^(\+|\-)//
ABresult
0X0
100
111
216  
217 $a->{$k} =
218 exists $a->{$k}
219 ? merge($a->{$k}, $b->{"$s$k"}, $allow_blessed, $s)
220 110 ▶ : $b->{$k};
Branch: 1 branch
BranchTrueFalseTotal
exists $a->{$k} ? : 86 24 110
221 }
222  
223 99 return $a;
224 }
225 0 return $b;
226 }
227  
228 56 1;
229  
230 __END__
231  
232 =pod
233  
234 =head1 NAME
235  
236 Kelp::Util - Kelp general utility functions
237  
238 =head1 SYNOPSIS
239  
240 use Kelp::Util;
241  
242 # MyApp::A::b
243 say Kelp::Util::camelize('a#b', 'MyApp');
244  
245 # Controller
246 say Kelp::Util::extract_class('Controller::Action');
247  
248 # Action
249 say Kelp::Util::extract_function('Controller::Action');
250  
251  
252 =head1 DESCRIPTION
253  
254 These are some helpful functions not seen in L<Plack::Util>.
255  
256 =head1 FUNCTIONS
257  
258 No functions are exported and have to be used with full package name prefix.
259  
260 =head2 camelize
261  
262 This function accepts a string and a base class. Does three things:
263  
264 =over
265  
266 =item * transforms snake_case into CamelCase for class names (with lowercasing)
267  
268 =item * replaces hashes C<#> with Perl package separators C<::>
269  
270 =item * constructs the class name in similar fasion as L<Plack::Util/load_class>
271  
272 =back
273  
274 The returned string will have leading C<+> removed and will be prepended with
275 the second argument if there was no C<+>. An optional third argument can also
276 be passed to treat the entire string as a class name.
277  
278 Will not do the camelizing if there is no C<#> sign in the string, even if
279 the third argument is present.
280  
281 =head2 extract_class
282  
283 Extracts the class name from a C<Controller::action> string. Returns undef if
284 no class in the string or the class is C<main>.
285  
286 =head2 extract_function
287  
288 Extracts the function name from a string. If there is no class name, returns
289 the entire string. Returns undef for empty strings.
290  
291 =head2 effective_charset
292  
293 Takes a charset name and returns it back if it is supported by Encode.
294 If there is no charset or it isn't supported, undef will be returned.
295  
296 =head2 adapt_psgi
297  
298 Transforms a given Plack/PSGI application (in form of a runner subroutine) to a
299 Kelp route handler. The route handler will take the last argument matched from
300 a pattern and adjust the proper environmental paths of the PSGI standard. This
301 will make the application mostly behave as if it was mounted directly where the
302 route points minus the last placeholder. For example, route C</app> will adjust
303 the script name to C<'/app'> and path info will always be empty, while route
304 C<< /app/>rest >> will have the same script name and path info set to whatever
305 was after C</app> in the URL (trailing slashes included).
306  
307 NOTE: having more than one placeholder in the pattern is mostly wasteful, as
308 their matched values will not be handled in any way (other than allowing a
309 varying request path).
310  
311 =head2 load_package
312  
313 Takes a name of a package and loads it efficiently.
314  
315 =head2 merge
316  
317 my $merged = Kelp::Util::merge($val1, $val2, $allow_blessed);
318  
319 Merges two structures. Used by config module to merge configuration files.
320 Optionally, a third argument can be passed to allow merging values of blessed
321 references as well.
322  
323 =cut
324  
« blib/lib/Kelp/Routes.pm ↑ Summary blib/lib/Kelp/Request.pm »