File Coverage

blib/lib/Catalyst/View/Template/Lace/Renderer.pm
Criterion Covered Total %
statement 21 169 12.4
branch 0 12 0.0
condition 0 2 0.0
subroutine 4 133 3.0
pod 5 126 3.9
total 30 442 6.7


line stmt bran cond sub pod time code
1             package Catalyst::View::Template::Lace::Renderer;
2              
3 1     1   6 use Moo;
  1         3  
  1         6  
4 1     1   2346 use HTTP::Status ();
  1         2  
  1         17  
5 1     1   5 use Catalyst::Utils;
  1         2  
  1         696  
6              
7             extends 'Template::Lace::Renderer';
8              
9             around 'prepare_component_attrs', sub {
10             my ($orig, $self, @args) = @_;
11             my %attrs = $self->$orig(@args);
12             $attrs{ctx} = $self->ctx;
13             return %attrs;
14             };
15              
16             sub inject_http_status_helpers {
17 1     1 0 3 my ($class) = @_;
18 1         6 foreach my $helper( grep { $_=~/^http/i} @HTTP::Status::EXPORT_OK) {
  61         190  
19 59         187 my $subname = lc $helper;
20 59         202 my $code = HTTP::Status->$helper;
21 59         115 my $codename = "http_".$code;
22 59     0 0 82 eval "sub ${\$class}::${\$subname} { return shift->respond(HTTP::Status::$helper,\@_) }";
  59     0 0 119  
  59     0 0 2260  
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0            
  0            
  0            
23 59     0 0 161 eval "sub ${\$class}::${\$codename} { return shift->respond(HTTP::Status::$helper,\@_) }";
  59     0 0 131  
  59     0 0 2249  
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0     0 0    
  0            
  0            
  0            
24             }
25             }
26              
27 0     0 1   sub ctx { shift->model->ctx }
28              
29 0     0 0   sub catalyst_component_name { shift->model->catalyst_component_name }
30              
31             sub respond {
32 0     0 1   my ($self, $status, $headers) = @_;
33 0           $self->_profile(begin => "=> ".Catalyst::Utils::class2classsuffix($self->catalyst_component_name)."->respond($status)");
34 0           for my $r ($self->ctx->res) {
35 0 0         $r->status($status) if $r->status != 200; # Catalyst sets 200
36 0 0         $r->content_type('text/html') if !$r->content_type;
37 0 0         $r->headers->push_header(@{$headers}) if $headers;
  0            
38 0           $r->body($self->render);
39             }
40 0           $self->_profile(end => "=> ".Catalyst::Utils::class2classsuffix($self->catalyst_component_name)."->respond($status)");
41 0           return $self;
42             }
43              
44             sub _profile {
45 0     0     my $self = shift;
46 0 0         $self->ctx->stats->profile(@_)
47             if $self->ctx->debug;
48             }
49              
50             # Support old school Catalyst::Action::RenderView for example (
51             # you probably also want the ::ArgsFromStash role).
52              
53             sub process {
54 0     0 0   my ($self, $c, @args) = @_;
55 0           $self->response(200, @args);
56             }
57              
58             # helper methods
59              
60             sub overlay_view {
61 0     0 1   my ($self, $view_name, $dom_proto, @args) = @_;
62 0 0 0       if( (ref($dom_proto)||'') eq 'CODE') {
    0          
63 0           local $_ = $self->dom;
64 0           @args = ($dom_proto->($self->dom), @args);
65             $self->dom->overlay(sub {
66 0     0     my $new = $self->view($view_name, @args, content=>$_)
67             ->get_processed_dom;
68 0           return $new;
69 0           });
70             } elsif($dom_proto->can('each')) {
71             $dom_proto->each(sub {
72 0     0     return $self->overlay_view($view_name, $_, @args);
73 0           });
74             } else {
75             $dom_proto->overlay(sub {
76 0     0     return $self->view($view_name, @args, content=>$_)
77             ->get_processed_dom;
78 0           });
79             }
80 0           return $self;
81             }
82              
83             # proxy methods
84              
85 0     0 1   sub detach { shift->ctx->detach(@_) }
86              
87 0     0 1   sub view { shift->ctx->view(@_) }
88              
89             # Helpers
90              
91             __PACKAGE__->inject_http_status_helpers;
92              
93             1;
94              
95             =head1 NAME
96              
97             Catalyst::View::Template::Lace::Renderer - Adapt Template::Lace for Catalyst
98              
99             =head1 SYNOPSIS
100              
101             TBD
102              
103             =head1 DESCRIPTION
104              
105             Subclass of L<Template::Lace:Renderer> with some useful, L<Catalyst> specific
106             methods.
107              
108             =head1 METHODS
109              
110             This class defines the following public methods
111              
112             =head2 respond
113              
114             $view->respond($status);
115             $view->respond($status, @headers);
116             $view->respond(@headers);
117            
118            
119             Used to setup a response. Calling this method will setup an http status, finalize
120             headers and set a body response for the HTML. Content type will be set to
121             'text/html' automatically. Status is 200 unless you specify otherwise.
122              
123             =head2 overlay_view
124              
125             Helper method to allow you to wrap or overlay the current view with another
126             view (like a master page view or some other transformation that you prefer
127             to have under the control of the controller). Example:
128              
129             $c->view('User',
130             name => 'John',
131             age => 42,
132             motto => 'Why Not?')
133             ->overlay_view(
134             'Master', sub {
135             my $user_dom = shift; # also $_ is localised to this for ease of use
136             title => $_->at('title')->content,
137             css => $_->find('link'),
138             meta => $_->find('meta'),
139             body => $_->at('body')->content}, @more_args_for_MasterView)
140             ->http_ok;
141              
142             Although you can do this via the template with components there might be cases
143             where you want this under the controller. For example you might use different
144             wrappers based on the logged in user (although again smart use of components could
145             solve that as well; the choice is yours).
146              
147             =head2 detach
148              
149             Proxy to '$c->detach'
150              
151             =head2 view
152              
153             Proxy to '$c->detach'
154              
155             =head2 ctx
156              
157             Proxy to '$c->ctx'
158              
159             =head1 Reponse Helpers
160              
161             We map status codes from L<HTTP::Status> into methods to make sending common
162             request types more simple and more descriptive. The following are the same:
163            
164             $c->view->respond(200, @args);
165             $c->view->http_ok(@args);
166            
167             do { $c->view->respond(200, @args); $c->detach };
168             $c->view->http_ok(@args)->detach;
169            
170             See L<HTTP::Status> for a full list of all the status code helpers.
171            
172             =head1 AUTHOR
173            
174              
175             John Napiorkowski L<email:jjnapiork@cpan.org>
176            
177             =head1 SEE ALSO
178            
179             L<Template::Lace>, L<Catalyst::View::Template::Lace>
180              
181             =head1 COPYRIGHT & LICENSE
182            
183             Copyright 2017, John Napiorkowski L<email:jjnapiork@cpan.org>
184            
185             This library is free software; you can redistribute it and/or modify it under
186             the same terms as Perl itself.
187              
188             =cut