package Plagger::Plugin::Filter::ForceDate;

use strict;
use warnings;

use Plagger::CacheProxy;
use Plagger::Date;

use base qw( Plagger::Plugin );

sub register {
    my ( $self, $c ) = @_;
    $c->register_hook(
        $self,
        'update.entry.fixup'    => $self->can('update'),
    );
}

sub init {
    my $self = shift;
    $self->SUPER::init( @_ );

    $self->{'cache'} = Plagger::CacheProxy->new( $self, Plagger->context->cache );
}

sub update {
    my ( $self, $c, $args ) = @_;
    my $entry = $args->{'entry'};

    return if ( ref $entry->date );

    my $cache = $self->{'cache'};

    my $data = $cache->get( $entry->permalink );

    if ( defined $data && $data->{'body'} eq $entry->body ) {
        $entry->date( $data->{'date'} );
    }
    else {
        $entry->date( Plagger::Date->now );
        $cache->set(
            $entry->permalink => {
                date    => $entry->date,
                body    => $entry->body,
            },
        );
    }
}


1;
__END__

=head1 NAME

Plagger::Plugin::Filter::ForceDate - Force set C<$entry-E<gt>date>

=head1 SYNOPSIS

  - module: Filter::ForceDate

=head1 DESCRIPTION

When C<$entry-E<gt>date> isn't set, this plug-in sets C<$entry-E<gt>date> compulsorily.

=head1 AUTHOR

Naoki Okamura (Nyarla,) E<lt>thotep@nyarla.netE<gt>

=head1 LICENSE

This Plug-in is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

=head1 SEE ALSO

L<Plagger>

=cut
