Skip to content

Commit

Permalink
improved Mojo::Asset::File to allow appending data to existing files …
Browse files Browse the repository at this point in the history
…(#closes 649)
  • Loading branch information
kraih committed Jul 21, 2014
1 parent 1bca6bb commit ce2bbf1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
4 changes: 3 additions & 1 deletion Changes
@@ -1,7 +1,9 @@

5.16 2014-07-20
5.16 2014-07-21
- Added empty method to Mojo::Cache.
- Added flush method to Mojolicious::Routes.
- Improved Mojo::Asset::File to allow appending data to existing files.
(iakuf, sri)

5.15 2014-07-17
- Improved Mojo::DOM::HTML performance slightly.
Expand Down
5 changes: 3 additions & 2 deletions lib/Mojo/Asset/File.pm
Expand Up @@ -3,7 +3,7 @@ use Mojo::Base 'Mojo::Asset';

use Carp 'croak';
use Errno 'EEXIST';
use Fcntl qw(O_CREAT O_EXCL O_RDWR);
use Fcntl qw(O_CREAT O_EXCL O_RDONLY O_RDWR);
use File::Copy 'move';
use File::Spec::Functions 'catfile';
use IO::File;
Expand All @@ -17,7 +17,8 @@ has handle => sub {
my $handle = IO::File->new;
my $path = $self->path;
if (defined $path && -f $path) {
$handle->open($path, '<') or croak qq{Can't open file "$path": $!};
$handle->open($path, -w _ ? O_RDWR : O_RDONLY)
or croak qq{Can't open file "$path": $!};
return $handle;
}

Expand Down
12 changes: 12 additions & 0 deletions t/mojo/asset.t
Expand Up @@ -183,6 +183,18 @@ ok !$asset->is_file, 'stored in memory';
$asset = $asset->add_chunk('lala');
ok !$asset->is_file, 'stored in memory';

# Append to file asset
$file = Mojo::Asset::File->new(cleanup => 0);
is $file->add_chunk('hello')->slurp, 'hello', 'right content';
$path = $file->path;
undef $file;
ok -e $path, 'file still exists';
$file = Mojo::Asset::File->new(path => $path, cleanup => 1);
is $file->add_chunk(' world')->slurp, 'hello world', 'right content';
is $file->add_chunk('!')->slurp, 'hello world!', 'right content';
undef $file;
ok !-e $path, 'file has been cleaned up';

# Temporary directory
{
my $tmpdir = tempdir CLEANUP => 1;
Expand Down

0 comments on commit ce2bbf1

Please sign in to comment.