Yancy/Util.pm

filter: Statementstmt 92.4% 172 / 186 Branchbran 87.2% 82 / 94 Conditioncond 78.9% 30 / 38 MC/DCmcdc 59.0% 13 / 22 Subroutinesub 95.0% 38 / 40 Podpod 100% 11 / 11 totaltotal 88.4% 346 / 391 CC 67 SCAR 20.9
CC
67
Cov
89%
match
18.9
copy_inline_refs
12.0
order_by
11.3
CRAP 8.1
« Yancy/Plugin/File.pm ↑ Summary Yancy/Plugin/Auth/Token.pm »
1 package Yancy::Util;
2 43 our $VERSION = '1.088';
3 # ABSTRACT: Utilities for Yancy
4  
5 #pod =head1 SYNOPSIS
6 #pod
7 #pod use Yancy::Util qw( load_backend );
8 #pod my $be = load_backend( 'memory://localhost', $schema );
9 #pod
10 #pod use Yancy::Util qw( curry );
11 #pod my $helper = curry( \&_helper_sub, @args );
12 #pod
13 #pod use Yancy::Util qw( currym );
14 #pod my $sub = currym( $object, 'method_name', @args );
15 #pod
16 #pod use Yancy::Util qw( match );
17 #pod if ( match( $where, $item ) ) {
18 #pod say 'Matched!';
19 #pod }
20 #pod
21 #pod use Yancy::Util qw( fill_brackets );
22 #pod my $value = fill_brackets( $template, $item );
23 #pod
24 #pod =head1 DESCRIPTION
25 #pod
26 #pod This module contains utility functions for Yancy.
27 #pod
28 #pod =head1 SEE ALSO
29 #pod
30 #pod L<Yancy>
31 #pod
32 #pod =cut
33  
34 43 ▶use Mojo::Base '-strict';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 43 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 43 ▶use List::Util qw( all any none first );
Subroutine BEGIN: called CC=1 SCAR=0.0
37 43 ▶use Mojo::Loader qw( load_class );
Subroutine BEGIN: called CC=1 SCAR=0.0
38 43 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
39 43 ▶use Mojo::JSON::Pointer;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 43 ▶use Mojo::JSON qw( to_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
41 43 ▶use Mojo::Util qw( xml_escape );
Subroutine BEGIN: called CC=1 SCAR=0.0
42 43 ▶use Carp qw( carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
43 43 ▶use JSON::Validator;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 43 our @EXPORT_OK = qw( load_backend curry currym copy_inline_refs match derp fill_brackets
46 is_type order_by is_format json_validator );
47  
48 #pod =sub load_backend
49 #pod
50 #pod my $backend = load_backend( $backend_url, $schema );
51 #pod my $backend = load_backend( { $backend_name => $arg }, $schema );
52 #pod my $backend = load_backend( $db_object, $schema );
53 #pod
54 #pod Get a Yancy backend from the given backend URL, or from a hash reference
55 #pod with a backend name and optional argument. The C<$schema> hash is
56 #pod the configured JSON schema for this backend.
57 #pod
58 #pod A backend URL should begin with a name followed by a colon. The first
59 #pod letter of the name will be capitalized, and used to build a class name
60 #pod in the C<Yancy::Backend> namespace.
61 #pod
62 #pod The C<$backend_name> should be the name of a module in the
63 #pod C<Yancy::Backend> namespace. The C<$arg> is handled by the backend
64 #pod module. Read your backend module's documentation for details.
65 #pod
66 #pod The C<$db_object> can be one of: L<Mojo::Pg>, L<Mojo::mysql>,
67 #pod L<Mojo::SQLite>, or a subclass of L<DBIx::Class::Schema>. The
68 #pod appropriate backend object will be created.
69 #pod
70 #pod See L<Yancy::Guides::Schema/Database Backend> for information about
71 #pod backend URLs and L<Yancy::Backend> for more information about backend
72 #pod objects.
73 #pod
74 #pod =cut
75  
76 # This allows users to pass in the database object directly
77 43 our %BACKEND_CLASSES = (
78 'Mojo::Pg' => 'pg',
79 'Mojo::mysql' => 'mysql',
80 'Mojo::SQLite' => 'sqlite',
81 'DBIx::Class::Schema' => 'dbic',
82 );
83  
84 # Aliases allow the user to specify the same string as they pass to
85 # their database object
86 43 our %TYPE_ALIAS = (
87 postgresql => 'pg',
88 );
89  
90 86 ▶sub load_backend {
Subroutine load_backend: called CC=8 SCAR=22.9
Pod documented
91 86 my ( $config, $schema ) = @_;
92 86 my ( $type, $arg );
93 86 ▶ if ( !ref $config ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $config) { } 85 1 86
elsif (builtin::blessed($config)) { } 0 1 1
94 85 ( $type ) = $config =~ m{^([^:]+)};
95 85 ▶ $type = $TYPE_ALIAS{ $type } // $type;
Condition33%1/3
$Yancy::Util::TYPE_ALIAS{$type} // $type
l!l&&r!l&&!r
0850
MC/DC0%0/2
$Yancy::Util::TYPE_ALIAS{$type} // $type
$Yancy::Util::TYPE_ALIAS{$type} $type
Truth table33%1/3
A // B
A= $Yancy::Util::TYPE_ALIAS{$type}B= $type
ABresult
1X1
011
000
96 85 $arg = $config;
97 }
98 elsif ( blessed $config ) {
99 0 for my $class ( keys %BACKEND_CLASSES ) {
100 0 ▶ if ( $config->isa( $class ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->isa($class)) 0 0 0
101 0 ( $type, $arg ) = ( $BACKEND_CLASSES{ $class }, $config );
102 0 last;
103 }
104 }
105 }
106 else {
107 1 ( $type, $arg ) = %{ $config };
108 }
109 86 my $class = 'Yancy::Backend::' . ucfirst $type;
110 86 ▶ if ( my $e = load_class( $class ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = load_class($class)) 2 84 86
111 2 ▶ die ref $e ? "Could not load class $class: $e" : "Could not find class $class";
Branch: 1 branch
BranchTrueFalseTotal
ref $e ? : 1 1 2
112 }
113 84 return $class->new( $arg, $schema );
114 }
115  
116 #pod =sub curry
117 #pod
118 #pod my $curried_sub = curry( $sub, @args );
119 #pod
120 #pod Return a new subref that, when called, will call the passed-in subref with
121 #pod the passed-in C<@args> first.
122 #pod
123 #pod For example:
124 #pod
125 #pod my $add = sub {
126 #pod my ( $lop, $rop ) = @_;
127 #pod return $lop + $rop;
128 #pod };
129 #pod my $add_four = curry( $add, 4 );
130 #pod say $add_four->( 1 ); # 5
131 #pod say $add_four->( 2 ); # 6
132 #pod say $add_four->( 3 ); # 7
133 #pod
134 #pod This is more-accurately called L<partial
135 #pod application|https://en.wikipedia.org/wiki/Partial_application>, but
136 #pod C<curry> is shorter.
137 #pod
138 #pod =cut
139  
140 611 ▶sub curry {
Subroutine curry: called CC=1 SCAR=0.0
Pod documented
141 611 my ( $sub, @args ) = @_;
142 611 ▶ return sub { $sub->( @args, @_ ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
143 }
144  
145 #pod =sub currym
146 #pod
147 #pod my $curried_sub = currym( $obj, $method, @args );
148 #pod
149 #pod Return a subref that, when called, will call given C<$method> on the
150 #pod given C<$obj> with any passed-in C<@args> first.
151 #pod
152 #pod See L</curry> for an example.
153 #pod
154 #pod =cut
155  
156 507 ▶sub currym {
Subroutine currym: called CC=2 SCAR=6.9
Pod documented
157 507 my ( $obj, $meth, @args ) = @_;
158 507 ▶ my $sub = $obj->can( $meth )
Condition100%2/2
$obj->can($meth) || die(sprintf(q[Can't curry method "%s" on object of type "%s": Method is not implemented], $meth, builtin::blessed($obj)))
l!l
5061
MC/DC100%1/1
$obj->can($meth) || die(sprintf(q[Can't curry method "%s" on object of type "%s": Method is not implemented], $meth, builtin::blessed($obj)))
$obj->can($meth)
Truth table100%2/2
A
A= $obj->can($meth)
Aresult
11
00
159 || die sprintf q{Can't curry method "%s" on object of type "%s": Method is not implemented},
160 $meth, blessed( $obj );
161 506 return curry( $sub, $obj, @args );
162 }
163  
164 #pod =sub copy_inline_refs
165 #pod
166 #pod my $subschema = copy_inline_refs( $schema, '/user' );
167 #pod
168 #pod Given:
169 #pod
170 #pod =over
171 #pod
172 #pod =item a "source" JSON schema (will not be mutated)
173 #pod
174 #pod =item a JSON Pointer into the source schema, from which to be copied
175 #pod
176 #pod =back
177 #pod
178 #pod will return another, copied standalone JSON schema, with any C<$ref>
179 #pod either copied in, or if previously encountered, with a C<$ref> to the
180 #pod new location.
181 #pod
182 #pod =cut
183  
184 21608 ▶sub copy_inline_refs {
Subroutine copy_inline_refs: called CC=12 SCAR=24.8
Pod documented
185 21608 my ( $schema, $pointer, $usschema, $uspointer, $refmap ) = @_;
186 21608 ▶ $usschema //= Mojo::JSON::Pointer->new( $schema )->get( $pointer );
Condition100%3/3
$usschema //= "Mojo::JSON::Pointer"->new($schema)->get($pointer)
l!l&&r!l&&!r
2108146364
MC/DC100%2/2
$usschema //= "Mojo::JSON::Pointer"->new($schema)->get($pointer)
$usschema "Mojo::JSON::Pointer"->new($schema)->get($pointer)
Truth table100%3/3
A //= B
A= $usschemaB= "Mojo::JSON::Pointer"->new($schema)->get($pointer)
ABresult
1X1
011
000
187 21608 ▶ $uspointer //= '';
Condition100%2/2
$uspointer //= ""
l!l
21141467
MC/DC100%1/1
$uspointer //= ""
$uspointer
Truth table100%2/2
A
A= $uspointer
Aresult
11
00
188 21608 ▶ $refmap ||= {};
Condition100%2/2
$refmap ||= {}
l!l
21141467
MC/DC100%1/1
$refmap ||= {}
$refmap
Truth table100%2/2
A
A= $refmap
Aresult
11
00
189 21608 ▶ return { '$ref' => $refmap->{ $uspointer } } if $refmap->{ $uspointer };
Branch: 1 branch
BranchTrueFalseTotal
if $refmap->{$uspointer} 2 21606 21608
190 $refmap->{ $pointer } = "#$uspointer"
191 21606 ▶ unless ref $usschema eq 'HASH' and $usschema->{'$ref'};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $usschema eq "HASH" and $usschema->{"\$ref"} 21592 14 21606
Condition100%3/3
ref $usschema eq "HASH" and $usschema->{"\$ref"}
!ll&&!rl&&r
17075451714
MC/DC100%2/2
ref $usschema eq "HASH" and $usschema->{"\$ref"}
ref $usschema eq "HASH" $usschema->{"\$ref"}
Truth table100%3/3
A and B
A= ref $usschema eq "HASH"B= $usschema->{"\$ref"}
ABresult
0X0
100
111
192 21606 ▶ return $usschema
Branch: 1 branch
BranchTrueFalseTotal
unless ref $usschema eq "ARRAY" or ref $usschema eq "HASH" 14993 6613 21606
Condition100%3/3
ref $usschema eq "ARRAY" or ref $usschema eq "HASH"
l!l&&r!l&&!r
2082453114993
MC/DC100%2/2
ref $usschema eq "ARRAY" or ref $usschema eq "HASH"
ref $usschema eq "ARRAY" ref $usschema eq "HASH"
Truth table100%3/3
A or B
A= ref $usschema eq "ARRAY"B= ref $usschema eq "HASH"
ABresult
1X1
011
000
193 unless ref $usschema eq 'ARRAY' or ref $usschema eq 'HASH';
194 6613 my $counter = 0;
195 6613 ▶ return [ map copy_inline_refs(
Branch: 1 branch
BranchTrueFalseTotal
if ref $usschema eq "ARRAY" 2082 4531 6613
196 $schema,
197 $pointer.'/'.$counter++,
198 $_,
199 $uspointer.'/'.$counter++,
200 $refmap,
201 ), @$usschema ] if ref $usschema eq 'ARRAY';
202 # HASH
203 4531 my $ref = $usschema->{'$ref'};
204 4531 ▶ return { map { $_ => copy_inline_refs(
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 4517 14 4531
205 $schema,
206 $pointer.'/'.$_,
207 16584 $usschema->{ $_ },
208 $uspointer.'/'.$_,
209 $refmap,
210 ) } sort keys %$usschema } if !$ref;
211 14 $ref =~ s:^#::;
212 14 ▶ return { '$ref' => $refmap->{ $ref } } if $refmap->{ $ref };
Branch: 1 branch
BranchTrueFalseTotal
if $refmap->{$ref} 10 4 14
213 4 copy_inline_refs(
214 $schema,
215 $ref,
216 Mojo::JSON::Pointer->new( $schema )->get( $ref ),
217 $uspointer,
218 $refmap,
219 );
220 }
221  
222 #pod =sub match
223 #pod
224 #pod my $bool = match( $where, $item );
225 #pod
226 #pod Test if the given C<$item> matches the given L<SQL::Abstract> C<$where>
227 #pod data structure. See L<SQL::Abstract/WHERE CLAUSES> for the full syntax.
228 #pod
229 #pod Not all of SQL::Abstract's syntax is supported yet, so patches are welcome.
230 #pod
231 #pod =cut
232  
233 611 ▶sub match {
Subroutine match: called CC=18 SCAR=29.4
Pod documented
234 611 my ( $match, $item ) = @_;
235 611 ▶ return undef if !defined $item;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item 3 608 611
236  
237 608 ▶ if ( ref $match eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $match eq "ARRAY") 3 605 608
238 3 ▶ return any { match( $_, $item ) } @$match;
Subroutine __ANON__: called CC=1 SCAR=0.0
239 }
240  
241 605 my %test;
242 605 for my $key ( keys %$match ) {
243 363 ▶ if ( $key =~ /^-(not_)?bool/ ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($key =~ /^-(not_)?bool/u) { } 17 346 363
elsif (not ref $match->{$key}) { } 237 109 346
elsif (ref $match->{$key} eq 'HASH') { } 105 4 109
elsif (ref $match->{$key} eq 'ARRAY') { } 4 0 4
244 17 my $want_false = $1;
245 17 $key = $match->{ $key }; # the actual field
246 17 ▶ $test{ $key } = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
247 17 my ( $value, $key ) = @_;
248 17 ▶ return $want_false ? !$value : !!$value;
Branch: 1 branch
BranchTrueFalseTotal
$want_false ? : 11 6 17
249 17 };
250 }
251 elsif ( !ref $match->{ $key } ) {
252 237 $test{ $key } = $match->{ $key };
253 }
254 elsif ( ref $match->{ $key } eq 'HASH' ) {
255 105 ▶ if ( my $value = $match->{ $key }{ -like } || $match->{ $key }{ like } ) {
Branch: 4 branches
BranchTrueFalseTotal
if (my $value = $match->{$key}{'-like'} || $match->{$key}{'like'}) { } 78 27 105
elsif ($value = $match->{$key}{'-has'}) { } 9 18 27
elsif ($value = $match->{$key}{'-not_has'}) { } 9 9 18
elsif (exists $match->{$key}{'!='}) { } 9 0 9
Condition100%3/3
$match->{$key}{'-like'} || $match->{$key}{'like'}
l!l&&r!l&&!r
542427
MC/DC100%2/2
$match->{$key}{'-like'} || $match->{$key}{'like'}
$match->{$key}{'-like'} $match->{$key}{'like'}
Truth table100%3/3
A || B
A= $match->{$key}{'-like'}B= $match->{$key}{'like'}
ABresult
1X1
011
000
256 78 $value = quotemeta $value;
257 78 $value =~ s/(?<!\\)\\%/.*/g;
258 78 $test{ $key } = qr{^$value$};
259 }
260 elsif ( $value = $match->{ $key }{ -has } ) {
261 9 my $expect = $value;
262 9 ▶ $test{ $key } = sub {
Subroutine __ANON__: called CC=7 SCAR=19.9
263 9 my ( $value, $key ) = @_;
264 9 ▶ return 0 if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 8 9
265 8 ▶ if ( ref $value eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 4 4 8
elsif (ref $value eq 'HASH') { } 4 0 4
266 4 ▶ if ( ref $expect eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $expect eq 'ARRAY') { } 2 2 4
elsif (not ref $expect) { } 2 0 2
267 2 ▶ return all { my $e = $_; any { $_ eq $e } @$value } @$expect;
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
268 }
269 elsif ( !ref $expect ) {
270 2 ▶ return any { $_ eq $expect } @$value;
Subroutine __ANON__: called CC=1 SCAR=0.0
271 }
272 }
273 elsif ( ref $value eq 'HASH' ) {
274 4 ▶ if ( ref $expect eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $expect eq 'HASH') { } 4 0 4
275 4 return match( $expect, $value );
276 }
277 else {
278 0 die 'Bad query in -has on hash value: ' . ref $expect;
279 }
280 }
281 else {
282 0 die '-has query does not work on non-ref fields';
283 }
284 9 };
285 }
286 elsif ( $value = $match->{ $key }{ -not_has } ) {
287 9 ▶ $test{ $key } = sub {
Subroutine __ANON__: called CC=7 SCAR=20.1
288 9 my $expect = $value;
289 9 my ( $value, $key ) = @_;
290 9 ▶ return 1 if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 8 9
291 8 ▶ if ( ref $value eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 4 4 8
elsif (ref $value eq 'HASH') { } 4 0 4
292 4 ▶ if ( ref $expect eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $expect eq 'ARRAY') { } 2 2 4
elsif (not ref $expect) { } 2 0 2
293 2 ▶ return all { my $e = $_; none { $_ eq $e } @$value } @$expect;
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
294 }
295 elsif ( !ref $expect ) {
296 2 ▶ return none { $_ eq $expect } @$value;
Subroutine __ANON__: called CC=1 SCAR=0.0
297 }
298 else {
299 0 die 'Bad query in -has on array value: ' . ref $expect;
300 }
301 }
302 elsif ( ref $value eq 'HASH' ) {
303 4 ▶ if ( ref $expect eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $expect eq 'HASH') { } 4 0 4
304 4 return !match( $expect, $value );
305 }
306 else {
307 0 die 'Bad query in -has on hash value: ' . ref $expect;
308 }
309 }
310 else {
311 0 die '-has query does not work on non-ref fields';
312 }
313 9 };
314 }
315 elsif ( exists $match->{ $key }{ '!=' } ) {
316 9 my $expect = $match->{ $key }{ '!=' };
317 9 ▶ $test{ $key } = sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
318 9 my ( $got, $key ) = @_;
319 9 ▶ if ( !defined $expect || !defined $got) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $expect and defined $got) 9 0 9
Condition66%2/3
defined $expect and defined $got
!ll&&!rl&&r
810
MC/DC0%0/2
defined $expect and defined $got
defined $expect defined $got
Truth table66%2/3
A and B
A= defined $expectB= defined $got
ABresult
0X0
100
111
320 9 return defined $got != defined $expect;
321 }
322 0 return $got ne $expect;
323 9 };
324 }
325 else {
326 0 die "Unimplemented query type: " . to_json( $match->{ $key } );
327 }
328 }
329 elsif ( ref $match->{ $key } eq 'ARRAY' ) {
330 4 my @tests = @{ $match->{ $key } };
331 # Array is an 'OR' combiner
332 4 ▶ $test{ $key } = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
333 4 my ( $value, $key ) = @_;
334 4 my $sub_item = { $key => $value };
335 4 ▶ return any { match( { $key => $_ }, $sub_item ) } @tests;
Subroutine __ANON__: called CC=1 SCAR=0.0
336 4 };
337 }
338 else {
339 0 die "Unimplemented match ref type: " . to_json( $match->{ $key } );
340 }
341 }
342  
343 my $passes
344 = grep {
345 605 !defined $test{ $_ } ? !defined $item->{ $_ }
346 : ref $test{ $_ } eq 'Regexp' ? $item->{ $_ } =~ $test{ $_ }
347 : ref $test{ $_ } eq 'CODE' ? $test{ $_ }->( $item->{ $_ }, $_ )
348 363 ▶ : ($item->{ $_ }//'') eq ($test{ $_ }//'')
Branch: 3 branches
BranchTrueFalseTotal
!defined($test{$_}) ? : 2 361 363
ref $test{$_} eq 'Regexp' ? : 78 283 361
ref $test{$_} eq 'CODE' ? : 48 235 283
Condition75%3/4
$item->{$_} // ""
l!l
2296
$test{$_} // ""
l!l
2350
MC/DC100%1/1
$item->{$_} // ""
$item->{$_}
MC/DC0%0/1
$test{$_} // ""
$test{$_}
Truth table100%2/2
A
A= $item->{$_}
Aresult
11
00
Truth table50%1/2
A
A= $test{$_}
Aresult
11
00
349 }
350 keys %test;
351  
352 605 return $passes == keys %test;
353 }
354  
355 #pod =sub order_by
356 #pod
357 #pod my $ordered_array = order_by( $order_by, $unordered_array );
358 #pod
359 #pod Order the given arrayref by the given L<SQL::Abstract> order-by clause.
360 #pod
361 #pod =cut
362  
363 340 ▶sub order_by {
Subroutine order_by: called CC=11 SCAR=24.2
Pod documented
364 340 my ( $order_by, $unordered ) = @_;
365 # Array of [ (-asc/-desc), (field) ]
366 340 my @sort_items;
367  
368 340 ▶ if ( ref $order_by eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $order_by eq 'ARRAY') { } 310 30 340
elsif (ref $order_by eq 'HASH') { } 21 9 30
369 310 ▶ @sort_items = map { [ ref $_ ? %$_ : ( -asc => $_ ) ] } @$order_by;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 8 313 321
370 }
371 elsif ( ref $order_by eq 'HASH' ) {
372 21 @sort_items = [ %$order_by ];
373 }
374 else {
375 9 @sort_items = [ -asc => $order_by ];
376 }
377  
378 my @ordered = sort {
379 340 for my $item ( @sort_items ) {
380 my $cmp = $item->[0] eq '-asc'
381 ? ($a->{ $item->[1] }//'') cmp ($b->{ $item->[1] }//'')
382 211 ▶ : ($b->{ $item->[1] }//'') cmp ($a->{ $item->[1] }//'')
Branch: 1 branch
BranchTrueFalseTotal
$item->[0] eq '-asc' ? : 188 23 211
Condition50%4/8
$$a{$item->[1]} // ""
l!l
1880
$$b{$item->[1]} // ""
l!l
1880
$$b{$item->[1]} // ""
l!l
230
$$a{$item->[1]} // ""
l!l
230
MC/DC0%0/1
$$a{$item->[1]} // ""
$$a{$item->[1]}
MC/DC0%0/1
$$b{$item->[1]} // ""
$$b{$item->[1]}
MC/DC0%0/1
$$b{$item->[1]} // ""
$$b{$item->[1]}
MC/DC0%0/1
$$a{$item->[1]} // ""
$$a{$item->[1]}
Truth table50%1/2
A
A= $$a{$item->[1]}
Aresult
11
00
Truth table50%1/2
A
A= $$b{$item->[1]}
Aresult
11
00
Truth table50%1/2
A
A= $$b{$item->[1]}
Aresult
11
00
Truth table50%1/2
A
A= $$a{$item->[1]}
Aresult
11
00
383 ;
384 211 ▶ return $cmp || next;
Condition100%2/2
$cmp || (next)
l!l
2083
MC/DC100%1/1
$cmp || (next)
$cmp
Truth table100%2/2
A
A= $cmp
Aresult
11
00
385 }
386 }
387 @$unordered;
388  
389 340 return \@ordered;
390 }
391  
392 #pod =sub fill_brackets
393 #pod
394 #pod my $string = fill_brackets( $template, $item );
395 #pod
396 #pod This routine will fill in the given template string with the values from
397 #pod the given C<$item> hashref. The template contains field names within curly braces.
398 #pod Values in the C<$item> hashref will be escaped with L<Mojo::Util/xml_escape>.
399 #pod
400 #pod my $item = {
401 #pod name => 'Doug Bell',
402 #pod email => 'doug@example.com',
403 #pod quote => 'I <3 Perl',
404 #pod };
405 #pod
406 #pod # Doug Bell <doug@example.com>
407 #pod fill_brackets( '{name} <{email}>', $item );
408 #pod
409 #pod # I &lt;3 Perl
410 #pod fill_brackets( '{quote}', $item );
411 #pod
412 #pod =cut
413  
414 4 ▶sub fill_brackets {
Subroutine fill_brackets: called CC=1 SCAR=0.0
Pod documented
415 4 my ( $template, $item ) = @_;
416 4 return scalar $template =~ s/(?<!\\)\{\s*([^\s\}]+)\s*\}/xml_escape $item->{$1}/reg;
417 }
418  
419 #pod =sub is_type
420 #pod
421 #pod my $bool = is_type( $schema->{properties}{myprop}{type}, 'boolean' );
422 #pod
423 #pod Returns true if the given JSON schema type value (which can be a string or
424 #pod an array of strings) contains the given value, allowing the given type for
425 #pod the property.
426 #pod
427 #pod # true
428 #pod is_type( 'boolean', 'boolean' );
429 #pod is_type( [qw( boolean null )], 'boolean' );
430 #pod # false
431 #pod is_type( 'string', 'boolean' );
432 #pod is_type( [qw( string null )], 'boolean' );
433 #pod
434 #pod =cut
435  
436 2536 ▶sub is_type {
Subroutine is_type: called CC=3 SCAR=11.0
Pod documented
437 2536 my ( $type, $is_type ) = @_;
438 2536 ▶ return unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 4 2532 2536
439 return ref $type eq 'ARRAY'
440 2532 ▶ ? !!grep { $_ eq $is_type } @$type
Branch: 1 branch
BranchTrueFalseTotal
ref $type eq 'ARRAY' ? : 593 1939 2532
441 : $type eq $is_type;
442 }
443  
444 #pod =sub is_format
445 #pod
446 #pod my $bool = is_format( $schema->{properties}{myprop}{format}, 'date-time' );
447 #pod
448 #pod Returns true if the given JSON schema format value (a string) is the given value.
449 #pod
450 #pod # true
451 #pod is_format( 'date-time', 'date-time' );
452 #pod # false
453 #pod is_format( 'email', 'date-time' );
454 #pod
455 #pod =cut
456  
457 660 ▶sub is_format {
Subroutine is_format: called CC=2 SCAR=6.9
Pod documented
458 660 my ( $format, $is_format ) = @_;
459 660 ▶ return unless $format;
Branch: 1 branch
BranchTrueFalseTotal
unless $format 405 255 660
460 255 return $format eq $is_format;
461 }
462  
463 #pod =sub derp
464 #pod
465 #pod derp "This feature is deprecated in file '%s'", $file;
466 #pod
467 #pod Print out a deprecation message as a warning. A message will only be
468 #pod printed once for each set of arguments from each caller.
469 #pod
470 #pod =cut
471  
472 43 our @CARP_NOT = qw(
473 Yancy::Controller::Yancy Yancy::Controller::Yancy::MultiTenant
474 Mojolicious::Plugin::Yancy Mojolicious::Plugins Mojolicious
475 Mojo::Server Yancy::Plugin::Editor Yancy::Plugin::Auth
476 Mojolicious::Renderer Yancy::Plugin::Auth::Token
477 Yancy::Plugin::Auth::Password
478 );
479 43 our %DERPED;
480 236 ▶sub derp(@) {
Subroutine derp: called CC=3 SCAR=11.0
Pod documented
481 236 my @args = @_;
482 236 my $key = to_json [ caller, @args ];
483 236 ▶ return if $DERPED{ $key };
Branch: 1 branch
BranchTrueFalseTotal
if $Yancy::Util::DERPED{$key} 218 18 236
484 18 ▶ if ( $args[0] !~ /\.$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args[0] =~ /\.$/u) 17 1 18
485 17 $args[0] .= '.';
486 }
487 18 carp sprintf( $args[0], @args[1..$#args] );
488 18 $DERPED{ $key } = 1;
489 }
490  
491 #pod =sub json_validator
492 #pod
493 #pod my $json_validator = json_validator( $schema );
494 #pod
495 #pod Build a L<JSON::Validator> object for the given schema, adding all the
496 #pod necessary attributes.
497 #pod
498 #pod =cut
499  
500 127 ▶sub json_validator {
Subroutine json_validator: called CC=1 SCAR=0.0
Pod documented
501 127 my ( $schema ) = @_;
502 127 my $v = JSON::Validator->new( coerce => 'bool,def,num,str' );
503 127 my $formats = $v->formats;
504 127 ▶ $formats->{ password } = sub { undef };
Subroutine __ANON__: called CC=1 SCAR=0.0
505 127 ▶ $formats->{ filepath } = sub { undef };
Subroutine __ANON__: called CC=1 SCAR=0.0
506 0 ▶ $formats->{ markdown } = sub { undef };
Subroutine __ANON__: not called CC=1 SCAR=1.2
507 0 ▶ $formats->{ tel } = sub { undef };
Subroutine __ANON__: not called CC=1 SCAR=1.2
508 127 ▶ $formats->{ textarea } = sub { undef };
Subroutine __ANON__: called CC=1 SCAR=0.0
509 127 return $v;
510 }
511  
512 43 1;
513  
514 __END__
« Yancy/Plugin/File.pm ↑ Summary Yancy/Plugin/Auth/Token.pm »