|
Revision 19768, 1.1 kB
(checked in by lopnor, 5 years ago)
|
|
lang/perl/App-Hachero: new result class, tt output plugin
|
| Line | |
|---|
| 1 | package App::Hachero::Result; |
|---|
| 2 | use strict; |
|---|
| 3 | use warnings; |
|---|
| 4 | use base qw(Class::Accessor::Fast Class::Data::Inheritable); |
|---|
| 5 | use Digest::MD5 qw(md5_hex); |
|---|
| 6 | __PACKAGE__->mk_classdata('primary'); |
|---|
| 7 | __PACKAGE__->mk_accessors(qw(data arrayref)); |
|---|
| 8 | |
|---|
| 9 | sub new { |
|---|
| 10 | my $class = shift; |
|---|
| 11 | my $self = $class->SUPER::new(@_); |
|---|
| 12 | $self->data({}); |
|---|
| 13 | bless $self, $class; |
|---|
| 14 | } |
|---|
| 15 | |
|---|
| 16 | sub push { |
|---|
| 17 | my ($self, $args) = @_; |
|---|
| 18 | my $key = $self->key($args); |
|---|
| 19 | $self->data->{$key} ||= $args; |
|---|
| 20 | $self->data->{$key}->{count}++; |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | sub values { |
|---|
| 24 | my $self = shift; |
|---|
| 25 | $self->sort; |
|---|
| 26 | return @{$self->arrayref}; |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | sub sort { |
|---|
| 30 | my $self = shift; |
|---|
| 31 | $self->arrayref([]); |
|---|
| 32 | my $package = ref $self; |
|---|
| 33 | no strict 'refs'; |
|---|
| 34 | my $cmp = *{"$package\::cmp"}; |
|---|
| 35 | for my $value (sort $cmp CORE::values %{$self->data}) { |
|---|
| 36 | CORE::push @{$self->arrayref}, $value; |
|---|
| 37 | } |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | sub cmp { |
|---|
| 41 | for (@{__PACKAGE__->primary}) { |
|---|
| 42 | if (my $res = $a->{$_} cmp $b->{$_}) { |
|---|
| 43 | return $res; |
|---|
| 44 | } |
|---|
| 45 | } |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | sub key { |
|---|
| 49 | my ($self, $args) = @_; |
|---|
| 50 | md5_hex (map {$args->{$_}} @{$self->primary}); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | 1; |
|---|