| 1 | # $Id: /local/perl/HTML-TagClouder/trunk/lib/HTML/TagClouder/Collection/Simple.pm 11407 2007-05-23T11:58:17.952570Z daisuke $ | ||
| 2 | # | ||
| 3 | # Copyright (c) 2007 Daisuke Maki <daisuke@endeworks.jp> | ||
| 4 | |||
| 5 | package HTML::TagClouder::Collection::Simple; | ||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use base qw(HTML::TagClouder::Collection); | ||
| 9 | |||
| 10 | __PACKAGE__->mk_accessors($_) for qw(tags); | ||
| 11 | |||
| 12 | sub new | ||
| 13 | { | ||
| 14 | my $class = shift; | ||
| 15 | my $self = $class->next::method(@_, iterator_class => 'HTML::TagClouder::Collection::Simple::Iterator'); | ||
| 16 | $self->tags([]); | ||
| 17 | return $self; | ||
| 18 | } | ||
| 19 | |||
| 20 | sub add | ||
| 21 | { | ||
| 22 | my $self = shift; | ||
| 23 | my $tag = shift; | ||
| 24 | push @{ $self->tags }, $tag unless $tag->count == 0; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub sort | ||
| 28 | { | ||
| 29 | my $self = shift; | ||
| 30 | my $strategy = shift; | ||
| 31 | |||
| 32 | $strategy ||= ''; | ||
| 33 | |||
| 34 | if ($strategy eq 'count') { | ||
| 35 | $self->tags([ sort { ($a->count || 0) <=> ($b->count || 0) } @{ $self->tags } ]); | ||
| 36 | } else { | ||
| 37 | $self->tags([return sort { ($a->label || '') cmp ($b->label || '') } @{ $self->tags } ]); | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | package HTML::TagClouder::Collection::Simple::Iterator; | ||
| 42 | use strict; | ||
| 43 | use warnings; | ||
| 44 | use base qw(HTML::TagClouder::Iterator); | ||
| 45 | |||
| 46 | sub new | ||
| 47 | { | ||
| 48 | my $class = shift; | ||
| 49 | my $collection = shift; | ||
| 50 | |||
| 51 | my $self = bless { | ||
| 52 | collection => $collection, | ||
| 53 | current => 0, | ||
| 54 | }, $class; | ||
| 55 | |||
| 56 | return $self; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub next | ||
| 60 | { | ||
| 61 | my $self = shift; | ||
| 62 | return $self->{collection}->tags->[ $self->{current}++ ]; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub reset | ||
| 66 | { | ||
| 67 | my $self = shift; | ||
| 68 | $self->{current} = 0; | ||
| 69 | } | ||
| 70 | |||
| 71 | |||
| 72 | 1; | ||
| 73 | |||
| 74 | __END__ |