| 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 | 3 | ▶ | sub order { | ||||||||||||||||||
|
Subroutine
order: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||
| 32 | 3 | my $self = shift; | |||||||||||||||||||
| 33 | 3 | ▶ | return ( $self->options->{order} // 'asc' ) if $self->can('options'); | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->options->{"order"} // "asc"
MC/DC0%0/1
$self->options->{"order"} // "asc"
$self->options->{"order"}
Truth table50%1/2
A
A= $self->options->{"order"}
| |||||||||||||||||||||
| 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 | 3 | ▶ | sub sort { | ||||||||||||||||||
|
Subroutine
sort: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 46 | 3 | my $self = shift; | |||||||||||||||||||
| 47 | 3 | my $items = shift; | |||||||||||||||||||
| 48 | 48 | ▶ | my $dec = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 49 | 48 | my $orig = shift; | |||||||||||||||||||
| 50 | 48 | ▶ | $self->can('decorate') | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | ? $self->decorate($orig) | ||||||||||||||||||||
| 52 | : $orig; | ||||||||||||||||||||
| 53 | 3 | }; | |||||||||||||||||||
| 54 | 0 | ▶ | return [ sort { $self->cmp( $dec->($b), $dec->($a) ) } @$items ] | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | if 'desc' eq $self->order; | ||||||||||||||||||||
| 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 | 2 | ▶ | sub schwartz { | ||||||||||||||||||
|
Subroutine
schwartz: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||
| 68 | 2 | my $self = shift; | |||||||||||||||||||
| 69 | 2 | my $items = shift; | |||||||||||||||||||
| 70 | 2 | ▶ | if ( $self->can('decorate') ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | return [ | ||||||||||||||||||||
| 72 | 6 | map { $_->[0] } | |||||||||||||||||||
| 73 | 5 | sort { $self->cmp( $a->[1], $b->[1] ) } | |||||||||||||||||||
| 74 | 2 | map { [ $_, $self->decorate($_) ] } @$items | |||||||||||||||||||
| 75 | ]; | ||||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | else { | ||||||||||||||||||||
| 78 | 0 | return $self->sort($items); | |||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 3 | 1; | |||||||||||||||||||