| 1 | #============================================================= -*-Perl-*- | ||||||||||
| 2 | # | ||||||||||
| 3 | # Template::Plugin::Assert | ||||||||||
| 4 | # | ||||||||||
| 5 | # DESCRIPTION | ||||||||||
| 6 | # Template Toolkit plugin module which allows you to assert that | ||||||||||
| 7 | # items fetches from the stash are defined. | ||||||||||
| 8 | # | ||||||||||
| 9 | # AUTHOR | ||||||||||
| 10 | # Andy Wardley <abw@wardley.org> | ||||||||||
| 11 | # | ||||||||||
| 12 | # COPYRIGHT | ||||||||||
| 13 | # Copyright (C) 2008-2022 Andy Wardley. All Rights Reserved. | ||||||||||
| 14 | # | ||||||||||
| 15 | # This module is free software; you can redistribute it and/or | ||||||||||
| 16 | # modify it under the same terms as Perl itself. | ||||||||||
| 17 | # | ||||||||||
| 18 | #============================================================================ | ||||||||||
| 19 | |||||||||||
| 20 | package Template::Plugin::Assert; | ||||||||||
| 21 | 1 | ▶ | use base 'Template::Plugin'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 1 | ▶ | use Template::Exception; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | |||||||||||
| 26 | 1 | our $VERSION = '3.106'; | |||||||||
| 27 | 1 | our $MONAD = 'Template::Monad::Assert'; | |||||||||
| 28 | 1 | our $EXCEPTION = 'Template::Exception'; | |||||||||
| 29 | 1 | our $AUTOLOAD; | |||||||||
| 30 | |||||||||||
| 31 | 1 | ▶ | sub load { | ||||||||
|
Subroutine
load: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 32 | 1 | my $class = shift; | |||||||||
| 33 | 1 | my $context = shift; | |||||||||
| 34 | 1 | my $stash = $context->stash; | |||||||||
| 35 | 13 | ▶ | my $vmethod = sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 36 | 13 | $MONAD->new($stash, shift); | |||||||||
| 37 | 1 | }; | |||||||||
| 38 | |||||||||||
| 39 | # define .assert vmethods for hash and list objects | ||||||||||
| 40 | 1 | $context->define_vmethod( hash => assert => $vmethod ); | |||||||||
| 41 | 1 | $context->define_vmethod( list => assert => $vmethod ); | |||||||||
| 42 | |||||||||||
| 43 | 1 | return $class; | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 12 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 47 | 12 | my ($class, $context, @args) = @_; | |||||||||
| 48 | # create an assert plugin object which will handle simple variable | ||||||||||
| 49 | # lookups. | ||||||||||
| 50 | 12 | return bless { _CONTEXT => $context }, $class; | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | 3 | ▶ | sub AUTOLOAD { | ||||||||
|
Subroutine
AUTOLOAD: called CC=3 SCAR=11.0
| |||||||||||
| 54 | 3 | my ($self, @args) = @_; | |||||||||
| 55 | 3 | my $item = $AUTOLOAD; | |||||||||
| 56 | 3 | $item =~ s/.*:://; | |||||||||
| 57 | 3 | ▶ | return if $item eq 'DESTROY'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | |||||||||||
| 59 | # lookup the named values | ||||||||||
| 60 | 3 | my $stash = $self->{ _CONTEXT }->stash; | |||||||||
| 61 | 3 | my $value = $stash->dotop($stash, $item, \@args); | |||||||||
| 62 | |||||||||||
| 63 | 3 | ▶ | if (! defined $value) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 2 | die $EXCEPTION->new( assert => "undefined value for $item" ); | |||||||||
| 65 | } | ||||||||||
| 66 | 1 | return $value; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | |||||||||||
| 70 | package Template::Monad::Assert; | ||||||||||
| 71 | |||||||||||
| 72 | 1 | our $EXCEPTION = 'Template::Exception'; | |||||||||
| 73 | 1 | our $AUTOLOAD; | |||||||||
| 74 | |||||||||||
| 75 | 13 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||
| 76 | 13 | my ($class, $stash, $this) = @_; | |||||||||
| 77 | 13 | bless [$stash, $this], $class; | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | 26 | ▶ | sub AUTOLOAD { | ||||||||
|
Subroutine
AUTOLOAD: called CC=3 SCAR=11.0
| |||||||||||
| 81 | 26 | my ($self, @args) = @_; | |||||||||
| 82 | 26 | my ($stash, $this) = @$self; | |||||||||
| 83 | 26 | my $item = $AUTOLOAD; | |||||||||
| 84 | 26 | $item =~ s/.*:://; | |||||||||
| 85 | 26 | ▶ | return if $item eq 'DESTROY'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | |||||||||||
| 87 | 13 | my $value = $stash->dotop($this, $item, \@args); | |||||||||
| 88 | |||||||||||
| 89 | 13 | ▶ | if (! defined $value) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 8 | die $EXCEPTION->new( assert => "undefined value for $item" ); | |||||||||
| 91 | } | ||||||||||
| 92 | 5 | return $value; | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | 1 | 1; | |||||||||
| 96 | |||||||||||
| 97 | __END__ | ||||||||||