File Coverage

blib/lib/App/RecordStream/Operation/sort.pm
Criterion Covered Total %
statement 26 30 86.6
branch 2 2 100.0
condition n/a
subroutine 7 8 87.5
pod 0 4 0.0
total 35 44 79.5


line stmt bran cond sub pod time code
1             package App::RecordStream::Operation::sort;
2              
3             our $VERSION = "4.0.24";
4              
5 3     3   885 use strict;
  3         7  
  3         84  
6 3     3   15 use warnings;
  3         6  
  3         80  
7              
8 3     3   15 use base qw(App::RecordStream::Accumulator App::RecordStream::Operation);
  3         7  
  3         1174  
9              
10             sub init {
11 3     3 0 7 my $this = shift;
12 3         5 my $args = shift;
13              
14 3         9 my @keys;
15             my $reverse;
16              
17             my $spec = {
18 3     3   1721 "key|k=s" => sub { push @keys, split(/,/, $_[1]); },
19 3         24 "reverse|r" => \$reverse,
20             };
21              
22 3         22 $this->parse_options($args, $spec);
23              
24 3         11 $this->{'KEYS'} = \@keys;
25 3         35 $this->{'REVERSE'} = $reverse;
26             }
27              
28             sub stream_done {
29 3     3 0 5 my $this = shift;
30              
31 3         11 my @records = App::RecordStream::Record::sort($this->get_records(), @{$this->{'KEYS'}});
  3         16  
32              
33 3 100       15 if ( $this->{'REVERSE'} ) {
34 1         4 @records = reverse @records;
35             }
36              
37 3         8 foreach my $record (@records) {
38 15         46 $this->push_record($record);
39             }
40             }
41              
42             sub add_help_types {
43 3     3 0 8 my $this = shift;
44 3         13 $this->use_help_type('keyspecs');
45             }
46              
47             sub usage {
48 0     0 0   my $this = shift;
49              
50 0           my $options = [
51             ['key ', "May be comma separated, May be specified multiple times. Each keyspec is a name or a name=sortType. The name should be a field name to sort on. The sort type should be either lexical or numeric. Default sort type is lexical (can also use nat, lex, n, and l). Additionallly, the sort type may be prefixed with '-' to indicate a decreasing sort order. Additionally, the sort type may be postfixed with '*' to sort the special value 'ALL' to the end (useful for the output of recs-collate --cube). See perldoc for App::RecordStream::Record for more on sort specs. May be a key spec, see '--help-keyspecs' for more. Cannot be a keygroup."],
52             ['reverse', 'Reverses the sort order'],
53             ];
54              
55 0           my $args_string = $this->options_string($options);
56              
57 0           return <
58             Usage: recs-sort []
59             __FORMAT_TEXT__
60             Sorts records from input or from . You may sort on a list of keys,
61             each key sorted lexically (alpha order) or numerically
62             __FORMAT_TEXT__
63              
64             $args_string
65              
66             Examples:
67             Sort on the id field, a numeric
68             recs-sort --key id=numeric
69             Sort on age, then name
70             recs-sort --key age=numeric,name
71             Sort on decreasing size, name
72             recs-sort --key size=-numeric --key name
73             USAGE
74             }
75              
76             1;