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