| 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::BackendTrackOrder 2.169; | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | # ABSTRACT: Track read order of elements from configuration | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 3 | ▶ | use Mouse; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 3 | ▶ | use Mouse::Util; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 3 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 3 | ▶ | use v5.20; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 3 | ▶ | use feature qw/postderef signatures/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 22 | 3 | ▶ | no warnings qw/experimental::postderef experimental::signatures/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 3 | ▶ | use Log::Log4perl qw(get_logger :levels); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 3 | my $logger = get_logger("BackendTrackOrder"); | |||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 3 | has backend_obj => ( | |||||||||||||||||||
| 29 | is => 'ro', | ||||||||||||||||||||
| 30 | isa => 'Config::Model::Backend::Any', | ||||||||||||||||||||
| 31 | weak_ref => 1, | ||||||||||||||||||||
| 32 | required => 1, | ||||||||||||||||||||
| 33 | handles => [qw/node get_element_names/], | ||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | has _creation_order => ( | ||||||||||||||||||||
| 37 | is => 'bare', | ||||||||||||||||||||
| 38 | isa => 'ArrayRef[Str]', | ||||||||||||||||||||
| 39 | traits => ['Array'], | ||||||||||||||||||||
| 40 | 8 | ▶ | default => sub { [] }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 41 | 3 | handles => { | |||||||||||||||||||
| 42 | _register_element => 'push', | ||||||||||||||||||||
| 43 | get_element_names_as_created => 'elements', | ||||||||||||||||||||
| 44 | _insert_element => 'insert', | ||||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | has _created => ( | ||||||||||||||||||||
| 49 | is => 'rw', | ||||||||||||||||||||
| 50 | isa => 'HashRef[Str]', | ||||||||||||||||||||
| 51 | traits => ['Hash'], | ||||||||||||||||||||
| 52 | 8 | ▶ | default => sub { {} }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 53 | 3 | handles => { | |||||||||||||||||||
| 54 | register_created => 'set', | ||||||||||||||||||||
| 55 | has_created => 'exists', | ||||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | # keeping order in Node does not make sense: one must read parameter | ||||||||||||||||||||
| 60 | # in canonical order to enable warp mechanism from one elemnet to the | ||||||||||||||||||||
| 61 | # other, so the read order will never differ from the canonical | ||||||||||||||||||||
| 62 | # order. Only some elements will be missing | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | # What about default values, not created, no store done ???? | ||||||||||||||||||||
| 65 | # -> when writing back, mix all elements from canonical list into existing list ... | ||||||||||||||||||||
| 66 | # or at the end of initial load ??? | ||||||||||||||||||||
| 67 | # or mixall at the end of init() ? | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 57 | ▶ | sub register_element ($self, $name) { | ||||||||||||||||||
|
Subroutine
register_element: called CC=10 SCAR=23.9
Pod
documented
| |||||||||||||||||||||
| 70 | 57 | ▶ | return if $self->has_created($name); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 35 | $self->register_created($name => 1 ); | |||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 35 | ▶ | if ($self->node->instance->initial_load) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | 26 | $logger->debug("registering $name during init"); | |||||||||||||||||||
| 76 | 26 | $self->_register_element($name); | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | else { | ||||||||||||||||||||
| 79 | # try to keep canonical order | ||||||||||||||||||||
| 80 | 9 | my $i = 1; | |||||||||||||||||||
| 81 | 9 | my %has = map{ ($_ , $i++ ) } $self->get_element_names_as_created; | |||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 9 | my $found_me = 0; | |||||||||||||||||||
| 84 | 9 | my $previous_idx = 0 ; | |||||||||||||||||||
| 85 | 9 | my $previous_name ; | |||||||||||||||||||
| 86 | # traverse the canonical list in reverse order (which includes | ||||||||||||||||||||
| 87 | # accepted elements) ... | ||||||||||||||||||||
| 88 | 9 | foreach my $std (reverse @{ $self->node->{model}{element_list} }) { | |||||||||||||||||||
| 89 | # ... until the new element is found in the canonical list ... | ||||||||||||||||||||
| 90 | 59 | ▶ | if ($name eq $std) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 91 | 9 | $found_me++; | |||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | # ... and the first previous element from the canonical | ||||||||||||||||||||
| 94 | # list already existing in the existing list is found | ||||||||||||||||||||
| 95 | ▶ | elsif ($found_me and $has{$std}) { | |||||||||||||||||||
|
Condition66%2/3
$found_me and $has{$std}
MC/DC50%1/2
$found_me and $has{$std}
$found_me
$has{$std}
Truth table66%2/3
A and B
A= $found_meB= $has{$std}
| |||||||||||||||||||||
| 96 | 9 | $previous_idx = $has{$std}; | |||||||||||||||||||
| 97 | 9 | $previous_name = $std; | |||||||||||||||||||
| 98 | 9 | last; | |||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | # then insert this element in the existing list after the | ||||||||||||||||||||
| 103 | # previous element (which may be 0, if the previous element | ||||||||||||||||||||
| 104 | # was not found, i.e. do an unshift). push it if search has failed. | ||||||||||||||||||||
| 105 | 9 | ▶ | if ($found_me) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 106 | 9 | ▶ | if ($logger->is_debug) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 107 | 0 | ▶ | my $str = $previous_name ? "after $previous_name" : "at beginning"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 108 | 0 | $logger->debug("registering $name $str"); | |||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | 9 | $self->_insert_element($previous_idx, $name); | |||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | else { | ||||||||||||||||||||
| 113 | 0 | $logger->debug("registering $name at end of list"); | |||||||||||||||||||
| 114 | 0 | $self->_register_element($name); | |||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | 35 | return; | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 7 | ▶ | sub get_ordered_element_names ($self) { | ||||||||||||||||||
|
Subroutine
get_ordered_element_names: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 121 | 7 | ▶ | if ($self->node->instance->canonical) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 122 | 2 | return $self->get_element_names; | |||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | else { | ||||||||||||||||||||
| 125 | # triggers a registration of all remaining elements in _creation_order | ||||||||||||||||||||
| 126 | 5 | for ( $self->get_element_names ) { | |||||||||||||||||||
| 127 | 31 | $self->register_element($_); | |||||||||||||||||||
| 128 | } | ||||||||||||||||||||
| 129 | 5 | return $self->get_element_names_as_created; | |||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 3 | 1; | |||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | __END__ | ||||||||||||||||||||