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
|
|
|
|
|
284
|
|
22
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
13
|
|
23
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
17
|
|
24
|
1
|
|
|
1
|
|
3
|
use Template::Exception; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
338
|
|
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
|
1
|
my $class = shift; |
33
|
1
|
|
|
|
|
1
|
my $context = shift; |
34
|
1
|
|
|
|
|
3
|
my $stash = $context->stash; |
35
|
|
|
|
|
|
|
my $vmethod = sub { |
36
|
8
|
|
|
8
|
|
70
|
$MONAD->new($stash, shift); |
37
|
1
|
|
|
|
|
3
|
}; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# define .assert vmethods for hash and list objects |
40
|
1
|
|
|
|
|
4
|
$context->define_vmethod( hash => assert => $vmethod ); |
41
|
1
|
|
|
|
|
2
|
$context->define_vmethod( list => assert => $vmethod ); |
42
|
|
|
|
|
|
|
|
43
|
1
|
|
|
|
|
2
|
return $class; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub new { |
47
|
6
|
|
|
6
|
1
|
6
|
my ($class, $context, @args) = @_; |
48
|
|
|
|
|
|
|
# create an assert plugin object which will handle simple variable |
49
|
|
|
|
|
|
|
# lookups. |
50
|
6
|
|
|
|
|
24
|
return bless { _CONTEXT => $context }, $class; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub AUTOLOAD { |
54
|
7
|
|
|
7
|
|
9
|
my ($self, @args) = @_; |
55
|
7
|
|
|
|
|
7
|
my $item = $AUTOLOAD; |
56
|
7
|
|
|
|
|
25
|
$item =~ s/.*:://; |
57
|
7
|
100
|
|
|
|
31
|
return if $item eq 'DESTROY'; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# lookup the named values |
60
|
2
|
|
|
|
|
9
|
my $stash = $self->{ _CONTEXT }->stash; |
61
|
2
|
|
|
|
|
6
|
my $value = $stash->dotop($stash, $item, \@args); |
62
|
|
|
|
|
|
|
|
63
|
2
|
50
|
|
|
|
5
|
if (! defined $value) { |
64
|
2
|
|
|
|
|
9
|
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
|
|
11
|
my ($class, $stash, $this) = @_; |
77
|
8
|
|
|
|
|
91
|
bless [$stash, $this], $class; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub AUTOLOAD { |
81
|
16
|
|
|
16
|
|
44
|
my ($self, @args) = @_; |
82
|
16
|
|
|
|
|
18
|
my ($stash, $this) = @$self; |
83
|
16
|
|
|
|
|
15
|
my $item = $AUTOLOAD; |
84
|
16
|
|
|
|
|
52
|
$item =~ s/.*:://; |
85
|
16
|
100
|
|
|
|
43
|
return if $item eq 'DESTROY'; |
86
|
|
|
|
|
|
|
|
87
|
8
|
|
|
|
|
41
|
my $value = $stash->dotop($stash, $item, \@args); |
88
|
|
|
|
|
|
|
|
89
|
8
|
50
|
|
|
|
16
|
if (! defined $value) { |
90
|
8
|
|
|
|
|
27
|
die $EXCEPTION->new( assert => "undefined value for $item" ); |
91
|
|
|
|
|
|
|
} |
92
|
0
|
|
|
|
|
|
return $value; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
1; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
__END__ |