Skip to content

Commit

Permalink
use $c and $app instead of $self in many places
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jun 23, 2014
1 parent e29dbc4 commit 6560294
Show file tree
Hide file tree
Showing 40 changed files with 910 additions and 920 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,5 +1,5 @@

5.09 2014-06-20
5.09 2014-06-23

5.08 2014-06-17
- Added reset method to Mojo::IOLoop.
Expand Down
16 changes: 8 additions & 8 deletions README.md
Expand Up @@ -76,18 +76,18 @@ get '/time' => 'clock';

# Scrape information from remote sites
post '/title' => sub {
my $self = shift;
my $url = $self->param('url') || 'http://mojolicio.us';
my $title = $self->ua->get($url)->res->dom->at('title')->text;
$self->render(json => {url => $url, title => $title});
my $c = shift;
my $url = $c->param('url') || 'http://mojolicio.us';
my $title = $c->ua->get($url)->res->dom->at('title')->text;
$c->render(json => {url => $url, title => $title});
};

# WebSocket echo service
websocket '/echo' => sub {
my $self = shift;
$self->on(message => sub {
my ($self, $msg) = @_;
$self->send("echo: $msg");
my $c = shift;
$c->on(message => sub {
my ($c, $msg) = @_;
$c->send("echo: $msg");
});
};

Expand Down
8 changes: 4 additions & 4 deletions examples/websocket.pl
Expand Up @@ -3,12 +3,12 @@
use Mojolicious::Lite;

websocket '/test' => sub {
my $self = shift;
$self->on(
my $c = shift;
$c->on(
json => sub {
my ($self, $hash) = @_;
my ($c, $hash) = @_;
$hash->{test} = "$hash->{test}";
$self->send({json => $hash});
$c->send({json => $hash});
}
);
};
Expand Down
10 changes: 7 additions & 3 deletions lib/Mojolicious.pm
Expand Up @@ -581,9 +581,13 @@ and the application object, as well as a function in C<ep> templates.
# Helper
$app->helper(cache => sub { state $cache = {} });
# Controller/Application
$self->cache->{foo} = 'bar';
my $result = $self->cache->{foo};
# Application
$app->cache->{foo} = 'bar';
my $result = $app->cache->{foo};
# Controller
$c->cache->{foo} = 'bar';
my $result = $c->cache->{foo};
# Template
% cache->{foo} = 'bar';
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Command/generate/lite_app.pm
Expand Up @@ -22,8 +22,8 @@ use Mojolicious::Lite;
plugin 'PODRenderer';
get '/' => sub {
my $self = shift;
$self->render('index');
my $c = shift;
$c->render('index');
};
app->start;
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Command/generate/plugin.pm
Expand Up @@ -88,8 +88,8 @@ use Test::Mojo;
plugin '<%= $name %>';
get '/' => sub {
my $self = shift;
$self->render(text => 'Hello Mojo!');
my $c = shift;
$c->render(text => 'Hello Mojo!');
};
my $t = Test::Mojo->new;
Expand Down
16 changes: 8 additions & 8 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -607,13 +607,13 @@ parameters, so you have to make sure it is not excessively large, there's a
# List context is ambiguous and should be avoided, you can get multiple
# values returned for a query string like "?foo=bar&foo=baz&foo=yada"
my $hash = {foo => $self->param('foo')};
my $hash = {foo => $c->param('foo')};
# Better enforce scalar context
my $hash = {foo => scalar $self->param('foo')};
my $hash = {foo => scalar $c->param('foo')};
# The multi-name form can also be used to enforce scalar context
my $hash = {foo => $self->param(['foo'])};
my $hash = {foo => $c->param(['foo'])};
For more control you can also access request information directly.
Expand Down Expand Up @@ -712,7 +712,7 @@ Try to render content, but do not call L</"render_not_found"> if no response
could be generated, takes the same arguments as L</"render">.
# Render template "index_local" only if it exists
$self->render_maybe('index_local') or $self->render('index');
$c->render_maybe('index_local') or $c->render('index');
=head2 render_not_found
Expand Down Expand Up @@ -750,12 +750,12 @@ Finalize response and emit hook L<Mojolicious/"after_dispatch">, defaults to
using a C<200> response code.
# Custom response
$self->res->headers->content_type('text/plain');
$self->res->body('Hello World!');
$self->rendered(200);
$c->res->headers->content_type('text/plain');
$c->res->body('Hello World!');
$c->rendered(200);
# Accept WebSocket handshake without subscribing to an event
$self->rendered(101);
$c->rendered(101);
=head2 req
Expand Down
74 changes: 37 additions & 37 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -262,8 +262,8 @@ But you could even use middleware right in your application.
use Plack::Builder;

get '/welcome' => sub {
my $self = shift;
$self->render(text => 'Hello Mojo!');
my $c = shift;
$c->render(text => 'Hello Mojo!');
};

builder {
Expand Down Expand Up @@ -371,10 +371,10 @@ latency backend web services.

# Search MetaCPAN for "mojolicious"
get '/' => sub {
my $self = shift;
$self->ua->get('api.metacpan.org/v0/module/_search?q=mojolicious' => sub {
my $c = shift;
$c->ua->get('api.metacpan.org/v0/module/_search?q=mojolicious' => sub {
my ($ua, $tx) = @_;
$self->render('metacpan', hits => $tx->res->json->{hits}{hits});
$c->render('metacpan', hits => $tx->res->json->{hits}{hits});
});
};

Expand Down Expand Up @@ -402,7 +402,7 @@ often result from continuation-passing style.

# Search MetaCPAN for "mojo" and "mango"
get '/' => sub {
my $self = shift;
my $c = shift;

# Prepare response in two steps
Mojo::IOLoop->delay(
Expand All @@ -412,14 +412,14 @@ often result from continuation-passing style.
my $delay = shift;
my $url = Mojo::URL->new('api.metacpan.org/v0/module/_search');
$url->query({sort => 'date:desc'});
$self->ua->get($url->clone->query({q => 'mojo'}) => $delay->begin);
$self->ua->get($url->clone->query({q => 'mango'}) => $delay->begin);
$c->ua->get($url->clone->query({q => 'mojo'}) => $delay->begin);
$c->ua->get($url->clone->query({q => 'mango'}) => $delay->begin);
},

# Delayed rendering
sub {
my ($delay, $mojo, $mango) = @_;
$self->render(json => {
$c->render(json => {
mojo => $mojo->res->json('/hits/hits/0/_source/release'),
mango => $mango->res->json('/hits/hits/0/_source/release')
});
Expand All @@ -441,9 +441,9 @@ processed concurrently.

# Wait 3 seconds before rendering a response
get '/' => sub {
my $self = shift;
my $c = shift;
Mojo::IOLoop->timer(3 => sub {
$self->render(text => 'Delayed by 3 seconds!');
$c->render(text => 'Delayed by 3 seconds!');
});
};

Expand All @@ -458,17 +458,17 @@ emitted.

# Count to 5 in 1 second steps
get '/' => sub {
my $self = shift;
my $c = shift;

# Start recurring timer
my $i = 1;
my $id = Mojo::IOLoop->recurring(1 => sub {
$self->write_chunk($i);
$self->finish if $i++ == 5;
$c->write_chunk($i);
$c->finish if $i++ == 5;
});

# Stop recurring timer
$self->on(finish => sub { Mojo::IOLoop->remove($id) });
$c->on(finish => sub { Mojo::IOLoop->remove($id) });
};

app->start;
Expand All @@ -490,8 +490,8 @@ created at startup time.

# Show current title
get '/' => sub {
my $self = shift;
$self->render(json => {title => $title});
my $c = shift;
$c->render(json => {title => $title});
};

app->start;
Expand Down Expand Up @@ -525,24 +525,24 @@ L<Mojolicious::Controller/"send">.

# WebSocket echo service
websocket '/echo' => sub {
my $self = shift;
my $c = shift;

# Opened
$self->app->log->debug('WebSocket opened.');
$c->app->log->debug('WebSocket opened.');

# Increase inactivity timeout for connection a bit
Mojo::IOLoop->stream($self->tx->connection)->timeout(300);
Mojo::IOLoop->stream($c->tx->connection)->timeout(300);

# Incoming message
$self->on(message => sub {
my ($self, $msg) = @_;
$self->send("echo: $msg");
$c->on(message => sub {
my ($c, $msg) = @_;
$c->send("echo: $msg");
});

# Closed
$self->on(finish => sub {
my ($self, $code, $reason) = @_;
$self->app->log->debug("WebSocket closed with status $code.");
$c->on(finish => sub {
my ($c, $code, $reason) = @_;
$c->app->log->debug("WebSocket closed with status $code.");
});
};

Expand Down Expand Up @@ -573,7 +573,7 @@ L<Mojolicious::Controller/"send">.
The event L<Mojo::Transaction::WebSocket/"finish"> will be emitted right after
the WebSocket connection has been closed.

$self->tx->with_compression;
$c->tx->with_compression;

You can activate C<permessage-deflate> compression with
L<Mojo::Transaction::WebSocket/"with_compression">, this can result in much
Expand Down Expand Up @@ -626,24 +626,24 @@ infrastructure requirements, since it reuses the HTTP protocol for transport.

# EventSource for log messages
get '/events' => sub {
my $self = shift;
my $c = shift;

# Increase inactivity timeout for connection a bit
Mojo::IOLoop->stream($self->tx->connection)->timeout(300);
Mojo::IOLoop->stream($c->tx->connection)->timeout(300);

# Change content type
$self->res->headers->content_type('text/event-stream');
$c->res->headers->content_type('text/event-stream');

# Subscribe to "message" event and forward "log" events to browser
my $cb = $self->app->log->on(message => sub {
my $cb = $c->app->log->on(message => sub {
my ($log, $level, @lines) = @_;
$self->write("event:log\ndata: [$level] @lines\n\n");
$c->write("event:log\ndata: [$level] @lines\n\n");
});

# Unsubscribe from "message" event again once we are done
$self->on(finish => sub {
my $self = shift;
$self->app->log->unsubscribe(message => $cb);
$c->on(finish => sub {
my $c = shift;
$c->app->log->unsubscribe(message => $cb);
});
};

Expand Down Expand Up @@ -743,10 +743,10 @@ allows other event loops like L<AnyEvent> to just work.

# Wait 3 seconds before rendering a response
get '/' => sub {
my $self = shift;
my $c = shift;
my $w;
$w = AE::timer 3, 0, sub {
$self->render(text => 'Delayed by 3 seconds!');
$c->render(text => 'Delayed by 3 seconds!');
undef $w;
};
};
Expand Down

0 comments on commit 6560294

Please sign in to comment.