| 1 | package Catalyst::ActionRole::CurrentView; | ||
| 2 | |||
| 3 | use Moose::Role; | ||
| 4 | |||
| 5 | requires 'attributes'; | ||
| 6 | |||
| 7 | has current_view => ( | ||
| 8 | is => 'ro', | ||
| 9 | required => 1, | ||
| 10 | lazy => 1, | ||
| 11 | builder => '_build_current_view' ); | ||
| 12 | |||
| 13 | sub _build_current_view { | ||
| 14 | my $self = shift; | ||
| 15 | my ($current_view) = @{$self->attributes->{View} || []}; | ||
| 16 | return $current_view; | ||
| 17 | } | ||
| 18 | |||
| 19 | around 'execute', sub { | ||
| 20 | my ($orig, $self, $controller, $ctx, @args) = @_; | ||
| 21 | $ctx->stash(current_view=>$self->current_view) if $self->current_view; | ||
| 22 | return $self->$orig($controller, $ctx, @args); | ||
| 23 | }; | ||
| 24 | |||
| 25 | |||
| 26 | 1; | ||
| 27 | |||
| 28 | =head1 NAME | ||
| 29 | |||
| 30 | Catalyst::ActionRole::CurrentView - Set the current view via an action attribute | ||
| 31 | |||
| 32 | =head1 SYNOPSIS | ||
| 33 | |||
| 34 | package MyApp::Controller::Example; | ||
| 35 | |||
| 36 | use Moose; | ||
| 37 | use MooseX::MethodAttributes; | ||
| 38 | |||
| 39 | extends 'Catalyst::Controller'; | ||
| 40 | |||
| 41 | # Same as $c->stash(current_view => 'HTML'); | ||
| 42 | sub root :Chained(/) Does(CurrentView) View(HTML) { | ||
| 43 | my ($self, $c) = @_; | ||
| 44 | } | ||
| 45 | |||
| 46 | __PACKAGE__->meta->make_immutable; | ||
| 47 | |||
| 48 | =head1 DESCRIPTION | ||
| 49 | |||
| 50 | Just an actionrole to let you set the current view via an attribute | ||
| 51 | |||
| 52 | =head1 AUTHOR | ||
| 53 | |||
| 54 | John Napiorkowski <jnapiork@cpan.org> | ||
| 55 | |||
| 56 | =head1 COPYRIGHT | ||
| 57 | |||
| 58 | Copyright (c) 2021 the above named AUTHOR and CONTRIBUTORS | ||
| 59 | |||
| 60 | =head1 LICENSE | ||
| 61 | |||
| 62 | You may distribute this code under the same terms as Perl itself. | ||
| 63 | |||
| 64 | =cut |