return Bloggery::Post->from_file($file);
}
+sub map {
+ my ($self, $code) = @_;
+ map $code->($_), $self->all;
+}
+
package Bloggery::Post;
sub from_file {
package Bloggery;
-default_config(
- title => 'Bloggery',
- posts_dir => $FindBin::Bin.'/posts',
-);
+has post_list => (is => 'lazy');
+
+sub default_config {
+ (
+ title => 'Bloggery',
+ posts_dir => $FindBin::Bin.'/posts',
+ );
+}
-sub post_list {
+sub _build_post_list {
my ($self) = @_;
- $self->{post_list}
- ||= Bloggery::PostList->from_dir(
- $self->config->{posts_dir}
- );
+ Bloggery::PostList->from_dir(
+ $self->config->{posts_dir}
+ );
}
sub post {
$self->post_list->post($post);
}
-dispatch [
- sub (.html) {
- filter_response { $self->render_html($_[1]) },
- },
+sub dispatch_request {
+ my $self = shift;
sub (GET + /) {
- $self->redispatch('index.html')
+ redispatch_to '/index.html'
+ },
+ sub (.html) {
+ response_filter { $self->render_html(@_) }
},
sub (GET + /index) {
$self->post_list
},
sub (GET + /*) {
- $self->post($_[1]);
+ $self->post($_[1])
},
sub (GET) {
[ 404, [ 'Content-type', 'text/plain' ], [ 'Not found' ] ]
sub {
[ 405, [ 'Content-type', 'text/plain' ], [ 'Method not allowed' ] ]
},
-];
+};
sub render_html {
my ($self, $data) = @_;
$data->html
} elsif ($data->isa('Bloggery::PostList')) {
<ul>,
- (map {
- my $path = '/'.$_->name.'.html';
+ $data->map(sub {
+ my $path = $_->name.'.html';
<li>,
<h4>, <a href="$path">, $_->title, </a>, </h4>,
<span class="summary">, $_->summary_html, </span>,
</li>;
- } $data->all),
+ }),
</ul>;
} else {
<h2>, "Don't know how to render $data", </h2>;