File Coverage

blib/lib/MooX/Value.pm
Criterion Covered Total %
statement 29 30 96.6
branch 4 6 66.6
condition n/a
subroutine 10 10 100.0
pod 2 2 100.0
total 45 48 93.7


line stmt bran cond sub pod time code
1             package MooX::Value;
2              
3 9     9   61986 use warnings;
  9         15  
  9         288  
4 9     9   36 use strict;
  9         14  
  9         227  
5              
6 9     9   1516 use Moo;
  9         40900  
  9         46  
7 9     9   6593 use namespace::clean;
  9         29289  
  9         50  
8              
9             our $VERSION = '0.03';
10              
11             has value => ( is => 'ro' );
12              
13             sub BUILDARGS
14             {
15 57     57 1 50276 my ($class, $value) = @_;
16 57         952 return { 'value' => $value };
17             }
18              
19             sub BUILD
20             {
21 57     57 1 636 my ($self) = @_;
22 57         178 my ($why, $long, $data) = $self->_why_invalid( $self->{value} );
23 57 100       164 $self->_throw_exception( $why, $long, $data ) if defined $why;
24 40         203 $self->{value} = $self->_untaint( $self->value );
25 40         726 return $self;
26             }
27              
28             # A subclass must override this method or _is_invalid to be able to create value objects.
29             sub _why_invalid
30             {
31 1     1   2 my ($self, $value) = @_;
32 1 50       4 return ( ref($self) . ": Invalid parameter when creating value object.", "", undef )
33             unless $self->_is_valid( $value );
34 0         0 return;
35             }
36              
37             # A subclass must override this method or _why_invalid to be able to create value objects.
38             sub _is_valid
39             {
40 1     1   2 my ($self, $value) = @_;
41 1         7 return;
42             }
43              
44             # Default exception support just uses die to throw an exception.
45             sub _throw_exception
46             {
47 17     17   26 my ($self, $why, $longmsg, $data) = @_;
48 17         232 die $why;
49             }
50              
51             # Brute force untaint.
52             sub _untaint
53             {
54 40     40   48 my ($self, $value) = @_;
55              
56             # Can only untaint scalars
57 40 50       88 return $value if ref $value;
58              
59             # This is usually a very bad idea. It should be safe here because the class
60             # has, by definition, validated the input before we get to this function.
61             # If there is a problem, the validation code should be corrected.
62 40         94 $value =~ m/\A(.*)\z/;
63 40         140 return $1;
64             }
65              
66             1;
67             __END__