Fix bug breaking content output
[catagits/CatalystX-HelpText.git] / lib / CatalystX / HelpText / Model.pm
CommitLineData
f22d4e6f 1package CatalystX::HelpText::Model;
2use Moose;
3use MooseX::Types::Moose qw/Str Undef/;
4use MooseX::Types::Path::Class qw/ Dir /;
5use Moose::Autobox;
6use Carp qw/ croak confess/;
7use namespace::autoclean;
8
9extends 'Catalyst::Model';
10
11has help_files_path => (
12 is => 'ro',
13 isa => Dir,
14 coerce => 1,
15 required => 1,
16 handles => {
17 _get_file => 'file',
18 }
19);
20
21has help_files_ext => (
22 is => 'ro',
23 isa => Str|Undef,
24 default => 'html',
25);
26
69ca94ae 27has wrapper_css_class => (
28 is => 'ro',
29 isa => Str,
30 default => 'help_text',
31);
32
33has wrapper_tag => (
34 is => 'ro',
35 isa => Str,
36 default => 'span',
37);
38
39
f22d4e6f 40sub get_help_text_for {
41 my ($self, $help_key) = @_;
42 confess('No $help_key provided') unless $help_key;
43
44 my $fn = $help_key;
45 $fn .= "." . $self->help_files_ext if defined($self->help_files_ext);
46
47 my $file = $self->_get_file($fn);
48
69ca94ae 49 if ( -e $file ) {
511880df 50 my $content = $file->slurp;
69ca94ae 51 return sprintf('<%s class="%s">%s</%s>',
52 $self->wrapper_tag,
53 $self->wrapper_css_class,
511880df 54 $content,
69ca94ae 55 $self->wrapper_tag
56 );
57 }
f22d4e6f 58
59 croak "Cannot find help text '$help_key' in $file";
60}
61
be01cec8 62__PACKAGE__->meta->make_immutable;
f22d4e6f 631;
64
be01cec8 65=head1 NAME
66
67CatalystX::HelpText::Model - Model to provide snippets of help text
68
69=head1 SYNOPSIS
70
71 package MyApp::Model::Help;
72 use Moose;
73 use namespace::autoclean;
74
75 extends 'CatalystX::HelpText::Model';
76
77 1;
78
79=head1 DESCRIPTION
80
81A very simple file system based help text finder.
82
83Expects each piece of help text to be in an individual file, and provides a
84C<get_help_text_for> method as used by L<CatalaystX::HelpText::ViewRole> which
85just slurps in this file verbatim.
86
87Users are expected (and encouraged) to replace the simple logic in this
88model with a more complicated method of help text resolution if needed.
89
90=head1 METHODS
91
92=head1 get_help_text_for ($help_key)
93
94Returns a snippet of help text.
95
96Will throw an exception if the requested help key is not found.
97
98=head1 CONFIGURATION ATTRIBUTES
99
100XXX - FIXME - document these
101
102=head2 help_files_path
103
104=head2 help_files_ext
105
106=head2 wrapper_css_class
107
108=head2 wrapper_tag
109
110=head1 BUGS
111
112=head2 File system only.
113
114Currently we only support getting help texts from the file system (as individual files).
115
116A model to get help from a database table, or YAML file or similar could (and should)
117be provided as part of this distribution. Patches are welcome!
118
119=head1 SEE ALSO
120
121=over
122
123=item L<CatalystX::HelpText>
124
125=back
126
127=head1 AUTHOR
128
129Toomas Doran, C<< t0m at state51.co.uk >>
130
131Cinxgler Mariaca Minda, C<< cinxgler at ci-info.com >>
132
133=head1 COPYRIGHT
134
135Copyright Oscar Music and Media 2011.
136
137=head1 LICENSE
138
139This sofware is free software, and is licensed under the same terms as perl itself.
140
141=cut
142