Skip to content

Commit

Permalink
allow config services to be used as all arguments
Browse files Browse the repository at this point in the history
This allows you to put the entire hash of object arguments in another
file, if you need. It makes meta references more consistent, as they can
be used directly as `args`.

The end goal is that everything should be using the meta reference
syntax, even regular services.
  • Loading branch information
preaction committed Mar 1, 2015
1 parent 8918d27 commit 862ee66
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 18 deletions.
62 changes: 44 additions & 18 deletions lib/Beam/Wire.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package Beam::Wire;
use strict;
use warnings;

use Scalar::Util qw( blessed );
use Moo;
use Config::Any;
use Module::Runtime qw( use_module );
Expand Down Expand Up @@ -308,6 +309,22 @@ object.
conf:
$config: db_config.yml
The config file can be used as all the arguments to the service:
# container.yml
dbobj:
class: My::DB
args:
$config: db_config.yml
In this example, the constructor will be called like:
my $dbobj = My::DB->new(
dsn => 'dbi:mysql:dbname',
user => 'mysql',
pass => '12345',
);
You can reference individual items in a configuration hash using C<$path>
references:
Expand Down Expand Up @@ -633,32 +650,41 @@ sub parse_args {
return if not $args;
my @args;
if ( ref $args eq 'ARRAY' ) {
@args = @{$args};
@args = $self->find_refs( @{$args} );
}
elsif ( ref $args eq 'HASH' ) {
@args = %{$args};
}
else {
# Try anyway?
@args = $args;
}
if ( $class->isa( 'Beam::Wire' ) ) {
my %args = @args;
# Hash args could be a ref
# Subcontainers cannot scan for refs in their configs
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 ( $class->isa( 'Beam::Wire' ) ) {
my %args = %{$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} );
}
@args = $self->find_refs( %args );
if ( $config ) {
push @args, config => $config;
}
}
@args = $self->find_refs( %args );
if ( $config ) {
push @args, config => $config;
else {
my ( $maybe_ref ) = $self->find_refs( $args );
if ( blessed $maybe_ref ) {
@args = ( $maybe_ref );
}
else {
@args = ref $maybe_ref eq 'HASH' ? %$maybe_ref
: ref $maybe_ref eq 'ARRAY' ? @$maybe_ref
: ( $maybe_ref );
}
}
}
else {
@args = $self->find_refs( @args );
# Try anyway?
@args = $args;
}

return @args;
}

Expand Down
19 changes: 19 additions & 0 deletions t/15_config_service.t
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,25 @@ subtest 'anonymous configs' => sub {
lives_ok { $svc = $wire->get( 'foo' ) };
isa_ok $svc, 'Foo';
cmp_deeply $svc->foo, $EXPECT;

subtest 'use a config as all the arguments' => sub {
my $wire = Beam::Wire->new(
config => {
foo => {
class => 'Foo',
args => {
'$config' => $SHARE_DIR->child( config => 'config.yml' )->stringify,
},
},
},
);

my $svc;
lives_ok { $svc = $wire->get( 'foo' ) };
isa_ok $svc, 'Foo';
cmp_deeply $svc->foo, $EXPECT->{foo};

};
};

subtest 'config references' => sub {
Expand Down

0 comments on commit 862ee66

Please sign in to comment.