Changeset 2341


Ignore:
Timestamp:
04/08/20 19:34:50 (4 years ago)
Author:
nanardon
Message:

Add sort* functions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Cli/Object.pm

    r2288 r2341  
    9292            my %ids = map { $_ => 1 } @ids; 
    9393            grep { !$ids{$_} } map { $_->id } @{$env->{_objects}}; 
     94        }, 
     95        } 
     96    ); 
     97    $self->add_func('sort', { 
     98        help => 'Sort the selection according attribute', 
     99        code => sub { 
     100            my ($env, $attribute) = @_; 
     101 
     102            if ($attribute) { 
     103                @{$env->{_objects}} = 
     104                    sort { ($a->get_attributes($attribute) || '') cmp ($b->get_attributes($attribute) || '') } 
     105                    @{$env->{_objects}}; 
     106            } else { 
     107                @{$env->{_objects}} = 
     108                    sort { $a->id cmp $b->id } 
     109                    @{$env->{_objects}}; 
     110            } 
     111 
     112            printf $OUT "select is now %s: %s\n", $env->{_otype}, join(', ', map { 
     113                $_->id } @{$env->{_objects}}); 
     114        }, 
     115        completion => sub { 
     116            if (!$_[2]) { 
     117                my $flag = $_[1] =~ /^_/ ? 'ra' : 'r'; 
     118                return $_[0]->base->list_canonical_fields($_[0]->{_otype}, $flag) 
     119            } 
     120        }, 
     121        } 
     122    ); 
     123    $self->add_func('sortr', { 
     124        help => 'Sort the selection (reverse) according attribute', 
     125        code => sub { 
     126            my ($env, $attribute) = @_; 
     127 
     128            if ($attribute) { 
     129                @{$env->{_objects}} = 
     130                    sort { ($b->get_attributes($attribute) || '') cmp ($a->get_attributes($attribute) || '') } 
     131                    @{$env->{_objects}}; 
     132            } else { 
     133                @{$env->{_objects}} = 
     134                    sort { $b->id cmp $a->id } 
     135                    @{$env->{_objects}}; 
     136            } 
     137 
     138            printf $OUT "select is now %s: %s\n", $env->{_otype}, join(', ', map { 
     139                $_->id } @{$env->{_objects}}); 
     140        }, 
     141        completion => sub { 
     142            if (!$_[2]) { 
     143                my $flag = $_[1] =~ /^_/ ? 'ra' : 'r'; 
     144                return $_[0]->base->list_canonical_fields($_[0]->{_otype}, $flag) 
     145            } 
     146        }, 
     147        } 
     148    ); 
     149    $self->add_func('sortn', { 
     150        help => 'Sort numeric the selection according attribute', 
     151        code => sub { 
     152            my ($env, $attribute) = @_; 
     153 
     154            if ($attribute) { 
     155                @{$env->{_objects}} = 
     156                    sort { ($a->get_attributes($attribute) || 0) <=> ($b->get_attributes($attribute) || 0) } 
     157                    @{$env->{_objects}}; 
     158            } else { 
     159                @{$env->{_objects}} = 
     160                    sort { $a->_get_ikey <=> $b->_get_ikey } 
     161                    @{$env->{_objects}}; 
     162            } 
     163 
     164            printf $OUT "select is now %s: %s\n", $env->{_otype}, join(', ', map { 
     165                $_->id } @{$env->{_objects}}); 
     166        }, 
     167        completion => sub { 
     168            if (!$_[2]) { 
     169                my $flag = $_[1] =~ /^_/ ? 'ra' : 'r'; 
     170                return $_[0]->base->list_canonical_fields($_[0]->{_otype}, $flag) 
     171            } 
     172        }, 
     173        } 
     174    ); 
     175    $self->add_func('sortnr', { 
     176        help => 'Sort numeric reverse the selection according attribute', 
     177        code => sub { 
     178            my ($env, $attribute) = @_; 
     179 
     180            if ($attribute) { 
     181                @{$env->{_objects}} = 
     182                    sort { ($b->get_attributes($attribute) || 0) <=> ($a->get_attributes($attribute) || 0) } 
     183                    @{$env->{_objects}}; 
     184            } else { 
     185                @{$env->{_objects}} = 
     186                    sort { $b->_get_ikey <=> $a->_get_ikey } 
     187                    @{$env->{_objects}}; 
     188            } 
     189 
     190            printf $OUT "select is now %s: %s\n", $env->{_otype}, join(', ', map { 
     191                $_->id } @{$env->{_objects}}); 
     192        }, 
     193        completion => sub { 
     194            if (!$_[2]) { 
     195                my $flag = $_[1] =~ /^_/ ? 'ra' : 'r'; 
     196                return $_[0]->base->list_canonical_fields($_[0]->{_otype}, $flag) 
     197            } 
    94198        }, 
    95199        } 
     
    387491 
    388492                if (! @objects) { 
    389                     @objects = grep { $_ }  
     493                    @objects = grep { $_ } 
    390494                      map { $_->get_attributes($attrname) } @{$env->{_objects}}; 
    391495                } 
     
    660764        }); 
    661765 
    662          
    663  
    664766    } elsif ($self->otype eq 'group') { 
    665767 
Note: See TracChangeset for help on using the changeset viewer.