Created
July 17, 2009 10:01
-
-
Save memowe/148986 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Blog::Post; | |
use Moose; | |
use Moose::Util::TypeConstraints; | |
use File::stat; | |
use File::Slurp; | |
use DateTime; | |
class_type 'DateTime'; | |
coerce 'DateTime' => from 'Int' => via { | |
DateTime->from_epoch( | |
epoch => $_, | |
time_zone => 'Europe/Berlin', | |
) | |
}; | |
has filename => ( is => 'ro', isa => 'Str', required => 1 ); | |
has url => ( is => 'ro', isa => 'Str', required => 1 ); | |
has tags => ( is => 'rw', isa => 'ArrayRef[Str]' ); | |
has next => ( is => 'rw', isa => 'Maybe[Blog::Post]' ); | |
has prev => ( is => 'rw', isa => 'Maybe[Blog::Post]' ); | |
has time => ( is => 'ro', isa => 'DateTime', coerce => 1, lazy_build => 1 ); | |
has title => ( is => 'ro', isa => 'Str', lazy_build => 1 ); | |
has teaser => ( is => 'ro', isa => 'Maybe[Str]', lazy_build => 1 ); | |
has content => ( is => 'ro', isa => 'Str', lazy_build => 1 ); | |
sub _build_time { | |
my ( $self ) = @_; | |
return +( stat( $self->filename ) )->mtime, | |
} | |
sub _build_title { | |
my ( $self ) = @_; | |
if ( $self->content =~ /^(.*)\n==+\n/m ) { | |
( my $title = $1 ) =~ tr/_/ /; | |
return ucfirst $title; | |
} | |
else { | |
return $self->url; | |
} | |
} | |
sub _build_teaser { | |
my ( $self ) = @_; | |
return $self->content =~ /^\*\*(.*)\*\*\n/m ? $1 : undef; | |
} | |
sub _build_content { | |
my ( $self ) = @_; | |
return read_file( $self->filename ); | |
} | |
use overload '""' => sub { $_[0]->title }; | |
use overload '<=>' => sub { $_[0]->time->epoch <=> $_[1]->time->epoch }; | |
use overload 'cmp' => sub { $_[0]->time->epoch cmp $_[1]->time->epoch }; | |
Blog::Post->meta->make_immutable; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment