| 1 | package HTML::Widgets::NavMenu::Predicate; | ||||||||||||||||||||
| 2 | 16 | $HTML::Widgets::NavMenu::Predicate::VERSION = '1.1000'; | |||||||||||||||||||
| 3 | 16 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 16 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 16 | ▶ | use parent 'HTML::Widgets::NavMenu::Object'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 16 | __PACKAGE__->mk_acc_ref( [qw(type bool regexp callback _capture)], ); | |||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 16 | ▶ | use HTML::Widgets::NavMenu::ExpandVal; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | sub _init | ||||||||||||||||||||
| 13 | 62 | ▶ | { | ||||||||||||||||||
|
Subroutine
_init: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 62 | my $self = shift; | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 62 | my %args = (@_); | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 62 | my $spec = $args{'spec'}; | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 62 | $self->_process_spec($spec); | |||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 60 | return 0; | |||||||||||||||||||
| 23 | } | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 16 | my %true_vals = ( map { $_ => 1 } (qw(1 yes true True)) ); | |||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | sub _is_true_bool | ||||||||||||||||||||
| 28 | 8 | ▶ | { | ||||||||||||||||||
|
Subroutine
_is_true_bool: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 29 | 8 | my $self = shift; | |||||||||||||||||||
| 30 | 8 | my $val = shift; | |||||||||||||||||||
| 31 | 8 | return exists( $true_vals{$val} ); | |||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 16 | my %false_vals = ( map { $_ => 1 } (qw(0 no false False)) ); | |||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | sub _is_false_bool | ||||||||||||||||||||
| 37 | 7 | ▶ | { | ||||||||||||||||||
|
Subroutine
_is_false_bool: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 38 | 7 | my $self = shift; | |||||||||||||||||||
| 39 | 7 | my $val = shift; | |||||||||||||||||||
| 40 | 7 | return exists( $false_vals{$val} ); | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | sub _get_normalized_spec | ||||||||||||||||||||
| 44 | 62 | ▶ | { | ||||||||||||||||||
|
Subroutine
_get_normalized_spec: called CC=6 SCAR=17.9
| |||||||||||||||||||||
| 45 | 62 | my $self = shift; | |||||||||||||||||||
| 46 | 62 | my $spec = shift; | |||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 62 | ▶ | if ( ref($spec) eq "HASH" ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | { | ||||||||||||||||||||
| 50 | 53 | return $spec; | |||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | 9 | ▶ | if ( ref($spec) eq "CODE" ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | { | ||||||||||||||||||||
| 54 | 1 | return +{ 'cb' => $spec }; | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | 8 | ▶ | if ( $self->_is_true_bool($spec) ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | { | ||||||||||||||||||||
| 58 | 1 | return +{ 'bool' => 1, }; | |||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | 7 | ▶ | if ( $self->_is_false_bool($spec) ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 61 | { | ||||||||||||||||||||
| 62 | 4 | return +{ 'bool' => 0, }; | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | # Default to regular expression | ||||||||||||||||||||
| 66 | 3 | ▶ | if ( ref($spec) eq "" ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | { | ||||||||||||||||||||
| 68 | 2 | return +{ 're' => $spec, }; | |||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | 1 | die "Unknown spec type!"; | |||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | sub _process_spec | ||||||||||||||||||||
| 74 | 62 | ▶ | { | ||||||||||||||||||
|
Subroutine
_process_spec: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 75 | 62 | my $self = shift; | |||||||||||||||||||
| 76 | 62 | my $spec = shift; | |||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | # TODO: Replace me with the real logic. | ||||||||||||||||||||
| 79 | 62 | $self->_assign_spec( $self->_get_normalized_spec( $spec, ), ); | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | sub _assign_spec | ||||||||||||||||||||
| 83 | 61 | ▶ | { | ||||||||||||||||||
|
Subroutine
_assign_spec: called CC=5 SCAR=16.1
| |||||||||||||||||||||
| 84 | 61 | my $self = shift; | |||||||||||||||||||
| 85 | 61 | my $spec = shift; | |||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 61 | ▶ | if ( exists( $spec->{'cb'} ) ) | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 88 | { | ||||||||||||||||||||
| 89 | 15 | $self->type("callback"); | |||||||||||||||||||
| 90 | 15 | $self->callback( $spec->{'cb'} ); | |||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | elsif ( exists( $spec->{'re'} ) ) | ||||||||||||||||||||
| 93 | { | ||||||||||||||||||||
| 94 | 29 | $self->type("regexp"); | |||||||||||||||||||
| 95 | 29 | $self->regexp( $spec->{'re'} ); | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | elsif ( exists( $spec->{'bool'} ) ) | ||||||||||||||||||||
| 98 | { | ||||||||||||||||||||
| 99 | 16 | $self->type("bool"); | |||||||||||||||||||
| 100 | 16 | $self->bool( $spec->{'bool'} ); | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | else | ||||||||||||||||||||
| 103 | { | ||||||||||||||||||||
| 104 | 1 | die "Neither 'cb' nor 're' nor 'bool' were specified in the spec."; | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 60 | ▶ | $self->_capture( ( ( !exists( $spec->{capt} ) ) ? 1 : $spec->{capt} ) ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | sub _evaluate_bool | ||||||||||||||||||||
| 111 | 101 | ▶ | { | ||||||||||||||||||
|
Subroutine
_evaluate_bool: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 112 | 101 | my ( $self, $args ) = @_; | |||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 101 | my $path_info = $args->{'path_info'}; | |||||||||||||||||||
| 115 | 101 | my $current_host = $args->{'current_host'}; | |||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 101 | my $type = $self->type(); | |||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | 101 | ▶ | if ( $type eq "callback" ) | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 120 | { | ||||||||||||||||||||
| 121 | 28 | return $self->callback()->(%$args); | |||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | elsif ( $type eq "bool" ) | ||||||||||||||||||||
| 124 | { | ||||||||||||||||||||
| 125 | 27 | return $self->bool(); | |||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | else # $type eq "regexp" | ||||||||||||||||||||
| 128 | { | ||||||||||||||||||||
| 129 | 46 | my $re = $self->regexp(); | |||||||||||||||||||
| 130 | 46 | ▶ | return ( ( $re eq "" ) || ( $path_info =~ /$re/ ) ); | ||||||||||||||||||
|
Condition100%3/3
$re eq '' || $path_info =~ /$re/
MC/DC100%2/2
$re eq '' || $path_info =~ /$re/
$re eq ''
$path_info =~ /$re/
Truth table100%3/3
A || B
A= $re eq ''B= $path_info =~ /$re/
| |||||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | sub evaluate | ||||||||||||||||||||
| 135 | 101 | ▶ | { | ||||||||||||||||||
|
Subroutine
evaluate: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 136 | 101 | my $self = shift; | |||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 101 | my $bool = $self->_evaluate_bool( {@_} ); | |||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 101 | ▶ | if ( !$bool ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 141 | { | ||||||||||||||||||||
| 142 | 50 | return $bool; | |||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | else | ||||||||||||||||||||
| 145 | { | ||||||||||||||||||||
| 146 | 51 | return HTML::Widgets::NavMenu::ExpandVal->new( | |||||||||||||||||||
| 147 | { | ||||||||||||||||||||
| 148 | capture => $self->_capture() | ||||||||||||||||||||
| 149 | }, | ||||||||||||||||||||
| 150 | ); | ||||||||||||||||||||
| 151 | } | ||||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | 16 | 1; | |||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | __END__ | ||||||||||||||||||||