Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
added experimental mojo_lib_dir and slurp_rel_file methods to Mojo::Home
  • Loading branch information
kraih committed Sep 11, 2011
1 parent b9ef7c0 commit feda70b
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 45 deletions.
4 changes: 3 additions & 1 deletion Changes
@@ -1,6 +1,8 @@
This file documents the revision history for Perl extension Mojolicious.

1.98 2011-09-09 00:00:00
1.98 2011-09-11 00:00:00
- Added EXPERIMENTAL mojo_lib_dir and slurp_rel_file methods to
Mojo::Home.
- Improved host condition to work in more environments.
- Improved CSS of all built in templates.
- Improved documentation. (rhaen, sri)
Expand Down
38 changes: 36 additions & 2 deletions lib/Mojo/Home.pm
Expand Up @@ -6,16 +6,24 @@ use overload
fallback => 1;

use Cwd 'abs_path';
use File::Basename 'dirname';
use File::Find 'find';
use File::Spec;
use FindBin;
use Mojo::Asset::File;
use Mojo::Command;
use Mojo::Loader;

has app_class => 'Mojo::HelloWorld';

# "I'm normally not a praying man, but if you're up there,
# please save me Superman."
sub new {
my $self = shift->SUPER::new();
$self->parse(@_) if @_;
return $self;
}

