Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a proof-of-concept version of Mojo::File for easier decision making
- Loading branch information
Showing
5 changed files
with
151 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package Mojo::File; | ||
use Mojo::Base -strict; | ||
use overload | ||
'@{}' => sub { shift->to_array }, | ||
bool => sub {1}, | ||
'""' => sub { ${$_[0]} }, | ||
fallback => 1; | ||
|
||
use Carp 'croak'; | ||
use Cwd qw(abs_path getcwd); | ||
use Exporter 'import'; | ||
use File::Basename (); | ||
use File::Copy (); | ||
use File::Path (); | ||
use File::Spec; | ||
use File::Temp (); | ||
use Mojo::Util; | ||
use Scalar::Util 'blessed'; | ||
|
||
our @EXPORT_OK = ('path', 'tempdir'); | ||
|
||
sub basename { path(File::Basename::basename ${shift()}) } | ||
|
||
sub child { path(shift()->to_string, @_) } | ||
|
||
sub dirname { path(File::Basename::dirname ${shift()}) } | ||
|
||
sub make_path { | ||
my $self = shift; | ||
File::Path::make_path $$self, @_ | ||
or croak qq{Can't make directory "$$self": $!}; | ||
return $self; | ||
} | ||
|
||
sub move_to { | ||
my ($self, $to) = @_; | ||
File::Copy::move($$self, $to) | ||
or croak qq{Can't move file "$$self" to "$to": $!}; | ||
return path($to); | ||
} | ||
|
||
sub new { | ||
my $class = shift; | ||
my $self = bless \my $dummy, ref $class || $class; | ||
|
||
unless (@_) { $$self = getcwd } | ||
|
||
elsif (blessed $_[0] && $_[0]->isa('File::Temp::Dir')) { $$self = $_[0] } | ||
|
||
else { $$self = File::Spec->catfile(@_) } | ||
|
||
return $self; | ||
} | ||
|
||
sub path { __PACKAGE__->new(@_) } | ||
|
||
sub slurp { Mojo::Util::slurp ${shift()} } | ||
|
||
sub spurt { | ||
my $self = shift; | ||
Mojo::Util::spurt shift, $$self; | ||
return $self; | ||
} | ||
|
||
sub tap { shift->Mojo::Base::tap(@_) } | ||
|
||
sub tempdir { path(File::Temp->newdir(@_)) } | ||
|
||
sub to_abs { path(abs_path ${shift()}) } | ||
|
||
sub to_array { [File::Spec->splitdir(${shift()})] } | ||
|
||
sub to_rel { path(File::Spec->abs2rel(${shift()}, shift)) } | ||
|
||
sub to_string {"${$_[0]}"} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use Mojo::Base -strict; | ||
|
||
use Test::More; | ||
use Cwd qw(abs_path getcwd); | ||
use File::Basename qw(basename dirname); | ||
use File::Spec::Functions qw(abs2rel catfile splitdir); | ||
use Mojo::File qw(path tempdir); | ||
|
||
# Constructor | ||
is(Mojo::File->new, getcwd(), 'same path'); | ||
is path(), getcwd(), 'same path'; | ||
is path()->to_string, getcwd(), 'same path'; | ||
is path('/foo/bar'), catfile('/foo/bar'), 'same path'; | ||
is path('foo', 'bar', 'baz'), catfile('foo', 'bar', 'baz'), 'same path'; | ||
|
||
# Children | ||
is path('foo', 'bar')->child('baz', 'yada'), | ||
catfile(catfile('foo', 'bar'), 'baz', 'yada'), 'same path'; | ||
|
||
# Array | ||
is_deeply path('foo', 'bar')->to_array, [splitdir catfile('foo', 'bar')], | ||
'same structure'; | ||
is_deeply [@{path('foo', 'bar')}], [splitdir catfile('foo', 'bar')], | ||
'same structure'; | ||
|
||
# Absolute | ||
is path('file.t')->to_abs, abs_path('file.t'), 'same path'; | ||
|
||
# Relative | ||
is path('test.txt')->to_abs->to_rel(getcwd), | ||
abs2rel(abs_path('test.txt'), getcwd), 'same path'; | ||
|
||
# Basename | ||
is path('file.t')->to_abs->basename, basename(abs_path 'file.t'), 'same path'; | ||
|
||
# Dirname | ||
is path('file.t')->to_abs->dirname, dirname(abs_path 'file.t'), 'same path'; | ||
|
||
# Temporary directory | ||
my $dir = tempdir; | ||
my $path = "$dir"; | ||
ok -d $path, 'directory exists'; | ||
undef $dir; | ||
ok !-d $path, 'directory does not exist anymore'; | ||
|
||
# Make path | ||
$dir = tempdir; | ||
my $subdir = $dir->child('foo', 'bar'); | ||
ok !-d $subdir, 'directory does not exist anymore'; | ||
$subdir->make_path; | ||
ok -d $subdir, 'directory exists'; | ||
|
||
# I/O | ||
$dir = tempdir; | ||
my $file = $dir->child('test.txt')->spurt('just works!'); | ||
is $file->slurp, 'just works!', 'right content'; | ||
|
||
done_testing(); |