Skip to content

Commit

Permalink
Merge pull request #1028 from kraih/mojo_file
Browse files Browse the repository at this point in the history
add a Mojo::File module
  • Loading branch information
kraih committed Jan 9, 2017
2 parents 4bed55d + 0afb80b commit 4ca19b1
Show file tree
Hide file tree
Showing 53 changed files with 826 additions and 449 deletions.
4 changes: 2 additions & 2 deletions lib/Mojo.pm
Expand Up @@ -78,8 +78,8 @@ L<Mojo> implements the following attributes.
The home directory of your application, defaults to a L<Mojo::Home> object
which stringifies to the actual path.
# Generate portable path relative to home directory
my $path = $app->home->rel_file('data/important.txt');
# Portably generate path relative to home directory
my $path = $app->home->child('data', 'important.txt');
=head2 log
Expand Down
11 changes: 5 additions & 6 deletions lib/Mojo/Asset/File.pm
Expand Up @@ -4,9 +4,9 @@ use Mojo::Base 'Mojo::Asset';
use Carp 'croak';
use Errno 'EEXIST';
use Fcntl qw(O_APPEND O_CREAT O_EXCL O_RDONLY O_RDWR);
use File::Copy 'move';
use File::Spec::Functions 'catfile';
use File::Spec;
use IO::File;
use Mojo::File;
use Mojo::Util 'md5_sum';

has [qw(cleanup path)];
Expand All @@ -22,7 +22,7 @@ has handle => sub {
}

# Open new or temporary file
my $base = catfile $self->tmpdir, 'mojo.tmp';
my $base = Mojo::File->new($self->tmpdir, 'mojo.tmp')->to_string;
my $name = $path // $base;
until ($handle->open($name, O_APPEND | O_CREAT | O_EXCL | O_RDWR)) {
croak qq{Can't open file "$name": $!} if defined $path || $! != $!{EEXIST};
Expand All @@ -35,7 +35,7 @@ has handle => sub {

return $handle;
};
has tmpdir => sub { $ENV{MOJO_TMPDIR} || File::Spec::Functions::tmpdir };
has tmpdir => sub { $ENV{MOJO_TMPDIR} || File::Spec->tmpdir };

sub DESTROY {
my $self = shift;
Expand Down Expand Up @@ -113,8 +113,7 @@ sub move_to {
delete $self->{handle};

# Move file and prevent clean up
my $from = $self->path;
move($from, $to) or croak qq{Can't move file "$from" to "$to": $!};
Mojo::File->new($self->path)->move_to($to);
return $self->path($to)->cleanup(0);
}

Expand Down
8 changes: 2 additions & 6 deletions lib/Mojo/Asset/Memory.pm
Expand Up @@ -2,7 +2,7 @@ package Mojo::Asset::Memory;
use Mojo::Base 'Mojo::Asset';

use Mojo::Asset::File;
use Mojo::Util 'spurt';
use Mojo::File 'path';

has 'auto_upgrade';
has max_memory_size => sub { $ENV{MOJO_MAX_MEMORY_SIZE} || 262144 };
Expand Down Expand Up @@ -41,11 +41,7 @@ sub get_chunk {
return substr shift->{content} // '', $offset, $max;
}

sub move_to {
my ($self, $to) = @_;
spurt $self->{content} // '', $to;
return $self;
}
sub move_to { path($_[1])->spurt($_[0]{content} // '') and return $_[0] }

sub size { length(shift->{content} // '') }

Expand Down
42 changes: 22 additions & 20 deletions lib/Mojo/ByteStream.pm
Expand Up @@ -12,8 +12,8 @@ our @EXPORT_OK = ('b');
my @UTILS = (
qw(b64_decode b64_encode camelize decamelize hmac_sha1_sum html_unescape),
qw(md5_bytes md5_sum punycode_decode punycode_encode quote sha1_bytes),
qw(sha1_sum slurp spurt term_escape trim unindent unquote url_escape),
qw(url_unescape xml_escape xor_encode)
qw(sha1_sum term_escape trim unindent unquote url_escape url_unescape),
qw(xml_escape xor_encode)
);
for my $name (@UTILS) {
my $sub = Mojo::Util->can($name);
Expand Down Expand Up @@ -47,11 +47,31 @@ sub secure_compare { Mojo::Util::secure_compare ${shift()}, shift }

sub size { length ${$_[0]} }

# DEPRECATED!
sub slurp {
Mojo::Util::deprecated 'Mojo::ByteStream::slurp is DEPRECATED'
. ' in favor of Mojo::File::slurp';
require Mojo::File;
my $self = shift;
$$self = Mojo::File->new($$self)->slurp;
return $self;
}

sub split {
my ($self, $pattern) = @_;
return Mojo::Collection->new(map { $self->new($_) } split $pattern, $$self);
}

# DEPRECATED!
sub spurt {
Mojo::Util::deprecated 'Mojo::ByteStream::spurt is DEPRECATED'
. ' in favor of Mojo::File::spurt';
require Mojo::File;
my $self = shift;
Mojo::File->new(shift)->spurt($$self);
return $self;
}

sub tap { shift->Mojo::Base::tap(@_) }

sub to_string { ${$_[0]} }
Expand Down Expand Up @@ -250,24 +270,6 @@ Generate SHA1 checksum for bytestream with L<Mojo::Util/"sha1_sum">.
Size of bytestream.
=head2 slurp
$stream = $stream->slurp;
Read all data at once from file into bytestream with L<Mojo::Util/"slurp">.
# Read file and print lines in random order
b('/home/sri/myapp.pl')->slurp->split("\n")->shuffle->join("\n")->say;
=head2 spurt
$stream = $stream->spurt('/home/sri/myapp.pl');
Write all data from bytestream at once to file with L<Mojo::Util/"spurt">.
# Remove unnecessary whitespace from file
b('/home/sri/foo.txt')->slurp->trim->spurt('/home/sri/bar.txt');
=head2 split
my $collection = $stream->split(',');
Expand Down

0 comments on commit 4ca19b1

Please sign in to comment.