package Bloggery;
-default_config(
- title => 'Bloggery',
- posts_dir => $FindBin::Bin.'/posts',
-);
+has post_list => (is => 'lazy');
-sub post_list {
+sub default_config {
+ (
+ title => 'Bloggery',
+ posts_dir => $FindBin::Bin.'/posts',
+ );
+}
+
+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 {
sub dispatch_request {
my $self = shift;
sub (GET + /) {
- redispatch_to '/index.html';
+ redispatch_to '/index.html'
},
sub (.html) {
- response_filter { $self->render_html($_[1]) },
+ response_filter { $self->render_html(@_) }
},
sub (GET + /index) {
$self->post_list
use strictures 1;
use Exporter 'import';
-our @EXPORT_OK = qw(dispatch_wrapper redispatch_to response_filter);
+our @EXPORT = qw(dispatch_wrapper redispatch_to response_filter);
sub dispatch_wrapper (&) {
+ my ($code) = @_;
+ __PACKAGE__->from_code($code);
+}
+
+sub from_code {
my ($class, $code) = @_;
bless(\$code, $class);
}
sub redispatch_to {
- my ($class, $new_path) = @_;
- $class->from_code(sub {
+ my ($new_path) = @_;
+ __PACKAGE__->from_code(sub {
$_[1]->({ %{$_[0]}, PATH_INFO => $new_path });
});
}
sub response_filter (&) {
- my ($class, $code) = @_;
- $class->from_code(sub {
+ my ($code) = @_;
+ __PACKAGE__->from_code(sub {
my @result = $_[1]->($_[0]);
if (@result) {
$code->(@result);
use strictures 1;
use 5.008;
use warnings::illegalproto ();
+use Moo ();
+use Web::Dispatch::Wrapper ();
our $VERSION = '0.004';
sub import {
my ($class, $app_package) = @_;
- $class->_export_into($app_package||caller);
- eval "package $class; use Web::Dispatch::Wrapper; use Moo;";
+ $app_package ||= caller;
+ $class->_export_into($app_package);
+ eval "package $app_package; use Web::Dispatch::Wrapper; use Moo; 1"
+ or die "Failed to setup app package: $@";
strictures->import;
warnings::illegalproto->unimport;
}
use Moo;
-has 'config' => (is => 'ro', trigger => sub {
- my ($self, $value) = @_;
- my %default = $self->_default_config;
- my @not = grep !exists $value->{$_}, keys %default;
- @{$value}{@not} = @default{@not};
-});
+has 'config' => (
+ is => 'ro',
+ default => sub {
+ my ($self) = @_;
+ +{ $self->default_config }
+ },
+ trigger => sub {
+ my ($self, $value) = @_;
+ my %default = $self->default_config;
+ my @not = grep !exists $value->{$_}, keys %default;
+ @{$value}{@not} = @default{@not};
+ }
+);
sub default_config { () }