Bread :: Board - Input parameters with restrictions of type ArrayRef?

Using Mooseand Bread::Board, is it possible to create an object with an attribute that has a type constraint ArrayRef[SomeObject]and enter this parameter in such a way that:

  • Restriction is ArrayRefsupported,
  • Each object that is a member of this ArrayRef array has all its dependencies encountered Bread::Board, and
  • Every object that is a member of this ArrayRef is an object created Bread::Board?

To make sure that I am clearly explaining myself, consider an incredibly naive example. Let them say that we have a class Wheel:

package Wheel;
use Moose;

has ['size', 'maker'] => (isa => 'Str', is => 'rw', required => 1);

And create a class Vehiclewhere each instance contains a bunch of wheels:

package Vehicle;
use Moose;

has 'wheels' => (
    is => 'rw',
    isa => 'ArrayRef[Wheel]',
    required => 1,
);

Wheel, , , Vehicle? :

my $c = container 'app' => as {
    container 'wheels' => as {
        service 'maker' => "Bob Tires";
        service 'size' => "195R65/15";
        service 'carTires' => (
            class   => 'Wheel',
            dependencies => [ depends_on('maker'), depends_on('size') ],
        )
    };

    container 'vehicles' => as {
        service 'sedan' => (
            class   => 'Vehicle',
            dependencies => {
                # WON'T WORK
                wheels => depends_on('/wheels/carTires'),
            }
        )
    };
};
my $v = $c->resolve(service => 'vehicles/sedan');

? , , Vehicle, , , , , .:-) .

+3
1

carTires ArrayRef :

container 'wheels' => as {
    service 'carTires' => (
        block => sub {
            return [ map {Wheel->new} 1..4 ];
        },
    )
};

, ArrayRef [Wheel].

, , :

container 'wheels' => as {
    service 'carTires' => (
        block => sub {
            return [ map {Window->new} 1..4 ];
        },
    )
};
+4

All Articles