line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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 |
11
|
|
|
|
|
|
|
# |
12
|
|
|
|
|
|
|
# COPYRIGHT |
13
|
|
|
|
|
|
|
# Copyright (C) 2008 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
|
|
|
1
|
|
4
|
use base 'Template::Plugin'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
411
|
|
22
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
27
|
|
23
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
20
|
|
24
|
1
|
|
|
1
|
|
5
|
use Template::Exception; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
467
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
our $VERSION = 1.00; |
27
|
|
|
|
|
|
|
our $MONAD = 'Template::Monad::Assert'; |
28
|
|
|
|
|
|
|
our $EXCEPTION = 'Template::Exception'; |
29
|
|
|
|
|
|
|
our $AUTOLOAD; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub load { |
32
|
1
|
|
|
1
|
1
|
2
|
my $class = shift; |
33
|
1
|
|
|
|
|
1
|
my $context = shift; |
34
|
1
|
|
|
|
|
4
|
my $stash = $context->stash; |
35
|
|
|
|
|
|
|
my $vmethod = sub { |
36
|
8
|
|
|
8
|
|
110
|
$MONAD->new($stash, shift); |
37
|
1
|
|
|
|
|
4
|
}; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# define .assert vmethods for hash and list objects |
40
|
1
|
|
|
|
|
6
|
$context->define_vmethod( hash => assert => $vmethod ); |
41
|
1
|
|
|
|
|
3
|
$context->define_vmethod( list => assert => $vmethod ); |
42
|
|
|
|
|
|
|
|
43
|
1
|
|
|
|
|
3
|
return $class; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub new { |
47
|
6
|
|
|
6
|
1
|
10
|
my ($class, $context, @args) = @_; |
48
|
|
|
|
|
|
|
# create an assert plugin object which will handle simple variable |
49
|
|
|
|
|
|
|
# lookups. |
50
|
6
|
|
|
|
|
42
|
return bless { _CONTEXT => $context }, $class; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub AUTOLOAD { |
54
|
2
|
|
|
2
|
|
5
|
my ($self, @args) = @_; |
55
|
2
|
|
|
|
|
4
|
my $item = $AUTOLOAD; |
56
|
2
|
|
|
|
|
10
|
$item =~ s/.*:://; |
57
|
2
|
50
|
|
|
|
6
|
return if $item eq 'DESTROY'; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# lookup the named values |
60
|
2
|
|
|
|
|
12
|
my $stash = $self->{ _CONTEXT }->stash; |
61
|
2
|
|
|
|
|
10
|
my $value = $stash->dotop($stash, $item, \@args); |
62
|
|
|
|
|
|
|
|
63
|
2
|
50
|
|
|
|
6
|
if (! defined $value) { |
64
|
2
|
|
|
|
|
13
|
die $EXCEPTION->new( assert => "undefined value for $item" ); |
65
|
|
|
|
|
|
|
} |
66
|
0
|
|
|
|
|
0
|
return $value; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
package Template::Monad::Assert; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
our $EXCEPTION = 'Template::Exception'; |
73
|
|
|
|
|
|
|
our $AUTOLOAD; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub new { |
76
|
8
|
|
|
8
|
|
15
|
my ($class, $stash, $this) = @_; |
77
|
8
|
|
|
|
|
149
|
bless [$stash, $this], $class; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub AUTOLOAD { |
81
|
8
|
|
|
8
|
|
13
|
my ($self, @args) = @_; |
82
|
8
|
|
|
|
|
17
|
my ($stash, $this) = @$self; |
83
|
8
|
|
|
|
|
10
|
my $item = $AUTOLOAD; |
84
|
8
|
|
|
|
|
34
|
$item =~ s/.*:://; |
85
|
8
|
50
|
|
|
|
18
|
return if $item eq 'DESTROY'; |
86
|
|
|
|
|
|
|
|
87
|
8
|
|
|
|
|
32
|
my $value = $stash->dotop($stash, $item, \@args); |
88
|
|
|
|
|
|
|
|
89
|
8
|
50
|
|
|
|
18
|
if (! defined $value) { |
90
|
8
|
|
|
|
|
43
|
die $EXCEPTION->new( assert => "undefined value for $item" ); |
91
|
|
|
|
|
|
|
} |
92
|
0
|
|
|
|
|
|
return $value; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
1; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
__END__ |