| 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
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 | |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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; | |||||||||||||||||||