| 1 | package Bread::Board::LifeCycle::Singleton::WithParameters; | ||||||||||
| 2 | 2 | our $AUTHORITY = 'cpan:STEVAN'; | |||||||||
| 3 | # ABSTRACT: singleton lifecycle role for a parameterized service | ||||||||||
| 4 | 2 | $Bread::Board::LifeCycle::Singleton::WithParameters::VERSION = '0.37'; | |||||||||
| 5 | 2 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 2 | with 'Bread::Board::LifeCycle'; | |||||||||
| 8 | |||||||||||
| 9 | has 'instances' => ( | ||||||||||
| 10 | traits => [ 'Hash', 'NoClone' ], | ||||||||||
| 11 | is => 'rw', | ||||||||||
| 12 | isa => 'HashRef', | ||||||||||
| 13 | lazy => 1, | ||||||||||
| 14 | 1 | ▶ | default => sub { +{} }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 2 | clearer => 'flush_instances', | |||||||||
| 16 | handles => { | ||||||||||
| 17 | 'has_instance_at_key' => 'exists', | ||||||||||
| 18 | 'get_instance_at_key' => 'get', | ||||||||||
| 19 | 'set_instance_at_key' => 'set', | ||||||||||
| 20 | } | ||||||||||
| 21 | ); | ||||||||||
| 22 | |||||||||||
| 23 | 5 | ▶ | around 'get' => sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||
| 24 | 5 | my $next = shift; | |||||||||
| 25 | 5 | my $self = shift; | |||||||||
| 26 | 5 | my $key = $self->generate_instance_key(@_); | |||||||||
| 27 | |||||||||||
| 28 | # return it if we got it ... | ||||||||||
| 29 | 5 | ▶ | return $self->get_instance_at_key($key) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | if $self->has_instance_at_key($key); | ||||||||||
| 31 | |||||||||||
| 32 | # otherwise fetch it ... | ||||||||||
| 33 | 3 | my $instance = $self->$next(@_); | |||||||||
| 34 | |||||||||||
| 35 | # if we get a copy, and our copy | ||||||||||
| 36 | # has not already been set ... | ||||||||||
| 37 | 3 | ▶ | $self->set_instance_at_key($key => $instance) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | unless $self->has_instance_at_key($key); | ||||||||||
| 39 | |||||||||||
| 40 | # return whatever we have ... | ||||||||||
| 41 | 3 | return $self->get_instance_at_key($key); | |||||||||
| 42 | 2 | }; | |||||||||
| 43 | |||||||||||
| 44 | 5 | ▶ | sub generate_instance_key { | ||||||||
|
Subroutine
generate_instance_key: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 45 | 5 | my ($self, @args) = @_; | |||||||||
| 46 | 5 | ▶ | return "$self" unless @args; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 5 | return join "|" => sort map { "$_" } @args | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 2 | ▶ | no Moose::Role; 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 51 | |||||||||||
| 52 | __END__ | ||||||||||