sub detect {
my ($self, $class) = @_;

Expand Down Expand Up @@ -97,10 +105,11 @@ sub list_files {
return [sort @files];
}

sub mojo_lib_dir { File::Spec->catdir(dirname(__FILE__), '..') }

sub parse {
my ($self, $path) = @_;
my @parts = File::Spec->splitdir($path);
$self->{parts} = \@parts;
$self->{parts} = [File::Spec->splitdir($path)];
return $self;
}

Expand All @@ -116,6 +125,10 @@ sub rel_file {
File::Spec->catfile(@$parts, split '/', shift);
}

sub slurp_rel_file {
Mojo::Asset::File->new(path => shift->rel_file(@_))->slurp;
}

sub to_string {
my $self = shift;
my $parts = $self->{parts} || [];
Expand Down Expand Up @@ -156,6 +169,13 @@ Application class.
L<Mojo::Home> inherits all methods from L<Mojo::Base> and implements the
following new ones.
=head2 C<new>
my $home = Mojo::Home->new;
my $home = Mojo::Home->new('/foo/bar');
Construct a new L<Mojo::Home> object.
=head2 C<detect>
$home = $home->detect;
Expand All @@ -176,6 +196,13 @@ Path to C<lib> directory.
List all files in directory and subdirectories recursively.
=head2 C<mojo_lib_dir>
my $path = $home->mojo_lib_dir;
Path to C<lib> directory in which L<Mojolicious> is installed.
Note that this method is EXPERIMENTAL and might change without warning!
=head2 C<parse>
$home = $home->parse('/foo/bar');
Expand All @@ -194,6 +221,13 @@ Generate absolute path for relative directory.
Generate absolute path for relative file.
=head2 C<slurp_rel_file>
my $string = $home->slurp_rel_file('foo/bar.html');
Read all file data at once.
Note that this method is EXPERIMENTAL and might change without warning!
=head2 C<to_string>
my $string = $home->to_string;
Expand Down
31 changes: 8 additions & 23 deletions lib/Mojolicious/Controller.pm
@@ -1,44 +1,29 @@
package Mojolicious::Controller;
use Mojo::Base -base;

use Mojo::Asset::File;
use Mojo::ByteStream;
use Mojo::Cookie::Response;
use Mojo::Exception;
use Mojo::Home;
use Mojo::Transaction::HTTP;
use Mojo::URL;
use Mojo::Util;

require Carp;
require File::Basename;
require File::Spec;

# "Scalpel... blood bucket... priest."
has [qw/app match/];
has tx => sub { Mojo::Transaction::HTTP->new };

# Template directory
my $T = File::Spec->catdir(File::Basename::dirname(__FILE__), 'templates');

# Exception template
our $EXCEPTION =
Mojo::Asset::File->new(path => File::Spec->catfile($T, 'exception.html.ep'))
->slurp;

# Exception template (development)
# Bundled files
my $H = Mojo::Home->new;
$H->parse($H->parse($H->mojo_lib_dir)->rel_dir('Mojolicious/templates'));
our $EXCEPTION = $H->slurp_rel_file('exception.html.ep');
our $DEVELOPMENT_EXCEPTION =
Mojo::Asset::File->new(
path => File::Spec->catfile($T, 'exception.development.html.ep'))->slurp;

# Not found template
our $NOT_FOUND =
Mojo::Asset::File->new(path => File::Spec->catfile($T, 'not_found.html.ep'))
->slurp;

# Not found template (development)
$H->slurp_rel_file('exception.development.html.ep');
our $NOT_FOUND = $H->slurp_rel_file('not_found.html.ep');
our $DEVELOPMENT_NOT_FOUND =
Mojo::Asset::File->new(
path => File::Spec->catfile($T, 'not_found.development.html.ep'))->slurp;
$H->slurp_rel_file('not_found.development.html.ep');

# Reserved stash values
my @RESERVED = (
Expand Down
20 changes: 6 additions & 14 deletions lib/Mojolicious/Plugin/PODRenderer.pm
@@ -1,12 +1,11 @@
package Mojolicious::Plugin::PODRenderer;
use Mojo::Base 'Mojolicious::Plugin';

use File::Basename 'dirname';
use File::Spec;
use IO::File;
use Mojo::Asset::File;
use Mojo::ByteStream 'b';
use Mojo::DOM;
use Mojo::Home;
use Mojo::Util 'url_escape';

# Core module since Perl 5.9.3, so it might not always be present
Expand All @@ -21,18 +20,11 @@ use Pod::Simple::Search;
# Paths
our @PATHS = map { $_, "$_/pods" } @INC;

# Template directory
my $T = File::Spec->catdir(dirname(__FILE__), '..', 'templates');

# Mojobar template
our $MOJOBAR =
Mojo::Asset::File->new(path => File::Spec->catfile($T, 'mojobar.html.ep'))
->slurp;

# Perldoc template
our $PERLDOC =
Mojo::Asset::File->new(path => File::Spec->catfile($T, 'perldoc.html.ep'))
->slurp;
# Bundled files
my $H = Mojo::Home->new;
$H->parse($H->parse($H->mojo_lib_dir)->rel_dir('Mojolicious/templates'));
our $MOJOBAR = $H->slurp_rel_file('mojobar.html.ep');
our $PERLDOC = $H->slurp_rel_file('perldoc.html.ep');

# "This is my first visit to the Galaxy of Terror and I'd like it to be a
# pleasant one."
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojolicious/Static.pm
@@ -1,12 +1,12 @@
package Mojolicious::Static;
use Mojo::Base -base;

use File::Basename 'dirname';
use File::Spec;
use Mojo::Asset::File;
use Mojo::Asset::Memory;
use Mojo::Command;
use Mojo::Content::Single;
use Mojo::Home;
use Mojo::Path;

has [qw/default_static_class root/];
Expand Down Expand Up @@ -53,8 +53,8 @@ sub serve {
my $ext = $1;

# Root for bundled files
$self->{bundled}
||= File::Spec->catdir(File::Spec->splitdir(dirname(__FILE__)), 'public');
$self->{bundled} ||= Mojo::Home->new(Mojo::Home->mojo_lib_dir)
->rel_dir('Mojolicious/public');

# Normal file
my $asset;
Expand Down
12 changes: 10 additions & 2 deletions t/mojo/home.t
@@ -1,7 +1,7 @@
#!/usr/bin/env perl
use Mojo::Base -strict;

use Test::More tests => 13;
use Test::More tests => 16;

use Cwd qw/cwd realpath/;
use File::Spec;
Expand Down Expand Up @@ -42,7 +42,7 @@ is_deeply [split /\\|\//,
[split /\\|\//, $home], 'right path detected';

# Path generation
$home = Mojo::Home->new->parse($FindBin::Bin);
$home = Mojo::Home->new($FindBin::Bin);
is $home->lib_dir,
File::Spec->catdir(File::Spec->splitdir($FindBin::Bin), 'lib'),
'right path';
Expand All @@ -66,3 +66,11 @@ is first(sub { $_ =~ /Base2\.pm$/ }, @{$home->list_files('lib')}),
'BaseTest/Base2.pm', 'right result';
is first(sub { $_ =~ /Base3\.pm$/ }, @{$home->list_files('lib')}),
'BaseTest/Base3.pm', 'right result';

# Slurp files
like $home->slurp_rel_file('lib/BaseTest/Base1.pm'), qr/Base1/,
'right content';
like $home->slurp_rel_file('lib/BaseTest/Base2.pm'), qr/Base2/,
'right content';
like $home->slurp_rel_file('lib/BaseTest/Base3.pm'), qr/Base3/,
'right content';

0 comments on commit feda70b

Please sign in to comment.