| 1 | # $Id: /local/perl/HTML-TagClouder/trunk/lib/HTML/TagClouder.pm 11417 2007-05-24T00:51:44.727126Z daisuke $ | ||
| 2 | # | ||
| 3 | # Copyright (c) 2007 Daisuke Maki <daisuke@endeworks.jp> | ||
| 4 | |||
| 5 | package HTML::TagClouder; | ||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use base qw(Class::Accessor::Fast); | ||
| 9 | use Class::C3; | ||
| 10 | use Class::Inspector; | ||
| 11 | use UNIVERSAL::isa; | ||
| 12 | use UNIVERSAL::require; | ||
| 13 | use overload | ||
| 14 | '""' => \&render, | ||
| 15 | fallback => 1 | ||
| 16 | ; | ||
| 17 | INIT { Class::C3::initialize() } | ||
| 18 | use HTML::TagClouder::Tag; | ||
| 19 | |||
| 20 | our $VERSION = 0.02; | ||
| 21 | |||
| 22 | __PACKAGE__->mk_accessors($_) for | ||
| 23 | qw(renderer collection processor is_processed) | ||
| 24 | ; | ||
| 25 | |||
| 26 | sub new | ||
| 27 | { | ||
| 28 | my $class = shift; | ||
| 29 | my $self = bless {}, $class; | ||
| 30 | $self->setup(@_); | ||
| 31 | |||
| 32 | return $self; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub setup | ||
| 36 | { | ||
| 37 | my $self = shift; | ||
| 38 | my %args = @_; | ||
| 39 | $self->is_processed(0); | ||
| 40 | |||
| 41 | my $render_class = $self->_load_module( $args{render_class} || 'HTML::TagClouder::Render::TT'); | ||
| 42 | $self->renderer( $render_class->new(%{ $args{render_class_args} || {} }, cloud => $self) ); | ||
| 43 | |||
| 44 | my $collection_class = $self->_load_module( $args{collection_class} || 'HTML::TagClouder::Collection::Simple' ); | ||
| 45 | $self->collection( $collection_class->new(%{ $args{collection_class_args} || {} }, cloud => $self) ); | ||
| 46 | |||
| 47 | my $processor_class = $self->_load_module( $args{processor_class} || 'HTML::TagClouder::Processor::Simple' ); | ||
| 48 | $self->processor( $processor_class->new(%{ $args{processor_class} || {} }) ); | ||
| 49 | |||
| 50 | } | ||
| 51 | |||
| 52 | sub _load_module | ||
| 53 | { | ||
| 54 | my $self = shift; | ||
| 55 | my $class = shift; | ||
| 56 | if (! Class::Inspector->loaded( $class )) { | ||
| 57 | $class->require or die "Could not require $class: $@"; | ||
| 58 | } | ||
| 59 | return $class; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub process | ||
| 63 | { | ||
| 64 | my $self = shift; | ||
| 65 | $self->processor->process( $self ); | ||
| 66 | $self->is_processed(1); | ||
| 67 | } | ||
| 68 | |||
| 69 | sub render | ||
| 70 | { | ||
| 71 | my $self = shift; | ||
| 72 | |||
| 73 | if (! $self->is_processed) { | ||
| 74 | $self->process; | ||
| 75 | } | ||
| 76 | |||
| 77 | $self->renderer()->render($self); | ||
| 78 | } | ||
| 79 | |||
| 80 | sub add | ||
| 81 | { | ||
| 82 | my $self = shift; | ||
| 83 | my $tag; | ||
| 84 | if (ref $_[0] && $_[0]->isa('HTML::TagClouder::Tag')) { | ||
| 85 | $tag = shift; | ||
| 86 | } else { | ||
| 87 | my ($label, $uri, $count, $timestamp) = @_; | ||
| 88 | $tag = HTML::TagClouder::Tag->new( | ||
| 89 | label => $label, | ||
| 90 | count => $count, | ||
| 91 | uri => $uri, | ||
| 92 | timestamp => $timestamp || now() | ||
| 93 | ); | ||
| 94 | } | ||
| 95 | $self->collection->add($tag); | ||
| 96 | } | ||
| 97 | |||
| 98 | 1; | ||
| 99 | |||
| 100 | __END__ |