| 1 | package Template::Plugin::StashValidate; | ||||||||||
| 2 | 1 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | $Template::Plugin::StashValidate::VERSION = '0.03'; | |||||||||
| 4 | } | ||||||||||
| 5 | |||||||||||
| 6 | =head1 NAME | ||||||||||
| 7 | |||||||||||
| 8 | Template::Plugin::StashValidate - MooseX::Params::Validate for template stash values | ||||||||||
| 9 | |||||||||||
| 10 | =head1 VERSION | ||||||||||
| 11 | |||||||||||
| 12 | version 0.03 | ||||||||||
| 13 | |||||||||||
| 14 | =head1 DESCRIPTION | ||||||||||
| 15 | |||||||||||
| 16 | Allows a template to validate specific hash keys via MooseX::Params::Validate | ||||||||||
| 17 | |||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||
| 19 | |||||||||||
| 20 | [% USE StashValidate { | ||||||||||
| 21 | 'advice_discrepant' => { 'isa' => 'ArrayRef | HashRef', 'optional' => 1 }, | ||||||||||
| 22 | } %] | ||||||||||
| 23 | |||||||||||
| 24 | =head1 OVERVIEW | ||||||||||
| 25 | |||||||||||
| 26 | Allows a template to validate keys from the stash (L<Template::Stash>) using | ||||||||||
| 27 | L<MooseX::Params::Validate>. Accepts a hashref as the sole argument, and this is | ||||||||||
| 28 | the C<parameter_spec> that's passed straight through to | ||||||||||
| 29 | L<MooseX::Params::Validate>'s C<validated_hash>. We only validate elements in | ||||||||||
| 30 | the stash for which you've specified an allowed value - other keys in the stash | ||||||||||
| 31 | are ignored. | ||||||||||
| 32 | |||||||||||
| 33 | B<In short, for options, see>: L<MooseX::Params::Validate>. | ||||||||||
| 34 | |||||||||||
| 35 | L<MooseX::Params::Validate> supports both coerced values and default values - | ||||||||||
| 36 | this means the value you put in might not be the value you get out again. This | ||||||||||
| 37 | module supports that - the stash is updated with any changes returned. | ||||||||||
| 38 | |||||||||||
| 39 | =cut | ||||||||||
| 40 | |||||||||||
| 41 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 42 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 43 | 1 | ▶ | use MooseX::Params::Validate; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 44 | 1 | ▶ | use base 'Template::Plugin'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 45 | |||||||||||
| 46 | =head1 METHODS | ||||||||||
| 47 | |||||||||||
| 48 | =head2 new | ||||||||||
| 49 | |||||||||||
| 50 | This is the method called when you say C<[% USE StashValidate {} %]>, as per | ||||||||||
| 51 | the documentation in L<Template::Plugin>. | ||||||||||
| 52 | |||||||||||
| 53 | =cut | ||||||||||
| 54 | |||||||||||
| 55 | 4 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 56 | 4 | my ($class, $context, $params) = @_; | |||||||||
| 57 | 4 | my $stash = $context->stash; | |||||||||
| 58 | |||||||||||
| 59 | 4 | my %check; | |||||||||
| 60 | { | ||||||||||
| 61 | # Template::Stash returns an empty string for undefined values. That's | ||||||||||
| 62 | # almost certainly NOT what we want here, so knock it out for this block | ||||||||||
| 63 | 1 | ▶ | no warnings "redefine"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 64 | 4 | ▶ | local *Template::Stash::undefined = sub { return undef; }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 65 | |||||||||||
| 66 | # Take only the values that were specified to be checked | ||||||||||
| 67 | %check = map { | ||||||||||
| 68 | 4 | my $key = $_; | |||||||||
| 69 | 12 | my $value = $stash->get( $key ); | |||||||||
| 70 | 12 | ▶ | defined $value ? ( $key => $value ) : (); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | } keys %$params; | ||||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | 4 | my %returned = eval { validated_hash( [%check], %$params ) }; | |||||||||
| 75 | 4 | ▶ | if ( $@ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | # If you're thinking "this is really weird", then yes, you're right. | ||||||||||
| 77 | # Seems to be the right thing to do though. | ||||||||||
| 78 | 1 | $class->error( $@ ); | |||||||||
| 79 | 1 | die $class->error(); | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | # Update the values in the stash if they might have been changed | ||||||||||
| 83 | 3 | for my $key ( keys %returned ) { | |||||||||
| 84 | 9 | $stash->set( $key, $returned{ $key } ); | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 3 | return 1; | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | =head1 AUTHOR | ||||||||||
| 91 | |||||||||||
| 92 | Peter Sergeant - C<pete@clueball.com>, while working for | ||||||||||
| 93 | L<Net-A-Porter|http://www.net-a-porter.com/>. | ||||||||||
| 94 | |||||||||||
| 95 | =cut | ||||||||||
| 96 | |||||||||||
| 97 | 1 | 1; | |||||||||