Skip to content

Commit

Permalink
allow Path::Tiny in addition to path strings
Browse files Browse the repository at this point in the history
We're now using Path::Tiny everywhere, because I like it better.

Fixes #35
  • Loading branch information
preaction committed Mar 1, 2015
1 parent a9d87ab commit d5374da
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
26 changes: 17 additions & 9 deletions lib/Beam/Wire.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use Module::Runtime qw( use_module );
use Data::DPath qw ( dpath );
use Path::Tiny qw( path );
use File::Basename qw( dirname );
use File::Spec::Functions qw( splitpath catfile file_name_is_absolute );
use Types::Standard qw( :all );

=head1 SYNOPSIS
Expand Down Expand Up @@ -450,7 +449,13 @@ a single hashref. The keys will become the service names.

has file => (
is => 'ro',
isa => Str,
isa => InstanceOf['Path::Tiny'],
coerce => sub {
if ( !blessed $_[0] || !$_[0]->isa('Path::Tiny') ) {
return path( $_[0] );
}
return $_[0];
},
);

=attr dir
Expand All @@ -463,11 +468,14 @@ the L<file|file attribute>.

has dir => (
is => 'ro',
isa => Str,
isa => InstanceOf['Path::Tiny'],
lazy => 1,
default => sub {
my ( $volume, $path, $file ) = splitpath( $_[0]->file );
return join "", $volume, $path;
default => sub { $_[0]->file->parent },
coerce => sub {
if ( !blessed $_[0] || !$_[0]->isa('Path::Tiny') ) {
return path( $_[0] );
}
return $_[0];
},
);

Expand Down Expand Up @@ -660,8 +668,8 @@ sub parse_args {
my $config = delete $args{config};
# Relative subcontainer files should be from the current
# container's directory
if ( exists $args{file} && !file_name_is_absolute( $args{file} ) ) {
$args{file} = catfile( $self->dir, $args{file} );
if ( exists $args{file} && !path( $args{file} )->is_absolute ) {
$args{file} = $self->dir->child( $args{file} );
}
@args = $self->find_refs( %args );
if ( $config ) {
Expand Down Expand Up @@ -962,7 +970,7 @@ has name => (

has file => (
is => 'ro',
isa => Maybe[Str],
isa => Maybe[InstanceOf['Path::Tiny']],
);

=head2 Beam::Wire::Exception::NotFound
Expand Down
14 changes: 9 additions & 5 deletions t/20_config.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use Test::More;
use Test::Deep;
use FindBin qw( $Bin );
use File::Spec::Functions qw( catfile );
use Path::Tiny qw( path );
use Scalar::Util qw( refaddr );

use Beam::Wire;
Expand Down Expand Up @@ -44,10 +44,14 @@ use Beam::Wire;
);
}

for (qw(file.json file.pl file.yml)) {
subtest "load module from config - $_" => sub {
my $FILE = catfile( $Bin, 'share', $_ );
my $wire = Beam::Wire->new( file => $FILE );
my @paths = map {; $_, "$_" }
map { path( $Bin, 'share', $_ ) }
qw( file.json file.pl file.yml )
;

for my $path ( @paths ) {
subtest "load module from config - $path " . ref($path) => sub {
my $wire = Beam::Wire->new( file => $path );
my $foo = $wire->get('foo');
isa_ok $foo, 'Foo';
is refaddr $wire->get('foo'), refaddr $foo, 'container caches the object';
Expand Down
8 changes: 4 additions & 4 deletions t/21_inner.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
use Test::More;
use Test::Deep;
use FindBin qw( $Bin );
use File::Spec::Functions qw( catfile );
use Path::Tiny qw( path );
use Scalar::Util qw( refaddr );

my $SINGLE_FILE = catfile( $Bin, 'share', 'file.yml' );
my $DEEP_FILE = catfile( $Bin, 'share', 'inner_inline.yml' );
my $INNER_FILE = catfile( $Bin, 'share', 'inner_file.yml' );
my $SINGLE_FILE = path( $Bin, 'share', 'file.yml' );
my $DEEP_FILE = path( $Bin, 'share', 'inner_inline.yml' );
my $INNER_FILE = path( $Bin, 'share', 'inner_file.yml' );

use Beam::Wire;

Expand Down

0 comments on commit d5374da

Please sign in to comment.