Skip to content

Commit

Permalink
added experimental upgrade event to Mojo::Asset::Memory
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Nov 9, 2011
1 parent e8aaf31 commit 871d0f5
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 15 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,5 +1,8 @@
This file documents the revision history for Perl extension Mojolicious.

2.26 2011-11-09 00:00:00
- Added EXPERIMENTAL upgrade event to Mojo::Asset::Memory.

2.25 2011-11-08 00:00:00
- Removed canonicalize method from Mojo::URL.
- Improved documentation.
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/Asset.pm
@@ -1,5 +1,5 @@
package Mojo::Asset;
use Mojo::Base -base;
use Mojo::Base 'Mojo::EventEmitter';

use Carp 'croak';

Expand Down Expand Up @@ -52,8 +52,8 @@ Pretend file starts later.
=head1 METHODS
L<Mojo::Asset> inherits all methods from L<Mojo::Base> and implements the
following new ones.
L<Mojo::Asset> inherits all methods from L<Mojo::EventEmitter> and implements
the following new ones.
=head2 C<add_chunk>
Expand Down
11 changes: 4 additions & 7 deletions lib/Mojo/Asset/File.pm
Expand Up @@ -150,12 +150,8 @@ sub move_to {

sub size {
my $self = shift;

# File size
my $file = $self->path;
return -s $file if $file;

return 0;
return 0 unless my $file = $self->path;
return -s $file;
}

sub slurp {
Expand Down Expand Up @@ -224,7 +220,8 @@ Actual file path.
my $tmpdir = $file->tmpdir;
$file = $file->tmpdir('/tmp');
Temporary directory.
Temporary directory, defaults to the value of C<MOJO_TMPDIR> or auto
detection.
=head1 METHODS
Expand Down
25 changes: 22 additions & 3 deletions lib/Mojo/Asset/Memory.pm
Expand Up @@ -19,9 +19,10 @@ sub new {
sub add_chunk {
my ($self, $chunk) = @_;
$self->{content} .= $chunk if defined $chunk;
return Mojo::Asset::File->new->add_chunk($self->slurp)
if $self->size > $self->max_memory_size;
return $self;
return $self unless $self->size > $self->max_memory_size;
my $file = Mojo::Asset::File->new->add_chunk($self->slurp);
$self->emit(upgrade => $file);
return $file;
}

sub contains {
Expand Down Expand Up @@ -79,6 +80,24 @@ Mojo::Asset::Memory - In-memory storage for HTTP 1.1 content
L<Mojo::Asset::Memory> is an in-memory storage backend for HTTP 1.1 content.
=head1 EVENTS
L<Mojo::Asset::Memory> can emit the following events.
=head2 C<upgrade>
$mem->on(upgrade => sub {
my ($mem, $file) = @_;
});
Emitted when asset gets upgraded to a L<Mojo::Asset::File> object.
Note that this event is EXPERIMENTAL and might change without warning!
$mem->on(upgrade => sub {
my ($mem, $file) = @_;
$file->tmpdir('/tmp');
});
=head1 ATTRIBUTES
L<Mojo::Asset::Memory> inherits all attributes from L<Mojo::Asset> and
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious.pm
Expand Up @@ -35,7 +35,7 @@ has static => sub { Mojolicious::Static->new };
has types => sub { Mojolicious::Types->new };

our $CODENAME = 'Leaf Fluttering In Wind';
our $VERSION = '2.25';
our $VERSION = '2.26';

# "These old doomsday devices are dangerously unstable.
# I'll rest easier not knowing where they are."
Expand Down
6 changes: 5 additions & 1 deletion t/mojo/request.t
Expand Up @@ -3,7 +3,7 @@ use Mojo::Base -strict;

use utf8;

use Test::More tests => 907;
use Test::More tests => 909;

# "When will I learn?
# The answer to life's problems aren't at the bottom of a bottle,
Expand Down Expand Up @@ -308,6 +308,8 @@ is $req->headers->content_length, undef, 'no "Content-Length" value';
my $backup = $ENV{MOJO_MAX_MEMORY_SIZE} || '';
$ENV{MOJO_MAX_MEMORY_SIZE} = 12;
$req = Mojo::Message::Request->new;
my $upgrade;
$req->content->asset->on(upgrade => sub { $upgrade = pop->is_file });
is $req->content->progress, 0, 'right progress';
$req->parse('GET /foo/bar/baz.html?fo');
is $req->content->progress, 0, 'right progress';
Expand All @@ -317,7 +319,9 @@ is $req->content->progress, 0, 'right progress';
$req->parse("plain\x0d\x0aContent-Length: 27\x0d\x0a\x0d\x0aHell");
is $req->content->progress, 4, 'right progress';
ok !$req->content->asset->is_file, 'stored in memory';
ok !$upgrade, 'upgrade event has not been emitted';
$req->parse("o World!\n");
ok $upgrade, 'upgrade event has been emitted';
is $req->content->progress, 13, 'right progress';
ok $req->content->asset->is_file, 'stored in file';
$req->parse("1234\nlalalala\n");
Expand Down

0 comments on commit 871d0f5

Please sign in to comment.