| 1 | # | ||||||||||||||||||||||||
| 2 | # This file is part of Config-Model | ||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||
| 4 | # This software is Copyright (c) 2005-2022 by Dominique Dumont. | ||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||
| 6 | # This is free software, licensed under: | ||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||
| 8 | # The GNU Lesser General Public License, Version 2.1, February 1999 | ||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||
| 10 | package Config::Model::Role::WarpMaster 2.169; | ||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||
| 12 | # ABSTRACT: register and trigger a warped element | ||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||
| 14 | 59 | ▶ | use Mouse::Role; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 15 | 59 | ▶ | use strict; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 16 | 59 | ▶ | use warnings; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||
| 18 | 59 | ▶ | use Mouse::Util; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 19 | 59 | ▶ | use Log::Log4perl qw(get_logger :levels); | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 20 | 59 | ▶ | use Scalar::Util qw/weaken/; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||
| 22 | 59 | my $logger = get_logger("Warper"); | |||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||
| 24 | has 'warp_these_objects' => ( | ||||||||||||||||||||||||
| 25 | traits => ['Array'], | ||||||||||||||||||||||||
| 26 | is => 'ro', | ||||||||||||||||||||||||
| 27 | isa => 'ArrayRef', | ||||||||||||||||||||||||
| 28 | 4326 | ▶ | default => sub { [] }, | ||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 29 | 59 | handles => { | |||||||||||||||||||||||
| 30 | _slave_info => 'elements', | ||||||||||||||||||||||||
| 31 | _add_slave_info => 'push', | ||||||||||||||||||||||||
| 32 | _delete_slave => 'delete', | ||||||||||||||||||||||||
| 33 | has_warped_slaves => 'count', | ||||||||||||||||||||||||
| 34 | # find_slave_idx => 'first_index', not available in Mouse | ||||||||||||||||||||||||
| 35 | }, | ||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||
| 38 | 505 | ▶ | sub register { | ||||||||||||||||||||||
|
Subroutine
register: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||
| 39 | 505 | my ( $self, $warped, $warper_name ) = @_; | |||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||
| 41 | 505 | my $w_name = $warped->name; | |||||||||||||||||||||||
| 42 | 505 | ▶ | $logger->debug( $self->get_type . ": " . $self->name, " registered $w_name ($warper_name)" ) | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 43 | if $logger->is_debug; | ||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||
| 45 | # weaken only applies to the passed reference, and there's no way | ||||||||||||||||||||||||
| 46 | # to duplicate a weak ref. Only a strong ref is created. See | ||||||||||||||||||||||||
| 47 | # qw(weaken) module for weaken() | ||||||||||||||||||||||||
| 48 | 505 | my @tmp = ( $warped, $w_name, $warper_name ); | |||||||||||||||||||||||
| 49 | 505 | weaken( $tmp[0] ); | |||||||||||||||||||||||
| 50 | 505 | $self->_add_slave_info( \@tmp ); | |||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||
| 52 | 505 | ▶ | return defined $self->{compute} ? 'computed' : 'regular'; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||
| 55 | 2 | ▶ | sub unregister { | ||||||||||||||||||||||
|
Subroutine
unregister: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||
| 56 | 2 | my ( $self, $w_name ) = @_; | |||||||||||||||||||||||
| 57 | 2 | ▶ | $logger->debug( $self->get_type .": " . $self->name, " unregister $w_name" ) | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 58 | if $logger->is_debug; | ||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||
| 60 | 2 | my $idx = 0; | |||||||||||||||||||||||
| 61 | 2 | foreach my $info ($self->_slave_info) { | |||||||||||||||||||||||
| 62 | 2 | ▶ | last if $info->[0] eq $w_name ; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 63 | 2 | $idx++; | |||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||
| 66 | 2 | $self->_delete_slave($idx); | |||||||||||||||||||||||
| 67 | 2 | return; | |||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||
| 70 | # And I'm going to warp them ... | ||||||||||||||||||||||||
| 71 | 128 | ▶ | sub trigger_warp { | ||||||||||||||||||||||
|
Subroutine
trigger_warp: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||
| 72 | 128 | my $self = shift; | |||||||||||||||||||||||
| 73 | 128 | my $value = shift; | |||||||||||||||||||||||
| 74 | 128 | ▶ | my $str_val = shift // $value // 'undefined'; | ||||||||||||||||||||||
|
Condition100%5/5
(shift() // $value) // "undefined"
shift() // $value
MC/DC100%2/2
(shift() // $value) // "undefined"
shift()
$value
Truth table100%3/3
A // B
A= shift()B= $value
| |||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||
| 76 | 128 | foreach my $ref ( $self->_slave_info ) { | |||||||||||||||||||||||
| 77 | 358 | my ( $warped, $w_name, $warp_index ) = @$ref; | |||||||||||||||||||||||
| 78 | 358 | ▶ | next unless defined $warped; # $warped is a weak ref and may vanish | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||
| 80 | # pure warp of object | ||||||||||||||||||||||||
| 81 | 322 | ▶ | if ($logger->is_debug) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 82 | 3 | $logger->debug("trigger_warp: ".$self->get_type." ", $self->name, | |||||||||||||||||||||||
| 83 | " warps '$w_name' with value <$str_val> "); | ||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||
| 85 | 322 | $warped->trigger( $value, $warp_index ); | |||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||
| 87 | 127 | return; | |||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||
| 90 | 1405 | ▶ | sub get_warped_slaves { | ||||||||||||||||||||||
|
Subroutine
get_warped_slaves: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||
| 91 | 1405 | my $self = shift; | |||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||
| 93 | # grep is used to clean up weak ref to object that were destroyed | ||||||||||||||||||||||||
| 94 | 1405 | return grep { defined $_ } map { $_->[0] } $self->_slave_info; | |||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||
| 97 | 59 | 1; | |||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||
| 99 | __END__ | ||||||||||||||||||||||||