initial checkin
[gitmo/MooseX-AutoDoc.git] / lib / MooseX / AutoDoc / View / TT.pm
CommitLineData
3890b670 1package MooseX::AutoDoc::View::TT;
2
3use Moose;
4use Scalar::Util qw/blessed/;
5use Template;
6
7extends 'MooseX::AutoDoc::View';
8
9has _tt => (is => 'ro', isa => 'Template', lazy_build => 1);
10has '+args' => (isa => 'HashRef');
11
12sub _build__tt {
13 my $self = shift;
14 Template->new($self->has_args ? $self->args : {})
15 || confess $Template::ERROR;
16}
17
18has role_template => (is => 'rw', isa => 'Str', lazy_build => 1);
19has class_template => (is => 'rw', isa => 'Str', lazy_build => 1);
20
21has role_template_blocks => (is => 'rw', isa => 'HashRef', lazy_build => 1);
22has class_template_blocks => (is => 'rw', isa => 'HashRef', lazy_build => 1);
23
24sub _build_role_template { "[% USE wrap; PROCESS role_block; %]" }
25sub _build_class_template { "[% USE wrap; PROCESS class_block; %]" }
26
27sub render_role {
28 my ($self, $vars, $options) = @_;
29 my $tt = $self->_tt;
30 my $output;
31 my $template = $self->role_template. " ".$self->_role_blocks;
32 $tt->process(\ $template, $vars, \ $output, %{ $options || {}})
33 || confess $tt->error;
34 return $output;
35}
36
37sub render_class {
38 my ($self, $vars, $options) = @_;
39 my $tt = $self->_tt;
40 my $output;
41 my $template = $self->class_template. " ".$self->_class_blocks;
42 $tt->process(\ $template, $vars, \ $output, %{ $options || {}})
43 || confess $tt->error;
44 return $output;
45}
46
47
48sub _class_blocks {
49 my $self= shift;
50 my $blocks = $self->class_template_blocks;
51 return join "",
52 map { " [%- BLOCK ${_} %] ".$blocks->{$_}." [% END; -%] "}
53 keys %$blocks;
54}
55
56sub _role_blocks {
57 my $self= shift;
58 my $blocks = $self->role_template_blocks;
59 return join "",
60 map { " [%- BLOCK ${_} %] ".$blocks->{$_}." [% END; -%] "}
61 keys %$blocks;
62}
63
641;
65
66sub _build_class_template_blocks{
67 my $blocks = {};
68 $blocks->{name_block} = q^
69=head1 NAME
70
71[% class.name %]
72^;
73
74 $blocks->{synopsys_block} = q^
75=head1 SYNOPSYS
76
77 use [% class.name %];
78 #TODO
79 [% class.name %]->new();
80^;
81
82 $blocks->{description_block} = q^
83=head1 DESCRIPTION
84
85[%- IF class.superclasses.size == 1 %]
86[% 'This class is a subclass of L<' _ class.superclasses.first.name _'>' _
87' and inherits all it\'s methods and attributes.' FILTER wrap(80,'','') %]
88
89[%- ELSIF class.superclasses.size > 1 %]
90This class is a subclass of the following classes and inherits all their
91methods and attributes;
92
93=over 4
94[%- FOREACH superclass = class.superclasses %]
95
96=item L<[% superclass.name %]>
97[%- END %]
98
99=back
100
101[%- END -%]
102^;
103
104 $blocks->{roles_consumed_block} = q^
105[%- IF class.roles.size %]
106=head1 ROLES CONSUMED
107
108The following roles are consumed by this class. Unless otherwise indicated, all
109methods, modifiers and attributes present in those roles are applied to this
110class.
111
112=over 4
113[% FOREACH role_consumed = class.roles;
114 PROCESS role_consumed_block;
115 END %]
116=back
117[% END -%]
118^;
119
120 $blocks->{role_consumed_block} = q^
121=item L<[% role_consumed.name %]>
122^;
123
124 $blocks->{attributes_block} = q^
125[%- IF class.attributes.size %]
126=head1 ATTRIBUTES
127
128Unless noted otherwise, you may set any of these attributes at C<new> time by
129passing key / value pairs to C<new> where the key is the name of the attribute
130you wish to set. Unless noted otherwise accessor methods for attributes also
131share the same name as the attribute.
132[%
133 FOREACH attribute = class.attributes;
134 PROCESS attribute_block;
135 END;
136END;
137-%]
138^;
139
140 $blocks->{attribute_block} = q^
141=head2 [% attribute.name %]
142[%- IF attribute.info.size %]
143
144=over 4
145[% FOREACH pair IN attribute.info.pairs %]
146=item B<[% pair.key %]> - [% pair.value %]
147[% END %]
148=back
149[%- END; %]
150
151[% attribute.description FILTER wrap(80, '',''); %]
152^;
153
154 $blocks->{methods_block} = q^
155=head1 METHODS
156
157=head2 new $key => $value
158
159Instantiate a new object. Please refer to L</"ATTRIBUTES"> for a list of valid
160key options.
161[%
162 FOREACH method = class.methods;
163 PROCESS method_block;
164 END;
165%]
166=head2 meta
167
168Retrieve the metaclass instance. Please see L<Moose::Meta::Class> and
169L<Class::MOP::Class> for more information.
170^;
171
172 $blocks->{method_block} = q^
173=head2 [% method.name %]
174
175Description of [% method.name %]
176^;
177
178 $blocks->{authors_block} = q^
179=head1 AUTHORS
180[%
181 FOREACH author = authors;
182 PROCESS author_block;
183 END;
184-%]
185^;
186
187 $blocks->{author_block} = q^
188[%
189IF author.name.length; author.name _ ' '; END;
190IF author.handle.length; '(' _ author.name _ ') '; END;
191IF author.email.length; '<' _ author.email _ '>'; END;
192%]
193^;
194
195 $blocks->{license_block} = q^
196=head1 COPYRIGHT AND LICENSE
197
198[% license FILTER wrap(80, '', '') %]
199^;
200
201 $blocks->{class_block} = q^
202[%
203PROCESS name_block;
204PROCESS synopsys_block;
205PROCESS description_block;
206PROCESS roles_consumed_block;
207PROCESS attributes_block;
208PROCESS methods_block;
209PROCESS authors_block;
210PROCESS license_block;
211%]
212=cut
213^;
214
215 return $blocks;
216}
217
218sub _build_role_template_blocks{
219 my $blocks = {};
220
221 $blocks->{name_block} = q^
222=head1 NAME
223
224[% role.name %]
225^;
226
227 $blocks->{synopsys_block} = q^
228=head1 SYNOPSYS
229
230 use Moose;
231 with '[% role.name %]';
232^;
233
234 $blocks->{description_block} = q^
235=head1 DESCRIPTION
236
237When consumed, this role will apply to the consuming class all the methods,
238method modifiers, and attributes it is composed of.
239^;
240
241 $blocks->{roles_consumed_block} = q^
242[%- IF role.roles.size %]
243=head1 ROLES CONSUMED
244
245The following roles are consumed by this role. Unless otherwise indicated, all
246methods, modifiers and attributes present in those roles will also be applied
247to any class or role consuming this role.
248
249=over 4
250[% FOREACH role_consumed = role.roles;
251 PROCESS role_consumed_block;
252 END %]
253=back
254[% END -%]
255^;
256
257 $blocks->{role_consumed_block} = q^
258=item L<[% role_consumed.name %]>
259^;
260
261 $blocks->{attributes_block} = q^
262[%- IF role.attributes.size %]
263=head1 ATTRIBUTES
264
265Unless noted otherwise, you may set any of these attributes on consuming
266classes at C<new()> time by passing key / value pairs to C<new> where the key
267is the name of the attribute you wish to set. Unless noted otherwise accessor
268methods for attributes also share the same name as the attribute.
269[%
270 FOREACH attribute = role.attributes;
271 PROCESS attribute_block;
272 END;
273END;
274-%]
275^;
276
277 $blocks->{attribute_block} = q^
278=head2 [% attribute.name %]
279[%- IF attribute.info.size %]
280
281=over 4
282[% FOREACH pair IN attribute.info.pairs %]
283=item B<[% pair.key %]> - [% pair.value %]
284[% END %]
285=back
286[%- END; %]
287
288[% attribute.description FILTER wrap(80, '',''); %]
289^;
290
291 $blocks->{methods_block} = q^
292=head1 METHODS
293[%
294 FOREACH method = role.methods;
295 PROCESS method_block;
296 END;
297%]
298=head2 meta
299
300Retrieve the role metaclass instance. Please see L<Moose::Meta::Role>;
301^;
302
303 $blocks->{method_block} = q^
304=head2 [% method.name %]
305
306Description of [% method.name %]
307^;
308
309 $blocks->{authors_block} = q^
310=head1 AUTHORS
311[%
312 FOREACH author = authors;
313 PROCESS author_block;
314 END;
315-%]
316^;
317
318 $blocks->{author_block} = q^
319[%
320IF author.name.length; author.name _ ' '; END;
321IF author.handle.length; '(' _ author.name _ ') '; END;
322IF author.email.length; '<' _ author.email _ '> '; END;
323%]
324^;
325
326 $blocks->{license_block} = q^
327=head1 COPYRIGHT AND LICENSE
328
329[% license FILTER wrap(80, '', '') %]
330^;
331
332 $blocks->{role_block} = q^
333[%
334PROCESS name_block;
335PROCESS synopsys_block;
336PROCESS description_block;
337PROCESS roles_consumed_block;
338PROCESS attributes_block;
339PROCESS methods_block;
340PROCESS authors_block;
341PROCESS license_block;
342%]
343=cut
344^;
345
346 return $blocks;
347}
348
3491;
350
351__END__;