Bread::Board service B depends on accessor on service A object

153 views Asked by At

Using Bread::Board I have an object/service A with with accessor $A->foo. Object/service B depends on $A->Foo for it's contstructor. How would I do this? Here's an example of what I have

use Moose;
use Bread::Board;


has info => ( is => 'ro', lazy => 1, isa => 'Str', default => sub { 'something' } );
extends 'Bread::Board::Container';

sub BUILD {
    my $self = shift;
    container $self => as {
        service info => $self->info;

        service A => (
             class => 'A',
             dependencies => {
                 info => depends_on('info'),
             },
        );
        service B => (
            class => 'B',
            dependencies => {
                foo => depends_on('foo'), # foo could be gotten by
            },                            # ->resolve( service => 'A' )->foo 
        );                                # e.g foo is an accessor on A
    };   
}

I am not sure what code I could add or should have to make this work.

1

There are 1 answers

0
xenoterracide On BEST ANSWER

The best way I've found so far is to add another service using block for just that accessor

use Moose;
use Bread::Board;


has info => ( is => 'ro', lazy => 1, isa => 'Str', default => sub { 'something' } );
extends 'Bread::Board::Container';

sub BUILD {
    my $self = shift;
    container $self => as {
        service info => $self->info;

        service A => (
             class => 'A',
             dependencies => {
                 info => depends_on('info'),
             },
        );
        service B => (
            class => 'B',
            dependencies => {
                foo => depends_on('foo'), # foo could be gotten by
            },                            # ->resolve( service => 'A' )->foo 
        );                                # e.g foo is an accessor on A

# ADD SERVICE
        service foo => (
            block => sub {
               my $s = shift;
               return $s->param('A')->foo;
            },
            dependencies => [ 'A' ],
        );
    };   
}

This of course all assumes that A has an accessor foo