| 1 | package Bread::Board::Container::Parameterized; | ||||||||||
| 2 | 55 | our $AUTHORITY = 'cpan:STEVAN'; | |||||||||
| 3 | 55 | $Bread::Board::Container::Parameterized::VERSION = '0.37'; | |||||||||
| 4 | 55 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 55 | ▶ | use Moose::Util 'find_meta'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 55 | ▶ | use Bread::Board::Container::FromParameterized; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | # ABSTRACT: A parameterized container | ||||||||||
| 8 | |||||||||||
| 9 | 55 | ▶ | use Bread::Board::Container; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 55 | with 'Bread::Board::Traversable'; | |||||||||
| 12 | |||||||||||
| 13 | 55 | has 'name' => ( | |||||||||
| 14 | is => 'rw', | ||||||||||
| 15 | isa => 'Str', | ||||||||||
| 16 | required => 1 | ||||||||||
| 17 | ); | ||||||||||
| 18 | |||||||||||
| 19 | 55 | has 'allowed_parameter_names' => ( | |||||||||
| 20 | is => 'ro', | ||||||||||
| 21 | isa => 'ArrayRef', | ||||||||||
| 22 | required => 1, | ||||||||||
| 23 | ); | ||||||||||
| 24 | |||||||||||
| 25 | 55 | has 'container' => ( | |||||||||
| 26 | is => 'ro', | ||||||||||
| 27 | isa => 'Bread::Board::Container', | ||||||||||
| 28 | lazy => 1, | ||||||||||
| 29 | builder => '_build_container', | ||||||||||
| 30 | handles => [qw[ | ||||||||||
| 31 | add_service | ||||||||||
| 32 | get_service | ||||||||||
| 33 | has_service | ||||||||||
| 34 | get_service_list | ||||||||||
| 35 | has_services | ||||||||||
| 36 | services | ||||||||||
| 37 | |||||||||||
| 38 | add_sub_container | ||||||||||
| 39 | get_sub_container | ||||||||||
| 40 | has_sub_container | ||||||||||
| 41 | get_sub_container_list | ||||||||||
| 42 | has_sub_containers | ||||||||||
| 43 | sub_containers | ||||||||||
| 44 | ]] | ||||||||||
| 45 | ); | ||||||||||
| 46 | |||||||||||
| 47 | 10 | ▶ | sub _build_container { | ||||||||
|
Subroutine
_build_container: called CC=1 SCAR=0.0
| |||||||||||
| 48 | 10 | my $self = shift; | |||||||||
| 49 | 10 | Bread::Board::Container->new( name => $self->name ) | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | 3 | ▶ | sub fetch { die "Cannot fetch from a parameterized container"; } | ||||||||
|
Subroutine
fetch: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 53 | 1 | ▶ | sub resolve { die "Cannot resolve from a parameterized container"; } | ||||||||
|
Subroutine
resolve: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 54 | |||||||||||
| 55 | 13 | ▶ | sub create { | ||||||||
|
Subroutine
create: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||
| 56 | 13 | my ($self, %params) = @_; | |||||||||
| 57 | |||||||||||
| 58 | 13 | my @allowed_names = sort @{ $self->allowed_parameter_names }; | |||||||||
| 59 | 13 | my @given_names = sort keys %params; | |||||||||
| 60 | |||||||||||
| 61 | 13 | ▶ | (scalar @allowed_names == scalar @given_names) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | || confess "You did not pass the correct number of parameters"; | ||||||||||
| 63 | |||||||||||
| 64 | 13 | ▶ | ((join "" => @allowed_names) eq (join "" => @given_names)) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | || confess "Incorrect parameter list, got: (" | ||||||||||
| 66 | . (join "" => @given_names) | ||||||||||
| 67 | . ") expected: (" | ||||||||||
| 68 | . (join "" => @allowed_names) | ||||||||||
| 69 | . ")"; | ||||||||||
| 70 | |||||||||||
| 71 | |||||||||||
| 72 | 13 | ▶ | my $clone = $self->container->clone( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | name => ($self->container->name eq $self->name | ||||||||||
| 74 | ? join "|" => $self->name, @given_names | ||||||||||
| 75 | : $self->container->name) | ||||||||||
| 76 | ); | ||||||||||
| 77 | |||||||||||
| 78 | 13 | my $from_parameterized_meta = find_meta('Bread::Board::Container::FromParameterized'); | |||||||||
| 79 | 13 | $clone = $from_parameterized_meta->rebless_instance($clone); | |||||||||
| 80 | |||||||||||
| 81 | 13 | ▶ | if ($self->has_parent) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 6 | my $cloned_parent = $self->parent->clone; | |||||||||
| 83 | |||||||||||
| 84 | $cloned_parent->sub_containers({ | ||||||||||
| 85 | 6 | %{ $cloned_parent->sub_containers }, | |||||||||
| 86 | $self->name => $clone, | ||||||||||
| 87 | }); | ||||||||||
| 88 | |||||||||||
| 89 | 6 | $clone->parent($cloned_parent); | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 13 | foreach my $key ( @given_names ) { | |||||||||
| 93 | $clone->add_sub_container( | ||||||||||
| 94 | 13 | $params{ $key }->clone( name => $key ) | |||||||||
| 95 | ); | ||||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | 13 | $clone; | |||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | 55 | __PACKAGE__->meta->make_immutable; | |||||||||
| 102 | |||||||||||
| 103 | 55 | ▶ | no Moose; no Moose::Util; 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 104 | |||||||||||
| 105 | __END__ | ||||||||||