| 1 | package TUI::Validate::RangeValidator; | ||||||||||||||||||||
| 2 | # ABSTRACT: integer range validator for numeric input | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 5 | ▶ | use 5.010; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 5 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 5 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 5 | our $VERSION = '2.000003'; | |||||||||||||||||||
| 9 | 5 | $VERSION =~ tr/_//d; | |||||||||||||||||||
| 10 | 5 | our $AUTHORITY = 'cpan:BRICKPOOL'; | |||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 5 | ▶ | use Exporter 'import'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 5 | our @EXPORT = qw( | |||||||||||||||||||
| 14 | TRangeValidator | ||||||||||||||||||||
| 15 | new_TRangeValidator | ||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 5 | ▶ | use Storable; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 5 | ▶ | use TUI::toolkit; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 5 | ▶ | use TUI::toolkit::Types qw( | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | is_Object | ||||||||||||||||||||
| 22 | :types | ||||||||||||||||||||
| 23 | 5 | ); | |||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 5 | ▶ | use TUI::MsgBox::Const qw( | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 26 | mfError | ||||||||||||||||||||
| 27 | mfOKButton | ||||||||||||||||||||
| 28 | 5 | ); | |||||||||||||||||||
| 29 | 5 | ▶ | use TUI::MsgBox::MsgBoxText qw( messageBox ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 30 | 5 | ▶ | use TUI::Validate::Const qw( | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 31 | voTransfer | ||||||||||||||||||||
| 32 | vtGetData | ||||||||||||||||||||
| 33 | vtSetData | ||||||||||||||||||||
| 34 | 5 | ); | |||||||||||||||||||
| 35 | 5 | ▶ | use TUI::Validate::FilterValidator; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | sub TRangeValidator() { __PACKAGE__ } | ||||||||||||||||||||
| 38 | sub name() { 'TRangeValidator' }; | ||||||||||||||||||||
| 39 | 0 | ▶ | sub new_TRangeValidator { __PACKAGE__->from(@_) } | ||||||||||||||||||
|
Subroutine
new_TRangeValidator: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 5 | extends TFilterValidator; | |||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | # declare global variables | ||||||||||||||||||||
| 44 | 5 | our $errorMsg = "Value not in the range %d to %d"; | |||||||||||||||||||
| 45 | 5 | our $validUnsignedChars = "+0123456789"; | |||||||||||||||||||
| 46 | 5 | our $validSignedChars = "+-0123456789"; | |||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | # protected attributes | ||||||||||||||||||||
| 49 | 0 | ▶ | has min => ( is => 'ro', default => sub { die 'required' } ); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 50 | 0 | ▶ | has max => ( is => 'ro', default => sub { die 'required' } ); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 0 | ▶ | sub BUILDARGS { # \%args (%args) | ||||||||||||||||||
|
Subroutine
BUILDARGS: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 53 | 0 | state $sig = signature( | |||||||||||||||||||
| 54 | method => 1, | ||||||||||||||||||||
| 55 | named => [ | ||||||||||||||||||||
| 56 | min => Int, { alias => 'aMin' }, | ||||||||||||||||||||
| 57 | max => Int, { alias => 'aMax' }, | ||||||||||||||||||||
| 58 | ], | ||||||||||||||||||||
| 59 | caller_level => +1, | ||||||||||||||||||||
| 60 | ); | ||||||||||||||||||||
| 61 | 0 | my ( $class, $args ) = $sig->( @_ ); | |||||||||||||||||||
| 62 | 0 | return { %$args }; | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 0 | ▶ | sub BUILD { # void (\%args) | ||||||||||||||||||
|
Subroutine
BUILD: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||
| 66 | 0 | my ( $self, $args ) = @_; | |||||||||||||||||||
| 67 | 0 | ▶ | assert ( @_ == 2 ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 68 | 0 | ▶ | assert ( is_Object $self ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 69 | 0 | ▶ | assert ( $self->{min} <= $self->{max} ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | 0 | ▶ | $self->{validChars} = $self->{min} >= 0 | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | ? $validUnsignedChars | ||||||||||||||||||||
| 72 | : $validSignedChars; | ||||||||||||||||||||
| 73 | 0 | return; | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 0 | ▶ | sub from { # $obj ($aMin, $aMax) | ||||||||||||||||||
|
Subroutine
from: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 77 | 0 | state $sig = signature( | |||||||||||||||||||
| 78 | method => 1, | ||||||||||||||||||||
| 79 | pos => [Int, Int], | ||||||||||||||||||||
| 80 | ); | ||||||||||||||||||||
| 81 | 0 | my ( $class, @args ) = $sig->( @_ ); | |||||||||||||||||||
| 82 | 0 | return $class->new( min => $args[0], max => $args[1] ); | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 0 | ▶ | sub error { # void () | ||||||||||||||||||
|
Subroutine
error: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 86 | 0 | state $sig = signature( | |||||||||||||||||||
| 87 | method => Object, | ||||||||||||||||||||
| 88 | pos => [], | ||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||
| 90 | 0 | my ( $self ) = $sig->( @_ ); | |||||||||||||||||||
| 91 | 0 | messageBox( mfError | mfOKButton, $errorMsg, $self->{min}, $self->{max} ); | |||||||||||||||||||
| 92 | 0 | return; | |||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 0 | ▶ | sub isValid { # $bool ($s) | ||||||||||||||||||
|
Subroutine
isValid: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||
| 96 | 0 | state $sig = signature( | |||||||||||||||||||
| 97 | method => Object, | ||||||||||||||||||||
| 98 | pos => [Str], | ||||||||||||||||||||
| 99 | ); | ||||||||||||||||||||
| 100 | 0 | my ( $self, $s ) = $sig->( @_ ); | |||||||||||||||||||
| 101 | 0 | ▶ | if ( $self->SUPER::isValid( $s ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | 0 | ▶ | if ( $s =~ /\A([+-]?\d+)\z/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | 0 | my $value = int( $1 ); | |||||||||||||||||||
| 104 | 0 | ▶ | return $value >= $self->{min} && $value <= $self->{max}; | ||||||||||||||||||
|
Condition0%0/3
$value >= $self->{'min'} && $value <= $self->{'max'}
MC/DC0%0/2
$value >= $self->{'min'} && $value <= $self->{'max'}
$value >= $self->{'min'}
$value <= $self->{'max'}
Truth table0%0/3
A && B
A= $value >= $self->{'min'}B= $value <= $self->{'max'}
| |||||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | 0 | return false; | |||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 0 | ▶ | sub transfer { # $n ($s, $buffer, $flags) | ||||||||||||||||||
|
Subroutine
transfer: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||
| 111 | 0 | state $sig = signature( | |||||||||||||||||||
| 112 | method => Object, | ||||||||||||||||||||
| 113 | pos => [Str, Any, PositiveOrZeroInt], | ||||||||||||||||||||
| 114 | ); | ||||||||||||||||||||
| 115 | 0 | my ( $self, $s, $buffer, $flags ) = $sig->( @_ ); | |||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 0 | ▶ | if ( $self->{options} & voTransfer ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 118 | 0 | ▶ | if ( $flags == vtGetData ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 119 | 0 | ▶ | if ( $s =~ /\A([+-]?\d+)\z/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 120 | 0 | $_[2] = int( $1 ); | |||||||||||||||||||
| 121 | 0 | return 1; | |||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | elsif ( $flags == vtSetData ) { | ||||||||||||||||||||
| 125 | 0 | ▶ | if ( defined $buffer ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 126 | 0 | $_[1] = sprintf( "%d", $buffer ); | |||||||||||||||||||
| 127 | 0 | return 1; | |||||||||||||||||||
| 128 | } | ||||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | 0 | return 0; | |||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 0 | ▶ | sub write :method { # void ($os) | ||||||||||||||||||
|
Subroutine
write: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 135 | 0 | state $sig = signature( | |||||||||||||||||||
| 136 | method => Object, | ||||||||||||||||||||
| 137 | pos => [Object], | ||||||||||||||||||||
| 138 | ); | ||||||||||||||||||||
| 139 | 0 | my ( $self, $os ) = $sig->( @_ ); | |||||||||||||||||||
| 140 | 0 | $self->SUPER::write( $os ); | |||||||||||||||||||
| 141 | 0 | $os->put( $self->{min} ); | |||||||||||||||||||
| 142 | 0 | $os->put( $self->{max} ); | |||||||||||||||||||
| 143 | 0 | return; | |||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | 0 | ▶ | sub read :method { # $self ($is) | ||||||||||||||||||
|
Subroutine
read: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 147 | 0 | state $sig = signature( | |||||||||||||||||||
| 148 | method => Object, | ||||||||||||||||||||
| 149 | pos => [Object], | ||||||||||||||||||||
| 150 | ); | ||||||||||||||||||||
| 151 | 0 | my ( $self, $is ) = $sig->( @_ ); | |||||||||||||||||||
| 152 | 0 | $self->SUPER::read( $is ); | |||||||||||||||||||
| 153 | 0 | $self->{min} = $is->get(); | |||||||||||||||||||
| 154 | 0 | $self->{max} = $is->get(); | |||||||||||||||||||
| 155 | 0 | return $self; | |||||||||||||||||||
| 156 | } | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | 0 | ▶ | sub build { # $obj () | ||||||||||||||||||
|
Subroutine
build: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||
| 159 | 0 | my ( $class ) = @_; | |||||||||||||||||||
| 160 | 0 | ▶ | assert ( @_ == 1 ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 161 | 0 | ▶ | assert ( is_Str $class ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 162 | 0 | ▶ | assert ( Storable::is_retrieving ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 163 | 0 | return $class->new( min => 0, max => 0 ); | |||||||||||||||||||
| 164 | } | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | 5 | 1 | |||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | __END__ | ||||||||||||||||||||