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.004001';
3              
4 2     2   5965492 use Moo;
  2         28111  
  2         3136  
5              
6 2     2   3188 use Module::Runtime qw(use_module);
  2         5  
  2         23  
7 2     2   105 use Carp qw(croak confess);
  2         33  
  2         192  
8 2     2   845 use Devel::Dwarn;
  2         16806  
  2         11  
9 2     2   1382 use Safe::Isa;
  2         847  
  2         194  
10              
11 2     2   1048 use namespace::clean -except => [qw(meta)];
  2         20533  
  2         16  
12 2     2   1559 use MooX::StrictConstructor;
  2         21643  
  2         11  
13              
14 2     2   38969 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 routes => (
22             is => 'ro',
23             required => 1,
24             );
25              
26             has route_maker => (
27             is => 'ro',
28             lazy => 1,
29             builder => 1,
30             isa => sub {
31             die "$_[0] is not a WebAPI::DBIC::RouteMaker" unless $_[0]->$_isa('WebAPI::DBIC::RouteMaker');
32             },
33             );
34              
35             sub _build_route_maker {
36             my ($self) = @_;
37              
38             return WebAPI::DBIC::RouteMaker->new();
39             }
40              
41              
42              
43             sub to_psgi_app {
44             my ($self) = @_;
45              
46             my $router = WebAPI::DBIC::Router->new; # XXX
47              
48             for my $route_spec (@{ $self->routes }) {
49              
50             for my $route ($self->route_maker->make_routes_for($route_spec)) {
51              
52             $router->add_route( $route->as_add_route_args );
53              
54             }
55             }
56              
57             if (not $router->match('/')) {
58             $router->add_route( $self->route_maker->make_root_route->as_add_route_args );
59             }
60              
61             return $router->to_psgi_app; # return Plack app
62             }
63              
64              
65             1;
66              
67             __END__
68              
69             =pod
70              
71             =encoding UTF-8
72              
73             =head1 NAME
74              
75             WebAPI::DBIC::WebApp
76              
77             =head1 VERSION
78              
79             version 0.004001
80              
81             =head1 SYNOPSIS
82              
83             This minimal example creates routes for all data sources in the schema:
84              
85             $app = WebAPI::DBIC::WebApp->new({
86             routes => [ map( $schema->source($_), $schema->sources) ]
87             })->to_psgi_app;
88              
89             is the same as:
90              
91             $app = WebAPI::DBIC::WebApp->new({
92             routes => [
93             { set => $schema->source('Artist') },
94             { set => $schema->source('CD') },
95             { set => $schema->source('Genre') },
96             { set => $schema->source('Track') },
97             ...
98             ]
99             })->to_psgi_app;
100              
101             which is the same as:
102              
103             $app = WebAPI::DBIC::WebApp->new({
104             routes => [
105             ... # as above
106             ],
107             route_maker => WebAPI::DBIC::RouteMaker->new(
108             resource_class_for_item => 'WebAPI::DBIC::Resource::GenericItem',
109             resource_class_for_item_invoke => 'WebAPI::DBIC::Resource::GenericItemInvoke',
110             resource_class_for_set => 'WebAPI::DBIC::Resource::GenericSet',
111             resource_class_for_set_invoke => 'WebAPI::DBIC::Resource::GenericSetInvoke',
112             resource_default_args => { },
113             type_namer => WebAPI::DBIC::TypeNamer->new( # EXPERIMENTAL
114             type_name_inflect => 'singular', # XXX will change to plural soon
115             type_name_style => 'under_score', # or 'camelCase' etc
116             ),
117             ),
118             })->to_psgi_app;
119              
120             The elements in C<routes> are passed to the specified C<route_maker>.
121             The elements can include any mix of result source objects, as in the example
122             above, resultset objects, and L<WebAPI::DBIC::Route> objects.
123              
124             WebAPI::DBIC::WebApp uses the L<WebAPI::DBIC::RouteMaker> object to convert and
125             expands the given C<routes> into a corresponding set of WebAPI::DBIC::Routes.
126             For example, if we gloss over some details along the way, a C<routes>
127             specification like this:
128              
129             routes => [
130             $schema->source('CD'),
131             ]
132              
133             is a short-hand way of writing this:
134              
135             routes => [
136             { set => $schema->source('CD'), path => undef, ... }
137             ]
138              
139             is a short-hand way of writing this:
140              
141             routes => [
142             $route_maker->make_routes_for( { set => $schema->source('CD'), ... } )
143             ]
144              
145             which is a short-hand way of writing this:
146              
147             $cd_resultset = $schema->source('CD')->resultset;
148             $cd_path = $type_namer->type_name_for_resultset($cd_resultset);
149             routes => [
150             $route_maker->make_routes_for_resultset($cd_path, $cd_resultset, ...)
151             ]
152              
153             which is a short-hand way of writing this:
154              
155             $cd_resultset = $schema->source('CD')->resultset;
156             $cd_path = $type_namer->type_name_for_resultset($cd_resultset);
157             routes => [
158             $route_maker->make_routes_for_set($cd_path, $cd_resultset), # /cd
159             $route_maker->make_routes_for_item($cd_path, $cd_resultset), # /cd/:id
160             ]
161              
162             which is a short-hand way of writing something much longer with explict calls
163             to create the fully specified L<WebAPI::DBIC::Route> objects.
164              
165             The I<default> URL path prefix is determined by the C<type_namer> from the
166             resultset source name using the C<type_name_inflect> and C<type_name_style> settings.
167             For example, a result source name of C<ClassicAlbum> would have a URL path
168             prefix of C</classic_albums> by default, i.e. plural, and lowercased with
169             underscores between words.
170              
171             =head1 NAME
172              
173             WebAPI::DBIC::WebApp - Build a Plack app using WebAPI::DBIC
174              
175             =head1 AUTHOR
176              
177             Tim Bunce <Tim.Bunce@pobox.com>
178              
179             =head1 COPYRIGHT AND LICENSE
180              
181             This software is copyright (c) 2015 by Tim Bunce.
182              
183             This is free software; you can redistribute it and/or modify it under
184             the same terms as the Perl 5 programming language system itself.
185              
186             =cut