| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Articulate::Role::Sortation::AllYouNeedIsCmp; | 
| 2 | 3 |  |  | 3 |  | 9366 | use strict; | 
|  | 3 |  |  |  |  | 5 |  | 
|  | 3 |  |  |  |  | 95 |  | 
| 3 | 3 |  |  | 3 |  | 11 | use warnings; | 
|  | 3 |  |  |  |  | 10 |  | 
|  | 3 |  |  |  |  | 62 |  | 
| 4 | 3 |  |  | 3 |  | 12 | use Moo::Role; | 
|  | 3 |  |  |  |  | 4 |  | 
|  | 3 |  |  |  |  | 18 |  | 
| 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 and C funtions. These call on the C method (which you are exected to write) and, optionally, the C method (if it exists). | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | =cut | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | =head1 METHODS | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | =head3 order | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | Convenience method which inspects the C value of the C hash and returns it (or returns C) if undefined). | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | =cut | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | sub order { | 
| 32 | 3 |  |  | 3 | 1 | 6 | my $self = shift; | 
| 33 | 3 | 100 | 50 |  |  | 34 | return ( $self->options->{order} // 'asc' ) if $self->can('options'); | 
| 34 | 1 |  |  |  |  | 6 | return 'asc'; | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | =head3 sort | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | $sorter->sort( [items] ) | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | Performs a simple sort, using C and C. | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | =cut | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | sub sort { | 
| 46 | 3 |  |  | 3 | 1 | 106 | my $self  = shift; | 
| 47 | 3 |  |  |  |  | 6 | my $items = shift; | 
| 48 |  |  |  |  |  |  | my $dec   = sub { | 
| 49 | 48 |  |  | 48 |  | 51 | my $orig = shift; | 
| 50 | 48 | 100 |  |  |  | 208 | $self->can('decorate') | 
| 51 |  |  |  |  |  |  | ? $self->decorate($orig) | 
| 52 |  |  |  |  |  |  | : $orig; | 
| 53 | 3 |  |  |  |  | 15 | }; | 
| 54 | 3 | 50 |  |  |  | 12 | return [ sort { $self->cmp( $dec->($b), $dec->($a) ) } @$items ] | 
|  | 0 |  |  |  |  | 0 |  | 
| 55 |  |  |  |  |  |  | if 'desc' eq $self->order; | 
| 56 | 3 |  |  |  |  | 696 | return [ sort { $self->cmp( $dec->($a), $dec->($b) ) } @$items ]; | 
|  | 24 |  |  |  |  | 128 |  | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | =head3 schwartz | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | $sorter->schwartz( [items] ); | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | Performs a schwartxian transform using C, and sorts the decorated items using C, then returns the originals in the sorted order. | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | =cut | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | sub schwartz { | 
| 68 | 2 |  |  | 2 | 1 | 42 | my $self  = shift; | 
| 69 | 2 |  |  |  |  | 3 | my $items = shift; | 
| 70 | 2 | 50 |  |  |  | 8 | if ( $self->can('decorate') ) { | 
| 71 |  |  |  |  |  |  | return [ | 
| 72 | 6 |  |  |  |  | 20 | map { $_->[0] } | 
|  | 5 |  |  |  |  | 12 |  | 
| 73 | 6 |  |  |  |  | 15 | sort { $self->cmp( $a->[1], $b->[1] ) } | 
| 74 | 2 |  |  |  |  | 3 | map { [ $_, $self->decorate($_) ] } @$items | 
| 75 |  |  |  |  |  |  | ]; | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  | else { | 
| 78 | 0 |  |  |  |  |  | return $self->sort($items); | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | 1; |