File Coverage

blib/lib/Catalyst/View/BasePerRequest/Lifecycle/Request.pm
Criterion Covered Total %
statement 32 37 86.4
branch 5 8 62.5
condition 5 14 35.7
subroutine 8 10 80.0
pod 0 6 0.0
total 50 75 66.6


line stmt bran cond sub pod time code
1             package Catalyst::View::BasePerRequest::Lifecycle::Request;
2              
3 1     1   683 use warnings;
  1         3  
  1         45  
4 1     1   16 use strict;
  1         2  
  1         34  
5 1     1   7 use Scalar::Util ();
  1         3  
  1         13  
6 1     1   6 use Module::Runtime ();
  1         3  
  1         510  
7              
8             sub ACCEPT_CONTEXT {
9 12     12 0 132401 my ($factory, $c, @args) = @_;
10 12   33     52 my $key = Scalar::Util::refaddr($factory) || $factory;
11 12   33     36 return $c->stash->{"__BasePerRequest_${key}"} ||= $factory->build($c, @args);
12             }
13              
14             sub build {
15 12     12 0 883 my ($factory, $c, @args) = @_;
16 12         36 my $class = $factory->{class};
17 12 100       125 @args = $class->prepare_build_args($c, @args) if $class->can('prepare_build_args');
18 12         100 my %build_args = $factory->prepare_build_args($c, @args);
19             my $view = eval {
20             $class->build(%build_args);
21 12   33     30 } || do {
22             $factory->do_handle_build_view_exception($class, $@);
23             };
24 12         1217 $c->stash(current_view_instance=>$view);
25 12         991 return $view;
26             }
27              
28 0     0 0 0 sub build_view_error_class { return 'Catalyst::View::BasePerRequest::Exception::BuildViewError' }
29              
30             sub do_handle_build_view_exception {
31 0     0 0 0 my ($factory, $class, $err) = @_;
32 0 0 0     0 return $err->rethrow if Scalar::Util::blessed($err) && $err->can('rethrow');
33 0         0 my $exception = Module::Runtime::use_module($factory->build_view_error_class);
34 0         0 $exception->throw(class=>$class, build_error=>$err);
35             }
36              
37             sub prepare_build_args {
38 12     12 0 33 my ($factory, $c, @args) = @_;
39 12         26 my %args = $factory->prepare_args(@args);
40 12 50       29 my %merged_args = %{$factory->{merged_args}||+{}};
  12         67  
41 12         75 return (%merged_args, %args, app=>$factory->{app}, ctx=>$c);
42             }
43              
44             sub prepare_args {
45 12     12 0 28 my ($factory, @args) = @_;
46 12 100 100     63 if( (ref($args[-1])||'') eq 'CODE' ) {
47 6         50 my $code = pop @args;
48 6         14 push @args, (code=>$code);
49             }
50 12         58 return @args;
51             }
52              
53             1;
54              
55             =head1 NAME
56            
57             Catalyst::View::BasePerRequest::Lifecycle::Request - Build a per request view
58              
59             =head1 SYNOPSIS
60              
61             Not intended for standalone enduser use.
62              
63             =head1 DESCRIPTION
64              
65             This module is used by L<Catalyst::View::BasePerRequest> to encapsulate building the actual
66             view object on a per request basis. Code here might be of interest to people building their
67             own view frameworks. for example you might wish to override the exception handler or change
68             how the initialization arguments are made.
69              
70             =head1 ALSO SEE
71            
72             L<Catalyst::View::BasePerRequest>
73              
74             =head1 AUTHORS & COPYRIGHT
75            
76             See L<Catalyst::View::BasePerRequest>
77              
78             =head1 LICENSE
79            
80             See L<Catalyst::View::BasePerRequest>
81            
82             =cut