File Coverage

blib/lib/WebAPI/DBIC/WebApp.pm
Criterion Covered Total %
statement 22 24 91.6
branch n/a
condition n/a
subroutine 8 8 100.0
pod n/a
total 30 32 93.7


line stmt bran cond sub pod time code
1             package WebAPI::DBIC::WebApp;
2             $WebAPI::DBIC::WebApp::VERSION = '0.003002';
3              
4 2     2   3127069 use Moo;
  2         37230  
  2         15  
5              
6 2     2   2734 use Module::Runtime qw(use_module);
  2         3  
  2         73  
7 2     2   111 use Carp qw(croak confess);
  2         3  
  2         217  
8 2     2   1205 use Devel::Dwarn;
  2         19057  
  2         29  
9 2     2   1257 use Safe::Isa;
  2         854  
  2         203  
10              
11 2     2   965 use namespace::clean -except => [qw(meta)];
  2         20304  
  2         17  
12 2     2   1755 use MooX::StrictConstructor;
  2         18888  
  2         11  
13              
14 2     2   35497 use Web::Machine;
  0            
  0            
15              
16             use WebAPI::DBIC::RouteMaker;
17             use WebAPI::DBIC::Router;
18             use WebAPI::DBIC::Route;
19              
20              
21             has schema => (is => 'ro', required => 1);
22              
23             has routes => (
24             is => 'ro',
25             lazy => 1,
26             default => sub { [ sort shift->schema->sources ] },
27             );
28              
29             has route_maker => (
30             is => 'ro',
31             lazy => 1,
32             builder => 1,
33             isa => sub {
34             die "$_[0] is not a WebAPI::DBIC::RouteMaker" unless $_[0]->$_isa('WebAPI::DBIC::RouteMaker');
35             },
36             );
37              
38             sub _build_route_maker {
39             my ($self) = @_;
40              
41             return WebAPI::DBIC::RouteMaker->new();
42             }
43              
44              
45              
46             sub to_psgi_app {
47             my ($self) = @_;
48              
49             my $router = WebAPI::DBIC::Router->new; # XXX
50              
51             # set the route_maker schema here so users don't have
52             # to set schema in both WebApp and RouteMaker
53             $self->route_maker->schema($self->schema);
54              
55             for my $route_spec (@{ $self->routes }) {
56              
57             for my $route ($self->route_maker->make_routes_for($route_spec)) {
58              
59             $router->add_route( $route->as_add_route_args );
60              
61             }
62             }
63              
64             if (not $router->match('/')) {
65             $router->add_route( $self->route_maker->make_root_route->as_add_route_args );
66             }
67              
68             return $router->to_psgi_app; # return Plack app
69             }
70              
71              
72             1;
73              
74             __END__
75              
76             =pod
77              
78             =encoding UTF-8
79              
80             =head1 NAME
81              
82             WebAPI::DBIC::WebApp
83              
84             =head1 VERSION
85              
86             version 0.003002
87              
88             =head1 SYNOPSIS
89              
90             This most simple example:
91              
92             $app = WebAPI::DBIC::WebApp->new({
93             schema => $schema,
94             })->to_psgi_app;
95              
96             is the same as:
97              
98             $app = WebAPI::DBIC::WebApp->new({
99             schema => $schema,
100             routes => [ $schema->sources ],
101             })->to_psgi_app;
102              
103             which is the same as:
104              
105             $app = WebAPI::DBIC::WebApp->new({
106             schema => $schema,
107             routes => [ $schema->sources ],
108             route_maker => WebAPI::DBIC::RouteMaker->new(
109             resource_class_for_item => 'WebAPI::DBIC::Resource::GenericItem',
110             resource_class_for_item_invoke => 'WebAPI::DBIC::Resource::GenericItemInvoke',
111             resource_class_for_set => 'WebAPI::DBIC::Resource::GenericSet',
112             resource_class_for_set_invoke => 'WebAPI::DBIC::Resource::GenericSetInvoke',
113             resource_default_args => { },
114             type_namer => WebAPI::DBIC::TypeNamer->new( # EXPERIMENTAL
115             type_name_inflect => 'singular', # XXX will change to plural soon
116             type_name_style => 'under_score', # or 'camelCase' etc
117             ),
118             ),
119             })->to_psgi_app;
120              
121             The elements in C<routes> are passed to the specified C<route_maker>.
122             The elements can include any mix of result source names, as in the example above,
123             resultset objects, and L<WebAPI::DBIC::Route> objects.
124              
125             Result source names are converted to resultset objects.
126              
127             The L<WebAPI::DBIC::RouteMaker> object converts the resultset objects
128             into a set of WebAPI::DBIC::Routes, e.g, C<foo/> for the resultset and
129             C<foo/:id> for an item of the set.
130              
131             The path prefix, i.e., C<foo> is determined from the resultset using the
132             C<type_name_inflect> and C<type_name_style> to define the route path, and the
133             C<resource_class_for_*> to define the resource classes that the routes should
134             refer to.
135              
136             =head1 NAME
137              
138             WebAPI::DBIC::WebApp - Build a Plack app using WebAPI::DBIC
139              
140             =head1 AUTHOR
141              
142             Tim Bunce <Tim.Bunce@pobox.com>
143              
144             =head1 COPYRIGHT AND LICENSE
145              
146             This software is copyright (c) 2015 by Tim Bunce.
147              
148             This is free software; you can redistribute it and/or modify it under
149             the same terms as the Perl 5 programming language system itself.
150              
151             =cut