File Coverage

lib/Mojolicious/Plugin/Vparam/Vsort.pm
Criterion Covered Total %
statement 20 20 100.0
branch 13 14 92.8
condition 5 6 83.3
subroutine 4 4 100.0
pod 0 1 0.0
total 42 45 93.3


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::Vparam::Vsort;
2 72     72   4077350 use Mojo::Base -strict;
  72         8268  
  72         455  
3 72     72   6767 use Mojolicious::Plugin::Vparam::Common;
  72         155  
  72         21482  
4              
5             sub register {
6 74     74 0 214 my ($class, $self, $app, $conf) = @_;
7              
8             # Same as vparams but add standart table sort parameters for:
9             # ORDER BY, LIMIT, OFFSET
10             $app->helper(vsort => sub{
11 12     12   29320 my ($self, %attr) = @_;
12              
13 12         26 my $sort = delete $attr{'-sort'};
14 12 100 100     67 die 'Key "-sort" must be ArrayRef'
15             if defined($sort) and 'ARRAY' ne ref $sort;
16              
17             $attr{ $conf->{vsort_page} } = {
18             type => 'int',
19             default => 1,
20 11 100       58 } if defined $conf->{vsort_page};
21              
22             $attr{ $conf->{vsort_rws} } = {
23             type => 'int',
24             default => $conf->{rows},
25 11 100       45 } if defined $conf->{vsort_rws};
26              
27             $attr{ $conf->{vsort_oby} } = {
28             type => 'int',
29             default => 0,
30 10 50 66     335 post => sub { $sort->[ $_[1] ] or ($_[1] + 1) or 1 },
31 11 100       64 } if defined $conf->{vsort_oby};
32              
33             $attr{ $conf->{vsort_ods} } = {
34             type => 'str',
35             default => $conf->{ods},
36 10         31 post => sub { uc $_[1] },
37             regexp => qr{^(?:asc|desc)$}i,
38 11 100       77 } if defined $conf->{vsort_ods};
39              
40 11         64 my $result = $self->vparams( %attr );
41 11 100       118 return wantarray ? %$result : $result;
42 74         561 });
43              
44 74         1730 return;
45             }
46              
47             1;