Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
move home and ua attributes from Mojo to Mojolicious
  • Loading branch information
kraih committed Dec 16, 2017
1 parent 7eb70d8 commit 0c828fd
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 43 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,5 +1,6 @@

7.59 2017-12-14
7.59 2017-12-15
- Moved home and ua attributes from Mojo to Mojolicious.
- Changed Mojo::IOLoop::Client to only start a thread pool with
Net::DNS::Native on demand. (Grinnz)
- Improved subprocess method in Mojo::IOLoop to allow for easier role
Expand Down
33 changes: 1 addition & 32 deletions lib/Mojo.pm
Expand Up @@ -4,20 +4,11 @@ use Mojo::Base -base;
# "Professor: These old Doomsday devices are dangerously unstable. I'll rest
# easier not knowing where they are."
use Carp ();
use Mojo::Home;
use Mojo::Log;
use Mojo::Transaction::HTTP;
use Mojo::UserAgent;
use Mojo::Util;
use Scalar::Util ();

has home => sub { Mojo::Home->new->detect(ref shift) };
has log => sub { Mojo::Log->new };
has ua => sub {
my $ua = Mojo::UserAgent->new;
Scalar::Util::weaken $ua->server->app(shift)->{app};
return $ua;
};
has log => sub { Mojo::Log->new };

sub build_tx { Mojo::Transaction::HTTP->new }

Expand Down Expand Up @@ -70,17 +61,6 @@ See L<Mojolicious::Guides> for more!
L<Mojo> implements the following attributes.
=head2 home
my $home = $app->home;
$app = $app->home(Mojo::Home->new);
The home directory of your application, defaults to a L<Mojo::Home> object
which stringifies to the actual path.
# Portably generate path relative to home directory
my $path = $app->home->child('data', 'important.txt');
=head2 log
my $log = $app->log;
Expand All @@ -91,17 +71,6 @@ The logging layer of your application, defaults to a L<Mojo::Log> object.
# Log debug message
$app->log->debug('It works');
=head2 ua
my $ua = $app->ua;
$app = $app->ua(Mojo::UserAgent->new);
A full featured HTTP user agent for use in your applications, defaults to a
L<Mojo::UserAgent> object.
# Perform blocking request
say $app->ua->get('example.com')->result->body;
=head1 METHODS
L<Mojo> inherits all methods from L<Mojo::Base> and implements the following
Expand Down
36 changes: 33 additions & 3 deletions lib/Mojolicious.pm
Expand Up @@ -4,8 +4,10 @@ use Mojo::Base 'Mojo';
# "Fry: Shut up and take my money!"
use Carp ();
use Mojo::Exception;
use Mojo::Home;
use Mojo::Log;
use Mojo::Util;
use Mojo::UserAgent;
use Mojolicious::Commands;
use Mojolicious::Controller;
use Mojolicious::Plugins;
Expand All @@ -24,6 +26,7 @@ has commands => sub {
return $commands;
};
has controller_class => 'Mojolicious::Controller';
has home => sub { Mojo::Home->new->detect(ref shift) };
has log => sub {
my $self = shift;

Expand Down Expand Up @@ -52,9 +55,14 @@ has secrets => sub {
# Default to moniker
return [$self->moniker];
};
has sessions => sub { Mojolicious::Sessions->new };
has static => sub { Mojolicious::Static->new };
has types => sub { Mojolicious::Types->new };
has sessions => sub { Mojolicious::Sessions->new };
has static => sub { Mojolicious::Static->new };
has types => sub { Mojolicious::Types->new };
has ua => sub {
my $ua = Mojo::UserAgent->new;
Scalar::Util::weaken $ua->server->app(shift)->{app};
return $ua;
};
has validator => sub { Mojolicious::Validator->new };

our $CODENAME = 'Doughnut';
Expand Down Expand Up @@ -405,6 +413,17 @@ Class to be used for the default controller, defaults to
L<Mojolicious::Controller>. Note that this class needs to have already been
loaded before the first request arrives.
=head2 home
my $home = $app->home;
$app = $app->home(Mojo::Home->new);
The home directory of your application, defaults to a L<Mojo::Home> object
which stringifies to the actual path.
# Portably generate path relative to home directory
my $path = $app->home->child('data', 'important.txt');
=head2 log
my $log = $app->log;
Expand Down Expand Up @@ -553,6 +572,17 @@ L<Mojolicious::Types> object.
# Add custom MIME type
$app->types->type(twt => 'text/tweet');
=head2 ua
my $ua = $app->ua;
$app = $app->ua(Mojo::UserAgent->new);
A full featured HTTP user agent for use in your applications, defaults to a
L<Mojo::UserAgent> object.
# Perform blocking request
say $app->ua->get('example.com')->result->body;
=head2 validator
my $validator = $app->validator;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Plugin/DefaultHelpers.pm
Expand Up @@ -474,7 +474,7 @@ the L</"stash">.
%= ua->get('mojolicious.org')->result->dom->at('title')->text
Alias for L<Mojo/"ua">.
Alias for L<Mojolicious/"ua">.
=head2 url_for
Expand Down
6 changes: 1 addition & 5 deletions t/mojo/daemon.t
Expand Up @@ -66,12 +66,8 @@ is $tx->res->body, 'Hello TestApp!', 'right content';
ok !!Mojo::Server::Daemon->new->reverse_proxy, 'reverse proxy';
}

# Optional home detection
my @path = qw(th is mojo dir wil l never-ever exist);
my $app = Mojo->new(home => Mojo::Home->new(@path));
is $app->home, path(@path), 'right home directory';

# Config
my $app = Mojo->new;
is $app->config('foo'), undef, 'no value';
is_deeply $app->config(foo => 'bar')->config, {foo => 'bar'}, 'right value';
is $app->config('foo'), 'bar', 'right value';
Expand Down
8 changes: 7 additions & 1 deletion t/mojolicious/app.t
Expand Up @@ -14,6 +14,7 @@ use lib "$FindBin::Bin/lib";
use Mojo::Asset::File;
use Mojo::Date;
use Mojo::File 'path';
use Mojo::Home;
use Mojo::IOLoop;
use Mojolicious;
use Mojolicious::Controller;
Expand All @@ -40,6 +41,11 @@ use Test::Mojo;
is(Test::Mojo->new('MojoliciousTest')->app->mode, 'else', 'right mode');
}

# Optional home detection
my @path = qw(th is mojo dir wil l never-ever exist);
my $app = Mojolicious->new(home => Mojo::Home->new(@path));
is $app->home, path(@path), 'right home directory';

my $t = Test::Mojo->new('MojoliciousTest');

# Application is already available
Expand Down Expand Up @@ -460,7 +466,7 @@ is(MojoliciousTest->new(mode => 'test')->mode, 'test', 'right mode');
is(MojoliciousTest->new({mode => 'test'})->mode, 'test', 'right mode');

# Persistent error
my $app = MojoliciousTest->new;
$app = MojoliciousTest->new;
my $tx = $t->ua->build_tx(GET => '/foo');
$app->handler($tx);
is $tx->res->code, 200, 'right status';
Expand Down

0 comments on commit 0c828fd

Please sign in to comment.