And make it actually work
Tomas Doran [Tue, 27 Sep 2011 15:21:32 +0000 (16:21 +0100)]
lib/CatalystX/HelpText/File.pm [deleted file]
lib/CatalystX/HelpText/Model.pm [new file with mode: 0644]
t/lib/TestApp.pm
t/lib/TestApp/Model/Help.pm [new file with mode: 0644]
t/lib/TestApp/root/helptext/fnoo.html [new file with mode: 0644]
t/live-test.t

diff --git a/lib/CatalystX/HelpText/File.pm b/lib/CatalystX/HelpText/File.pm
deleted file mode 100644 (file)
index 0100d03..0000000
+++ /dev/null
@@ -1,38 +0,0 @@
-package CatalystX::HelpText::File;
-use Moose;
-use MooseX::Types::Moose qw/Str/;
-use Moose::Autobox;
-use Carp qw/confess/;
-use namespace::autoclean;
-
-has help_files_path => (
-    is => 'ro',
-    isa => Str,
-    default => '/support/help/',
-);
-
-has help_files_ext => (
-  is => 'ro',
-  isa => Str,
-  default => '.html',
-);
-
-sub get_help_text_for {
-    my ($self, $c, $help_key) = @_;
-    confess('No $c provided') unless $c;
-    confess('No $help_key provided') unless $help_key;
-
-    my $file_path = $c->path_to('root', $self->help_files_path.$help_key.$self->help_files_ext);
-    my $string = '';
-    if ( -e $file_path ) {
-        local $/=undef;
-        open FILE, $file_path or confess "Couldn't open file: $!";
-        $string = <FILE>;
-        close FILE;
-    }
-
-    return $string;
-}
-
-1;
-
diff --git a/lib/CatalystX/HelpText/Model.pm b/lib/CatalystX/HelpText/Model.pm
new file mode 100644 (file)
index 0000000..2ed4adc
--- /dev/null
@@ -0,0 +1,42 @@
+package CatalystX::HelpText::Model;
+use Moose;
+use MooseX::Types::Moose qw/Str Undef/;
+use MooseX::Types::Path::Class qw/ Dir /;
+use Moose::Autobox;
+use Carp qw/ croak confess/;
+use namespace::autoclean;
+
+extends 'Catalyst::Model';
+
+has help_files_path => (
+    is => 'ro',
+    isa => Dir,
+    coerce => 1,
+    required => 1,
+    handles => {
+        _get_file => 'file',
+    }
+);
+
+has help_files_ext => (
+    is => 'ro',
+    isa => Str|Undef,
+    default => 'html',
+);
+
+sub get_help_text_for {
+    my ($self, $help_key) = @_;
+    confess('No $help_key provided') unless $help_key;
+
+    my $fn = $help_key;
+    $fn .= "." . $self->help_files_ext if defined($self->help_files_ext);
+
+    my $file = $self->_get_file($fn);
+
+    return $file->slurp if ( -e $file );
+
+    croak "Cannot find help text '$help_key' in $file";
+}
+
+1;
+
index a18a1ef..0d9f176 100644 (file)
@@ -6,6 +6,7 @@ use Catalyst;
 
 extends 'Catalyst';
 
+__PACKAGE__->config('Model::Help' => { help_files_path => __PACKAGE__->path_to(qw/ root helptext /) });
 __PACKAGE__->setup;
 
 1;
diff --git a/t/lib/TestApp/Model/Help.pm b/t/lib/TestApp/Model/Help.pm
new file mode 100644 (file)
index 0000000..20e0b55
--- /dev/null
@@ -0,0 +1,8 @@
+package TestApp::Model::Help;
+use Moose;
+use namespace::autoclean;
+
+extends 'CatalystX::HelpText::Model';
+
+1;
+
diff --git a/t/lib/TestApp/root/helptext/fnoo.html b/t/lib/TestApp/root/helptext/fnoo.html
new file mode 100644 (file)
index 0000000..a5906a1
--- /dev/null
@@ -0,0 +1 @@
+blargh
\ No newline at end of file
index 4ca1c81..f4f905c 100644 (file)
@@ -15,6 +15,6 @@ use ok 'TestApp';
 use Test::WWW::Mechanize::Catalyst 'TestApp';
 my $mech = Test::WWW::Mechanize::Catalyst->new;
 $mech->get_ok('http://localhost/', 'get main page');
-$mech->content_like(qr/it works/i, 'see if it has our text');
+$mech->content_like(qr/fnarblarghfnee/i, 'see if it has our text');
 
 done_testing;