X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=lib%2FMaypole%2FCLI.pm;h=3839521e2d8112e03bb8eb9971551a28f691843c;hb=8617321ebf13ce6b63d41a65a003d153f2b5cc5e;hp=fec6f5e8739929565c624f7b9e27499a9ef2abee;hpb=5ba93a09518403ae28e8a71b5299e2458a6dfa0b;p=maypole.git diff --git a/lib/Maypole/CLI.pm b/lib/Maypole/CLI.pm index fec6f5e..3839521 100644 --- a/lib/Maypole/CLI.pm +++ b/lib/Maypole/CLI.pm @@ -9,6 +9,13 @@ use warnings; my $package; our $buffer; +# Command line action +CHECK { + if ( ( caller(0) )[1] eq "-e" ) { + $package->handler() == OK and print $buffer; + } +} + sub import { $package = $_[1]; $package->require; @@ -17,15 +24,15 @@ sub import { unshift @{ $package . "::ISA" }, "Maypole::CLI"; } -sub get_request { } sub get_template_root { $ENV{MAYPOLE_TEMPLATES} || "." } sub parse_location { my $self = shift; my $url = URI->new( shift @ARGV ); - my $root = URI->new( $self->config->{uri_base} )->path; + (my $uri_base = $self->config->uri_base) =~ s:/$::; + my $root = URI->new( $uri_base )->path; $self->{path} = $url->path; - $self->{path} =~ s/^$root//i if $root; + $self->{path} =~ s:^$root/?::i if $root; $self->parse_path; $self->parse_args($url); } @@ -44,12 +51,6 @@ sub call_url { $package->handler() == OK and return $buffer; } -# Do it! -CHECK { - if ( ( caller(0) )[1] eq "-e" ) { - $package->handler() == OK and print $buffer; - } -} 1; @@ -98,7 +99,7 @@ more easily. For instance, a test script could look like this: - use Test::More tests => 5; + use Test::More tests => 3; use Maypole::CLI qw(BeerDB); use Maypole::Constants; $ENV{MAYPOLE_TEMPLATES} = "t/templates"; @@ -106,11 +107,36 @@ For instance, a test script could look like this: # Hack because isa_ok only supports object isa not class isa isa_ok( (bless {},"BeerDB") , "Maypole"); - @ARGV = ("http://localhost/beerdb/"); - is(BeerDB->handler, OK, "OK"); - like($Maypole::CLI::buffer, qr/frontpage/, "Got the front page"); + like(BeerDB->call_url("http://localhost/beerdb/"), qr/frontpage/, "Got the front page"); + + like(BeerDB->call_url("http://localhost/beerdb/beer/list"), qr/Organic Best/, "Found a beer in the list"); + +=head1 METHODS + +=over + +=item call_url + +for use in scripts. takes an url as argument, and returns the buffer. + +=back + + +=head1 Implementation + +This class overrides a set of methods in the base Maypole class to provide it's +functionality. See L for these: + +=over + +=item get_template_root + +=item parse_args + +=item parse_location + +=item send_output - @ARGV = ("http://localhost/beerdb/beer/list"); - is(BeerDB->handler, OK, "OK"); - like($Maypole::CLI::buffer, qr/Organic Best/, "Found a beer in the list"); +=back +=cut