Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: p5-mop
Fetching contributors…

Cannot retrieve contributors at this time

file 73 lines (53 sloc) 1.617 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
package Bread::Board;
use v5.16;
use warnings;
use mop;

use Bread::Board::Util qw(coerce_dependencies);

use Carp 'confess';
use Scalar::Util 'blessed';

class ConstructorInjection with Bread::Board::Service::WithClass,
                                Bread::Board::Service::WithParameters,
                                Bread::Board::Service::WithDependencies {

    has $!constructor_name is rw, lazy = $_->_build_constructor_name;

    method new ($class: %args) {
        confess '$class is required'
            unless exists $args{'class'};
        $args{'class_name'} = delete $args{'class'};
        coerce_dependencies( \%args );
        $class->next::method( %args );
    }

    method BUILD {
        $_->parent($self) foreach values %{ $self->dependencies };
    }

    method init_params {
        # NOTE:
        # this is tricky, cause we need to call
        # the underlying init_params in Service
        # but we can't easily do that since it
        # is a role.
        # - SL
        +{ %{ +{} }, $self->resolve_dependencies }
    }

    method get {
        $self->get_class;
        $self->prepare_parameters( @_ );
        my $result = $self->class->$!constructor_name( %{ $self->params } );
        $self->clean_parameters;
        $self->clear_params;
        return $result;
    }

    method _build_constructor_name { 'new' }
}

no mop;

__END__

=pod

=head1 DESCRIPTION

=head1 METHODS

=over 4

=item B<get>

=back

=head1 BUGS

All complex software has bugs lurking in it, and this module is no
exception. If you find a bug please either email me, or add the bug
to cpan-RT.

=cut
Something went wrong with that request. Please try again.