Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added test and documentation for conditional API

  • Loading branch information...
commit 806ebc48d4db27810b6cc2baa512e1dadf19b76e 1 parent 43321f3
@miyagawa miyagawa authored
Showing with 48 additions and 26 deletions.
  1. +40 −26 lib/Plack/Builder.pm
  2. +8 −0 t/Plack-Builder/oo_interface.t
View
66 lib/Plack/Builder.pm
@@ -162,35 +162,11 @@ Plack::Builder - OO and DSL to enable Plack Middlewares
};
# using OO interface
-
- # Just add middleware
my $builder = Plack::Builder->new;
$builder->add_middleware('Foo', opt => 1);
$builder->add_middleware('Bar');
$builder->wrap($app);
- # With mount
- my $builder = Plack::Builder->new;
- $builder->add_middleware('Foo', opt => 1);
- $builder->mount('/foo' => $foo_app);
- $builder->mount('/' => $root_app);
- $builder->to_app;
-
- # Nested builders. Equivalent to:
- # builder {
- # mount '/foo' => builder {
- # enable 'Foo';
- # $app;
- # };
- # mount '/' => $app2;
- # };
- my $builder_out = Plack::Builder->new;
- my $builder_in = Plack::Builder->new;
- $builder_in->add_middleware('Foo');
- $builder_out->mount("/foo" => $builder_in->wrap($app));
- $builder_out->mount("/" => $app2);
- $builder_out->to_app;
-
=head1 DESCRIPTION
Plack::Builder gives you a quick domain specific language (DSL) to
@@ -317,8 +293,46 @@ which is located under C</foo> because of the outer C<builder> block.
=head1 CONDITIONAL MIDDLEWARE SUPPORT
You can use C<enable_if> to conditionally enable middleware based on
-the runtime environment. See L<Plack::Middleware::Conditional> for
-details.
+the runtime environment.
+
+ builder {
+ enable_if { $_[0]->{REMOTE_ADDR} eq '127.0.0.1' } 'StackTrace', force => 1;
+ $app;
+ };
+
+See L<Plack::Middleware::Conditional> for details.
+
+=head1 OBJECT ORIENTED INTERFACE
+
+Object oriented interface supports the same functionality with the DSL
+version in a clearer interace, probably with more typing required.
+
+ # With mount
+ my $builder = Plack::Builder->new;
+ $builder->add_middleware('Foo', opt => 1);
+ $builder->mount('/foo' => $foo_app);
+ $builder->mount('/' => $root_app);
+ $builder->to_app;
+
+ # Nested builders. Equivalent to:
+ # builder {
+ # mount '/foo' => builder {
+ # enable 'Foo';
+ # $app;
+ # };
+ # mount '/' => $app2;
+ # };
+ my $builder_out = Plack::Builder->new;
+ my $builder_in = Plack::Builder->new;
+ $builder_in->add_middleware('Foo');
+ $builder_out->mount("/foo" => $builder_in->wrap($app));
+ $builder_out->mount("/" => $app2);
+ $builder_out->to_app;
+
+ # conditional. You can also directly use Plack::Middleware::Conditional
+ my $builder = Plack::Builder->new;
+ $builder->add_middleware_if(sub { $_[0]->{REMOTE_ADDR} eq '127.0.0.1' }, 'StackTrace');
+ $builder->wrap($app);
=head1 SEE ALSO
View
8 t/Plack-Builder/oo_interface.t
@@ -40,6 +40,14 @@ sub test_app {
{
my $builder = Plack::Builder->new;
+ $builder->add_middleware_if(sub { $_[0]->{HTTP_HOST} eq 'localhost' }, 'Runtime');
+ $builder->add_middleware('XFramework', framework => 'Plack::Builder');
+ $builder->mount('/app/foo/bar' => $app);
+ test_app $builder->to_app;
+}
+
+{
+ my $builder = Plack::Builder->new;
$builder->add_middleware('Runtime');
eval { $builder->to_app };
like $@, qr/called without mount/, $@;

1 comment on commit 806ebc4

@oalders

This additional documentation for the OO interface is very helpful. :+1:

Please sign in to comment.
Something went wrong with that request. Please try again.