root/lang/perl/App-Hachero/trunk/lib/App/Hachero/Result.pm @ 19768

Revision 19768, 1.1 kB (checked in by lopnor, 5 years ago)

lang/perl/App-Hachero: new result class, tt output plugin

Line 
1package App::Hachero::Result;
2use strict;
3use warnings;
4use base qw(Class::Accessor::Fast Class::Data::Inheritable);
5use Digest::MD5 qw(md5_hex);
6__PACKAGE__->mk_classdata('primary');
7__PACKAGE__->mk_accessors(qw(data arrayref));
8
9sub new {
10    my $class = shift;
11    my $self = $class->SUPER::new(@_);
12    $self->data({});
13    bless $self, $class;
14}
15
16sub push {
17    my ($self, $args) = @_;
18    my $key = $self->key($args);
19    $self->data->{$key} ||= $args;
20    $self->data->{$key}->{count}++;
21}
22
23sub values {
24    my $self = shift;
25    $self->sort;
26    return @{$self->arrayref};
27}
28
29sub 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
40sub cmp {
41    for (@{__PACKAGE__->primary}) {
42        if (my $res = $a->{$_} cmp $b->{$_}) {
43            return $res;
44        }
45    }
46}
47
48sub key {
49    my ($self, $args) = @_;
50    md5_hex (map {$args->{$_}} @{$self->primary});
51}
52
531;
Note: See TracBrowser for help on using the browser.