| 1 | package Bread::Board::Service; | ||||||||||
| 2 | 80 | our $AUTHORITY = 'cpan:STEVAN'; | |||||||||
| 3 | # ABSTRACT: Base service role | ||||||||||
| 4 | 80 | $Bread::Board::Service::VERSION = '0.37'; | |||||||||
| 5 | 80 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 80 | ▶ | use Module::Runtime (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 80 | ▶ | use Moose::Util::TypeConstraints 'find_type_constraint'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 80 | with 'Bread::Board::Traversable'; | |||||||||
| 11 | |||||||||||
| 12 | 80 | has 'name' => ( | |||||||||
| 13 | is => 'rw', | ||||||||||
| 14 | isa => 'Str', | ||||||||||
| 15 | required => 1 | ||||||||||
| 16 | ); | ||||||||||
| 17 | |||||||||||
| 18 | 80 | has 'params' => ( | |||||||||
| 19 | traits => [ 'Hash' ], | ||||||||||
| 20 | is => 'rw', | ||||||||||
| 21 | isa => 'HashRef', | ||||||||||
| 22 | lazy => 1, | ||||||||||
| 23 | builder => 'init_params', | ||||||||||
| 24 | clearer => 'clear_params', | ||||||||||
| 25 | handles => { | ||||||||||
| 26 | get_param => 'get', | ||||||||||
| 27 | get_param_keys => 'keys', | ||||||||||
| 28 | _clear_param => 'delete', | ||||||||||
| 29 | _set_param => 'set', | ||||||||||
| 30 | } | ||||||||||
| 31 | ); | ||||||||||
| 32 | |||||||||||
| 33 | has 'is_locked' => ( | ||||||||||
| 34 | is => 'rw', | ||||||||||
| 35 | isa => 'Bool', | ||||||||||
| 36 | 283 | ▶ | default => sub { 0 } | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 37 | 80 | ); | |||||||||
| 38 | |||||||||||
| 39 | has 'lifecycle' => ( | ||||||||||
| 40 | is => 'rw', | ||||||||||
| 41 | isa => 'Str', | ||||||||||
| 42 | 29 | ▶ | trigger => sub { | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||
| 43 | 29 | my ($self, $lifecycle) = @_; | |||||||||
| 44 | 29 | ▶ | if ($self->does('Bread::Board::LifeCycle')) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 1 | my $base = (Class::MOP::class_of($self)->superclasses)[0]; | |||||||||
| 46 | 1 | Class::MOP::class_of($base)->rebless_instance_back($self); | |||||||||
| 47 | 1 | ▶ | return if $lifecycle eq 'Null'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 28 | ▶ | my $lifecycle_role = $lifecycle =~ /^\+/ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | ? substr($lifecycle, 1) | ||||||||||
| 52 | : "Bread::Board::LifeCycle::${lifecycle}"; | ||||||||||
| 53 | 28 | Module::Runtime::require_module($lifecycle_role); | |||||||||
| 54 | 28 | Class::MOP::class_of($lifecycle_role)->apply($self); | |||||||||
| 55 | } | ||||||||||
| 56 | 80 | ); | |||||||||
| 57 | |||||||||||
| 58 | 224 | ▶ | sub init_params { +{} } | ||||||||
|
Subroutine
init_params: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 59 | 89 | ▶ | sub param { | ||||||||
|
Subroutine
param: called CC=5 SCAR=24.1
Pod
documented
| |||||||||||
| 60 | 89 | my $self = shift; | |||||||||
| 61 | 89 | ▶ | return $self->get_param_keys if scalar @_ == 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 79 | ▶ | return $self->get_param( $_[0] ) if scalar @_ == 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 0 | ▶ | ((scalar @_ % 2) == 0) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | || confess "parameter assignment must be an even numbered list"; | ||||||||||
| 65 | 0 | my %new = @_; | |||||||||
| 66 | 0 | ▶ | while (my ($key, $value) = each %new) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | 0 | $self->set_param( $key => $value ); | |||||||||
| 68 | } | ||||||||||
| 69 | 0 | return; | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | { | ||||||||||
| 73 | 80 | my %mergeable_params = ( | |||||||||
| 74 | dependencies => { | ||||||||||
| 75 | interface => 'Bread::Board::Service::WithDependencies', | ||||||||||
| 76 | constraint => 'Bread::Board::Service::Dependencies', | ||||||||||
| 77 | }, | ||||||||||
| 78 | parameters => { | ||||||||||
| 79 | interface => 'Bread::Board::Service::WithParameters', | ||||||||||
| 80 | constraint => 'Bread::Board::Service::Parameters', | ||||||||||
| 81 | }, | ||||||||||
| 82 | ); | ||||||||||
| 83 | |||||||||||
| 84 | 7 | ▶ | sub clone_and_inherit_params { | ||||||||
|
Subroutine
clone_and_inherit_params: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||
| 85 | 7 | my ($self, %params) = @_; | |||||||||
| 86 | |||||||||||
| 87 | confess "Changing a service's class is not possible when inheriting" | ||||||||||
| 88 | 7 | ▶ | unless $params{service_class} eq blessed $self; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | |||||||||||
| 90 | 3 | for my $p (keys %mergeable_params) { | |||||||||
| 91 | 6 | ▶ | if (exists $params{$p}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | 2 | ▶ | if ($self->does($mergeable_params{$p}->{interface})) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | 2 | my $type = find_type_constraint $mergeable_params{$p}->{constraint}; | |||||||||
| 94 | |||||||||||
| 95 | 2 | my $val = $type->assert_coerce($params{$p}); | |||||||||
| 96 | |||||||||||
| 97 | $params{$p} = { | ||||||||||
| 98 | 2 | %{ $self->$p }, | |||||||||
| 99 | 2 | %{ $val }, | |||||||||
| 100 | }; | ||||||||||
| 101 | } | ||||||||||
| 102 | else { | ||||||||||
| 103 | 0 | confess "Trying to add $p to a service not supporting them"; | |||||||||
| 104 | } | ||||||||||
| 105 | } | ||||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | 3 | $self->clone(%params); | |||||||||
| 109 | } | ||||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | 80 | requires 'get'; | |||||||||
| 113 | |||||||||||
| 114 | 213 | ▶ | sub lock { (shift)->is_locked(1) } | ||||||||
|
Subroutine
lock: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 115 | 213 | ▶ | sub unlock { (shift)->is_locked(0) } | ||||||||
|
Subroutine
unlock: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 116 | |||||||||||
| 117 | 80 | ▶ | no Moose::Util::TypeConstraints; no Moose::Role; 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 118 | |||||||||||
| 119 | __END__ | ||||||||||