File Coverage

blib/lib/Catalyst/ActionRole/FindViewByIsa.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Catalyst::ActionRole::FindViewByIsa;
2 1     1   1758 use Moose::Role;
  0            
  0            
3             use List::MoreUtils qw/uniq/;
4             use namespace::autoclean;
5              
6             our $VERSION = '0.000003';
7              
8             sub BUILD { }
9              
10             after 'BUILD' => sub {
11             my ($self, $args) = @_;
12             my $attrs = $args->{attributes};
13             die("Catalyst::ActionRole::FindViewByIsa used without a FindViewByIsa attribute in /" . $self->reverse . "\n")
14             unless $attrs->{FindViewByIsa};
15             };
16              
17             after 'execute' => sub {
18             my ($self, $controller, $c, @args ) = @_;
19             return if $c->stash->{current_view};
20             my $isa = $self->attributes->{FindViewByIsa}[0];
21             if ($c->config->{default_view}) {
22             my $view = $c->view($c->config->{default_view});
23             $c->stash->{current_view} = $c->config->{default_view}
24             if $view->isa($isa);
25             }
26             my @views = grep { $c->view($_)->isa($isa) } $c->views;
27             die("$c does not have a view which is a subclass of $isa")
28             unless scalar @views;
29             $c->stash->{current_view} = $views[0];
30             };
31              
32              
33             =head1 NAME
34              
35             Catalyst::ActionRole::FindViewByIsa - Select from the available application views by type
36              
37             =head1 SYNOPSIS
38              
39             package MyApp::Controller::Foo;
40             use Moose;
41              
42             BEGIN { extends 'Catalyst::Controller::ActionRole'; }
43              
44             sub foo : Local Does('FindViewByIsa') FindViewByIsa('Catalyst::View::TT') {
45             # Code here. If $c->stash->{current_view} is set, it will be left alone
46             # after this method is run. Otherwise it will be set to
47             # the first app view which @ISA Catalyst::View::TT
48             }
49              
50             =head1 DESCRIPTION
51              
52             If you are trying to write a generic controller component which will be reused within an application, you do not
53             want to mandate the use of one type of view, but if you're providing templates with your component, then
54             you need to be able to find a view of the appropriate type.
55              
56             Therefore this action role will select a the view in the application which
57             matches the class of view that you want, no matter what it is named locally
58             within the application.
59              
60             =head1 AUTHOR
61              
62             Tomas Doran (t0m), C<< <bobtfish@bobtfish.net> >>
63              
64             =head1 COPYRIGHT & LICENSE
65              
66             Copyright 2009 Tomas Doran, some rights reserved.
67              
68             This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
69              
70             =cut
71              
72             1;
73