File Coverage

blib/lib/Rose/HTMLx/Form/Field/PopUpMenuNumeric.pm
Criterion Covered Total %
statement 12 24 50.0
branch 0 10 0.0
condition 0 6 0.0
subroutine 4 6 66.6
pod 2 2 100.0
total 18 48 37.5


line stmt bran cond sub pod time code
1             package Rose::HTMLx::Form::Field::PopUpMenuNumeric;
2              
3 1     1   39390 use warnings;
  1         3  
  1         32  
4 1     1   5 use strict;
  1         2  
  1         27  
5 1     1   4 use Carp;
  1         5  
  1         101  
6 1         1870 use base qw(
7             Rose::HTML::Form::Field::PopUpMenu
8 1     1   5 );
  1         2  
9              
10             our $VERSION = '0.001';
11              
12             =head1 NAME
13              
14             Rose::HTMLx::Form::Field::PopUpMenuNumeric - popup menu for numeric values
15              
16             =head1 SYNOPSIS
17              
18             use Rose::HTMLx::Form::Field::PopUpMenuNumeric;
19             my $menu = Rose::HTMLx::Form::Field::PopUpMenuNumeric->new(
20             id => 'mynum',
21             name => 'mynum',
22             type => 'menu',
23             class => 'numeric-popup',
24             label => 'My Number',
25             options => [qw( 1 2 3 )],
26             labels => { 1 => 'one', 2 => 'two', 3 => 'three' },
27             );
28             $menu->input_value('1');
29             $menu->internal_value; # returns 1
30             $menu->input_value('');
31             $menu->internal_value; # returns undef
32              
33             =head1 DESCRIPTION
34              
35             Rose::HTMLx::Form::Field::PopUpMenuNumeric is like a normal RHTMLO PopUpMenu
36             but it returns an internal_value() like Rose::HTML::Form::Field::Numeric does.
37              
38             This module exists mostly to ensure that popup menus representing numeric values
39             properly return undef instead of an empty string -- an important distinction when
40             you *really* want a numeric value and not a string.
41            
42             =head1 METHODS
43              
44             Only new or overridden method are documented here.
45              
46             =cut
47              
48             =head2 internal_value
49              
50             Cribbed verbatim from Rose::HTML::Form::Field::Numeric.
51              
52             =cut
53              
54             sub internal_value {
55 0     0 1   my ($self) = shift;
56              
57 0           my $value = $self->SUPER::internal_value(@_);
58              
59 0 0         if ( defined $value ) {
60 0           for ($value) {
61 0 0         s/-\s+/-/ || s/\+\s+//;
62             }
63             }
64              
65 0 0 0       return ( defined $value && $value =~ /\S/ ) ? $value : undef;
66             }
67              
68             # This is $RE{num}{dec} from Regexp::Common::number
69             my $Match
70             = qr{^(?:(?i)(?:[+-]?)(?:(?=[0123456789]|[.])(?:[0123456789]*)(?:(?:[.])(?:[0123456789]{0,}))?)(?:(?:[E])(?:(?:[+-]?)(?:[0123456789]+))|))$};
71              
72             =head2 validate
73              
74             Cribbed nearly verbatim from Rose::HTML::Form::Field::Numeric.
75              
76             =cut
77              
78             sub validate {
79 0     0 1   my ($self) = shift;
80              
81 0           my $ok = $self->SUPER::validate(@_);
82 0 0         return $ok unless ($ok);
83              
84 0           my $value = $self->internal_value;
85 0 0 0       return 1 unless ( defined $value && length $value );
86              
87 0           return $value =~ $Match;
88             }
89              
90             1;
91              
92             __END__