Add built local::lib
[catagits/Gitalist.git] / local-lib5 / lib / perl5 / i486-linux-gnu-thread-multi / Template / Plugin / Assert.pm
1 #============================================================= -*-Perl-*-
2 #
3 # Template::Plugin::Assert
4 #
5 # DESCRIPTION
6 #   Template Toolkit plugin module which allows you to assert that
7 #   items fetchs from the stash are defined.
8 #
9 # AUTHOR
10 #   Andy Wardley   <abw@wardley.org>
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 use base 'Template::Plugin';
22 use strict;
23 use warnings;
24 use Template::Exception;
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     my $class   = shift;
33     my $context = shift;
34     my $stash   = $context->stash;
35     my $vmethod = sub {
36         $MONAD->new($stash, shift);
37     };
38
39     # define .assert vmethods for hash and list objects
40     $context->define_vmethod( hash => assert => $vmethod );
41     $context->define_vmethod( list => assert => $vmethod );
42
43     return $class;
44 }
45
46 sub new {
47     my ($class, $context, @args) = @_;
48     # create an assert plugin object which will handle simple variable
49     # lookups.
50     return bless { _CONTEXT => $context }, $class;
51 }
52
53 sub AUTOLOAD {
54     my ($self, @args) = @_;
55     my $item = $AUTOLOAD;
56     $item =~ s/.*:://;
57     return if $item eq 'DESTROY';
58     
59     # lookup the named values
60     my $stash = $self->{ _CONTEXT }->stash;
61     my $value = $stash->dotop($stash, $item, \@args);
62
63     if (! defined $value) {
64         die $EXCEPTION->new( assert => "undefined value for $item" );
65     }
66     return $value;
67 }
68
69
70 package Template::Monad::Assert;
71
72 our $EXCEPTION = 'Template::Exception';
73 our $AUTOLOAD;
74
75 sub new {
76     my ($class, $stash, $this) = @_;
77     bless [$stash, $this], $class;
78 }
79
80 sub AUTOLOAD {
81     my ($self, @args) = @_;
82     my ($stash, $this) = @$self;
83     my $item = $AUTOLOAD;
84     $item =~ s/.*:://;
85     return if $item eq 'DESTROY';
86
87     my $value = $stash->dotop($stash, $item, \@args);
88
89     if (! defined $value) {
90         die $EXCEPTION->new( assert => "undefined value for $item" );
91     }
92     return $value;
93 }
94
95 1;
96
97 __END__
98
99 =head1 NAME
100
101 Template::Plugin::Assert - trap undefined values
102
103 =head1 SYNOPSIS
104
105     [% USE assert %]
106     
107     # throws error if any undefined values are returned
108     [% object.assert.method %]
109     [% hash.assert.key %]
110     [% list.assert.item %]
111
112 =head1 DESCRIPTION
113
114 This plugin defines the C<assert> virtual method that can be used
115 to automatically throw errors when undefined values are used.
116
117 For example, consider this dotop:
118
119     [% user.name %]
120
121 If C<user.name> is an undefined value then TT will silently ignore the 
122 fact and print nothing.  If you C<USE> the C<assert> plugin then you
123 can add the C<assert> vmethod between the C<user> and C<name> elements,
124 like so:
125
126     [% user.assert.name %]
127
128 Now, if C<user.name> is an undefined value, an exception will be thrown:
129
130     assert error - undefined value for name
131
132 =head1 AUTHOR
133
134 Andy Wardley E<lt>abw@wardley.orgE<gt> L<http://wardley.org/>
135
136 =head1 COPYRIGHT
137
138 Copyright (C) 2008 Andy Wardley.  All Rights Reserved.
139
140 This module is free software; you can redistribute it and/or
141 modify it under the same terms as Perl itself.
142
143 =head1 SEE ALSO
144
145 L<Template::Plugin>
146
147 =cut
148
149 # Local Variables:
150 # mode: perl
151 # perl-indent-level: 4
152 # indent-tabs-mode: nil
153 # End:
154 #
155 # vim: expandtab shiftwidth=4: