Better error handling
[catagits/CatalystX-HelpText.git] / lib / CatalystX / HelpText / ViewRole.pm
CommitLineData
c8c4f056 1package CatalystX::HelpText::ViewRole;
2use Moose::Role;
3use Try::Tiny;
030fd7f2 4use Moose::Autobox;
5use namespace::autoclean;
c8c4f056 6
7requires 'expose_methods';
8
9around expose_methods => sub {
10 my ($orig, $self, @args) = @_;
030fd7f2 11 my $m = $self->$orig(@args) || [];
12 [ $m->flatten, 'help_text' ];
c8c4f056 13};
14
72bb1615 15has helptext_model => (
16 is => 'ro',
17 default => 'Help',
18);
19
c8c4f056 20sub help_text {
21 my ($self, $c, $key) = @_;
72bb1615 22 my $model = try { $c->model($self->helptext_model) };
23 if (!$model) {
24 $c->log->warn(sprintf("Cannot find the '%s' model for %s", $self->helptext_model, ref($self))) if $c->debug;
25 return;
26 }
27 if (!$model->can('get_help_text_for')) {
28 $c->log->warn(sprintf("Your '%s' model used by '%s' does not have a get_help_text_for method", $self->helptext_model, ref($self))) if $c->debug;
29 return;
30 }
c8c4f056 31 try {
72bb1615 32 $model->get_help_text_for($key);
c8c4f056 33 }
34 catch {
72bb1615 35 $c->log->warn("Error retrieving help_text key '$key' from model: $_")
36 if $c->debug;
c8c4f056 37 return '';
38 };
39}
40
411;
42
43=head1 NAME
44
45CatalystX::HelpText::ViewRole - Role to be applied to Views
46
47=head1 SYNOPSIS
48
49 package MyApp::View::HTML;
50 use Moose;
51
52 extends 'Catalyst::View::TT';
53 with 'CatalystX::HelpText::ViewRole';
54
55 ... then, in your template code ...
56
57 [% helptext('SomeHelpTopic') %]
58
59=cut
60