Catalyst::ComponentRole::PathFrom - Derive a path using common Catalyst patterns
package MyApp::Model::Path; use Moose; extends 'Catalyst::Component'; with 'Catalyst::ComponentRole::PathFrom', 'Catalyst::Component::InstancePerContext'; has ctx => (is=>'rw', weak_ref=>1); sub build_per_context_instance { my ($self, $c) = @_; $self->ctx($c); return $self; } around 'path_from', sub { my ($orig, $self, @args) = @_; return $self->$orig($self->ctx, @args); }; __PACKAGE__->meta->make_immutable; package MyApp::Controller::Example; use base 'Catalyst::Controller'; sub test_a :Local { my ($self, $c) = @_; } sub test_b :Local PathFrom('ffffff') { my ($self, $c) = @_; } sub test_c :Local { my ($self, $c) = @_; $c->stash(path_from=>'foo/bar'); }
Common Catalyst views set a template path using a standard process, typically one based on the action or from a stash key. This component role trys to encapsulate that common pattern, with the hope that it makes it easier for people to make new Views in a consistent way. For example if you make your own custom Views this could save you some time in getting a common and expected setup.
This role exposes the following attributes for configuration
Optional. This is a file extension added to the end of your generated file path. For example 'html', 'tt2'. You don't need to include the '.' separator.
Has default, 'path_from'. Used to set the stash key you wish to use to programmatically set the file path pattern in your action body.
Has default, 'PathFrom'. Used to set the action attribute we use to get a file path pattern.
Has default "$app->config->{root}". Used to set the base path for relative paths. Usually you leave this one alone :)
This role exposes the following public methods
Builds a full path to a file on the filesystem using common Catalyst conventions.
Given an $action, will return $base_path + $action->reverse + $extension OR if the $action has an attribute value for $action_attribute, return $base_path + $action_attribute + $extension.
Given $c, will do all the above (using $c->action for $action), but also check if the stash contains $stash_key and if so use that path instead.
Given $c, $action, does as above but uses the given $action instead of $c->action
Given $c, $string_path, uses $string_path instead of $action->reverse.
When using a $string_path, a $stash_key value or a value in $action_attribute you may use the following placeholders in the string (for example ':namespace/foo')
The action namespace ($action->namespace), which is typically the controller namespace
"$action->reverse" (which is basically the default
"action->name" (the subroutine method name, typically).
NOTE: if you use a $string_path, a $stash_key value or a value in $action_attribute and that value starts with '/', that is a signal you wish to use an absolute path, and we don't prepend $self->base_path. You probably won't need this...
Catalyst, Catalyst::Component, File::Spec, Moose::Role.
John Napiorkowski email:jjnapiork@cpan.org
Copyright 2015, John Napiorkowski email:jjnapiork@cpan.org
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
2 POD Errors
The following errors were encountered while parsing the POD:
Unknown directive: =over4
'=item' outside of any '=over'
To install Catalyst::ComponentRole::PathFrom, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Catalyst::ComponentRole::PathFrom
CPAN shell
perl -MCPAN -e shell install Catalyst::ComponentRole::PathFrom
For more information on module installation, please visit the detailed CPAN module installation guide.