| 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::TreeSearcher 2.169; | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 59 | ▶ | use Mouse; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 59 | ▶ | use Mouse::Util::TypeConstraints; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 59 | ▶ | use List::MoreUtils qw/any/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 59 | ▶ | use Log::Log4perl qw(get_logger :levels); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 59 | ▶ | use Config::Model::Exception; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 59 | ▶ | use Config::Model::ObjTreeScanner; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 59 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 59 | my @search_types = qw/element value key summary description help/; | |||||||||||||||||||||||||||||||||||||||||
| 22 | 59 | enum( 'SearchType' => [ @search_types, 'all' ] ); | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | # clean up namespace to avoid clash between MUTC keywords and | ||||||||||||||||||||||||||||||||||||||||||
| 25 | # my functions | ||||||||||||||||||||||||||||||||||||||||||
| 26 | # See http://www.nntp.perl.org/group/perl.moose/2010/10/msg1935.html | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 59 | ▶ | no Mouse::Util::TypeConstraints; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 59 | has 'node' => ( | |||||||||||||||||||||||||||||||||||||||||
| 30 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 31 | isa => 'Config::Model::Node', | ||||||||||||||||||||||||||||||||||||||||||
| 32 | weak_ref => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 33 | required => 1 | ||||||||||||||||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 59 | has 'type' => ( is => 'ro', isa => 'SearchType' ); | |||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 59 | has '_type_hash' => ( | |||||||||||||||||||||||||||||||||||||||||
| 39 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 40 | isa => 'HashRef[Bool]', | ||||||||||||||||||||||||||||||||||||||||||
| 41 | builder => '_build_type_hash', | ||||||||||||||||||||||||||||||||||||||||||
| 42 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 59 | my $logger = get_logger("TreeSearcher"); | |||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 9 | ▶ | sub _build_type_hash { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_type_hash: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 9 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 9 | my $t = $self->type; | |||||||||||||||||||||||||||||||||||||||||
| 50 | 9 | ▶ | my $def = $t eq 'all' ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 9 | my %res = map { $_ => $def; } @search_types; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 9 | ▶ | $res{$t} = 1 unless $t eq 'all'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 9 | return \%res; | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 9 | ▶ | sub search { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
search: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 9 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 9 | my $string = shift; # string to search, can be a regexp | |||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 9 | $logger->trace( "TreeSearcher: creating scanner for " . $self->node->name ); | |||||||||||||||||||||||||||||||||||||||||
| 61 | 9 | my $reg = qr/$string/i; | |||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 9 | my @scanner_args; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 9 | my $need_search = $self->_build_type_hash; | |||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 549 | ▶ | push @scanner_args, leaf_cb => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=19.1
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 549 | my ( $scanner, $data_ref, $node, $element_name, $index, $leaf_object ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 549 | my $loc = $leaf_object->location; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 549 | $logger->debug("TreeSearcher: scanning leaf $loc"); | |||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 549 | my $v = $leaf_object->fetch( check => 'no' ); | |||||||||||||||||||||||||||||||||||||||||
| 73 | 549 | ▶ | if ( $need_search->{value} and defined $v and $v =~ $reg ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$need_search->{"value"} and defined $v and $v =~ /$reg/
$need_search->{"value"} and defined $v
MC/DC100%3/3
$need_search->{"value"} and defined $v and $v =~ /$reg/
$need_search->{"value"}
defined $v
$v =~ /$reg/
Truth table100%4/4
A and B and C
A= $need_search->{"value"}B= defined $vC= $v =~ /$reg/
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 10 | $data_ref->($loc); | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 549 | ▶ | if ( $need_search->{help} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | my $help_ref = $leaf_object->get_help; | |||||||||||||||||||||||||||||||||||||||||
| 78 | $data_ref->($loc) | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | if any { $_ =~ $reg; } values %$help_ref; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 9 | }; | |||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 54 | ▶ | push @scanner_args, hash_element_cb => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 54 | my ( $scanner, $data_ref, $node, $element_name, @keys ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 54 | my $loc = $node->location; | |||||||||||||||||||||||||||||||||||||||||
| 86 | 54 | ▶ | $loc .= ' ' if $loc; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 54 | $loc .= $element_name; | |||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 54 | $logger->debug("TreeSearcher: scanning hash $loc"); | |||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 54 | foreach my $k (@keys) { | |||||||||||||||||||||||||||||||||||||||||
| 92 | 45 | ▶ | if ( $need_search->{key} and $k =~ $reg ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$need_search->{"key"} and $k =~ /$reg/
MC/DC100%2/2
$need_search->{"key"} and $k =~ /$reg/
$need_search->{"key"}
$k =~ /$reg/
Truth table100%3/3
A and B
A= $need_search->{"key"}B= $k =~ /$reg/
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 1 | my $hloc = $node->fetch_element($element_name)->fetch_with_id($k)->location; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 1 | $data_ref->($hloc); | |||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 45 | $scanner->scan_hash( $data_ref, $node, $element_name, $k ); | |||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 9 | }; | |||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 126 | ▶ | push @scanner_args, node_content_cb => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.6
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 126 | my ( $scanner, $data_ref, $node, @element ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 126 | my $loc = $node->location; | |||||||||||||||||||||||||||||||||||||||||
| 103 | 126 | $logger->debug("TreeSearcher: scanning node $loc"); | |||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 126 | foreach my $e (@element) { | |||||||||||||||||||||||||||||||||||||||||
| 106 | 693 | my $store = 0; | |||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 693 | for ( qw/description summary/ ) { | |||||||||||||||||||||||||||||||||||||||||
| 109 | 1386 | ▶ | if ($need_search->{$_} and $node->get_help_as_text( $_ => $e ) =~ $reg) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$need_search->{$_} and $node->get_help_as_text($_, $e) =~ /$reg/
MC/DC100%2/2
$need_search->{$_} and $node->get_help_as_text($_, $e) =~ /$reg/
$need_search->{$_}
$node->get_help_as_text($_, $e) =~ /$reg/
Truth table100%3/3
A and B
A= $need_search->{$_}B= $node->get_help_as_text($_, $e) =~ /$reg/
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 4 | $store = 1; | |||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 693 | ▶ | if ($need_search->{element} and $e =~ $reg) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$need_search->{"element"} and $e =~ /$reg/
MC/DC0%0/2
$need_search->{"element"} and $e =~ /$reg/
$need_search->{"element"}
$e =~ /$reg/
Truth table33%1/3
A and B
A= $need_search->{"element"}B= $e =~ /$reg/
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | $store = 1; | |||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 693 | ▶ | $data_ref->( $loc ? $loc . ' ' . $e : $e ) if $store; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 693 | $scanner->scan_element( $data_ref, $node, $e ); | |||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 9 | }; | |||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | 9 | my $scan = Config::Model::ObjTreeScanner->new( @scanner_args, ); | |||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | # use hash to avoid duplication of path | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 9 | my @loc; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 15 | ▶ | my $store_sub = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 15 | my $p = shift; | |||||||||||||||||||||||||||||||||||||||||
| 129 | 15 | ▶ | return if @loc and $loc[-1] eq $p; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@loc and $loc[-1] eq $p
MC/DC0%0/2
@loc and $loc[-1] eq $p
@loc
$loc[-1] eq $p
Truth table66%2/3
A and B
A= @locB= $loc[-1] eq $p
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 15 | $logger->trace("TreeSearcher: storing location '$p'"); | |||||||||||||||||||||||||||||||||||||||||
| 131 | 15 | push @loc, $p; | |||||||||||||||||||||||||||||||||||||||||
| 132 | 9 | }; | |||||||||||||||||||||||||||||||||||||||||
| 133 | 9 | $scan->scan_node( $store_sub, $self->node ); | |||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 9 | return @loc; | |||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 59 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | 59 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | # ABSTRACT: Search tree for match in value, description... | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | __END__ | ||||||||||||||||||||||||||||||||||||||||||