Articulate/Role/Sortation/AllYouNeedIsCmp.pm

filter: Statementstmt 93.3% 28 / 30 Branchbran 75.0% 6 / 8 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 7 / 7 Podpod 100% 3 / 3 totaltotal 88.2% 45 / 51 SCAR 18.0
CC
6
Cov
88%
order
3.0
schwartz
2.0
sort
2.0
CRAP 6.1
« Articulate/Serialisation/StatusSetter.pm ↑ Summary Articulate/Service/List.pm »
1 package Articulate::Role::Sortation::AllYouNeedIsCmp;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Articulate::Role::Sortation::AllYouNeedIsCmp - provides sortation methods derived from you cmp method
9  
10 =head1 SYNOPSIS
11  
12 package My::Sortation::Class;
13 use Moo;
14 with 'Articulate::Role::Sortation::AllYouNeedIsCmp';
15 sub cmp { ... }
16 sub decorate { ... } # optional
17 1;
18  
19 Provides the C<sort> and C<schwartz> funtions. These call on the C<cmp> method (which you are exected to write) and, optionally, the C<decorate> method (if it exists).
20  
21 =cut
22  
23 =head1 METHODS
24  
25 =head3 order
26  
27 Convenience method which inspects the C<order> value of the C<options> hash and returns it (or returns C<asc>) if undefined).
28  
29 =cut
30  
31 sub order {
32 3 ▶ my $self = shift;
Subroutine order: called CC=3 SCAR=11.1
Pod documented
33 3 ▶ return ( $self->options->{order} // 'asc' ) if $self->can('options');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("options") 2 1 3
Condition50%1/2
$self->options->{"order"} // "asc"
l!l
20
MC/DC0%0/1
$self->options->{"order"} // "asc"
$self->options->{"order"}
Truth table50%1/2
A
A= $self->options->{"order"}
Aresult
11
00
34 1 return 'asc';
35 }
36  
37 =head3 sort
38  
39 $sorter->sort( [items] )
40  
41 Performs a simple sort, using C<cmp> and C<decorate>.
42  
43 =cut
44  
45  
46 sub sort {
47 3 ▶ my $self = shift;
Subroutine sort: called CC=2 SCAR=7.0
Pod documented
48 3 my $items = shift;
49 my $dec = sub {
50 48 ▶ my $orig = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
51 48 ▶ $self->can('decorate')
Branch: 1 branch
BranchTrueFalseTotal
$self->can('decorate') ? : 10 38 48
52 ? $self->decorate($orig)
53 : $orig
54 3 };
55 3 ▶ return [ sort { $self->cmp( $dec->($b), $dec->($a) ) } @$items ] if 'desc' eq $self->order;
Branch: 1 branch
BranchTrueFalseTotal
if "desc" eq $self->order 0 3 3
56 3 return [ sort { $self->cmp( $dec->($a), $dec->($b) ) } @$items ];
57 }
58  
59 =head3 schwartz
60  
61 $sorter->schwartz( [items] );
62  
63 Performs a schwartxian transform using C<decorate>, and sorts the decorated items using C<cmp>, then returns the originals in the sorted order.
64  
65 =cut
66  
67  
68 sub schwartz {
69 2 ▶ my $self = shift;
Subroutine schwartz: called CC=2 SCAR=7.1
Pod documented
70 2 my $items = shift;
71 2 ▶ if ( $self->can('decorate') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can('decorate')) { } 2 0 2
72 return [
73 6 map { $_->[0] }
74 5 sort { $self->cmp( $a->[1], $b->[1] ) }
75 2 map { [$_, $self->decorate($_)] }
76 @$items
77 ];
78 }
79 else {
80 0 return $self->sort($items);
81 }
82 }
83  
84 3 1;
« Articulate/Serialisation/StatusSetter.pm ↑ Summary Articulate/Service/List.pm »