| 1 | package Form::Sensible::Field::FileSelector; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | extends 'Form::Sensible::Field'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | has 'filename' => ( | ||||||||||||||||||||||||||||
| 10 | is => 'rw', | ||||||||||||||||||||||||||||
| 11 | isa => 'Str', | ||||||||||||||||||||||||||||
| 12 | lazy => 1, | ||||||||||||||||||||||||||||
| 13 | 1 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | return scalar fileparse(shift->full_path); | |||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||
| 16 | 2 | ); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | has 'full_path' => ( | ||||||||||||||||||||||||||||
| 19 | is => 'rw', | ||||||||||||||||||||||||||||
| 20 | isa => 'Str', | ||||||||||||||||||||||||||||
| 21 | lazy => 1, | ||||||||||||||||||||||||||||
| 22 | 1 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 1 | return shift->value(); | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | 2 | ); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | has 'valid_extensions' => ( | ||||||||||||||||||||||||||||
| 28 | is => 'rw', | ||||||||||||||||||||||||||||
| 29 | isa => 'ArrayRef', | ||||||||||||||||||||||||||||
| 30 | required => 1, | ||||||||||||||||||||||||||||
| 31 | 4 | ▶ | default => sub { return [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 2 | ); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 2 | has 'maximum_size' => ( | |||||||||||||||||||||||||||
| 35 | is => 'rw', | ||||||||||||||||||||||||||||
| 36 | isa => 'Int', | ||||||||||||||||||||||||||||
| 37 | required => 1, | ||||||||||||||||||||||||||||
| 38 | default => 0, | ||||||||||||||||||||||||||||
| 39 | lazy => 1, | ||||||||||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 2 | has 'must_exist' => ( | |||||||||||||||||||||||||||
| 43 | is => 'rw', | ||||||||||||||||||||||||||||
| 44 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 45 | required => 1, | ||||||||||||||||||||||||||||
| 46 | default => 1, | ||||||||||||||||||||||||||||
| 47 | lazy => 1, | ||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 2 | has 'must_be_readable' => ( | |||||||||||||||||||||||||||
| 51 | is => 'rw', | ||||||||||||||||||||||||||||
| 52 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 53 | required => 1, | ||||||||||||||||||||||||||||
| 54 | default => 1, | ||||||||||||||||||||||||||||
| 55 | lazy => 1, | ||||||||||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | sub get_additional_configuration { | ||||||||||||||||||||||||||
|
Subroutine
get_additional_configuration: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | return { map { $_ => $self->$_ } | |||||||||||||||||||||||||||
| 62 | qw(maximum_size valid_extensions must_exist must_be_readable) | ||||||||||||||||||||||||||||
| 63 | }; | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 1 | ▶ | around 'validate' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=25.6
| |||||||||||||||||||||||||||||
| 67 | 1 | my $orig = shift; | |||||||||||||||||||||||||||
| 68 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 1 | my @errors; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 1 | push @errors, $self->$orig(@_); | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # file must exist. | ||||||||||||||||||||||||||||
| 75 | 1 | ▶ | if (defined($self->filename)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 1 | ▶ | if ($self->must_exist && ! -e $self->full_path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->must_exist and not -e $self->full_path
MC/DC0%0/2
$self->must_exist and not -e $self->full_path
$self->must_exist
not -e $self->full_path
Truth table33%1/3
A and not(B)
A= $self->must_existB= not -e $self->full_path
| |||||||||||||||||||||||||||||
| 77 | 1 | push @errors, "_FIELDNAME_ does not exist."; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 1 | ▶ | if ($#{$self->valid_extensions} != -1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 1 | ▶ | if ( ! grep { $self->filename =~ /\.$_$/ } @{$self->valid_extensions} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | { | ||||||||||||||||||||||||||||
| 82 | 1 | push @errors, "_FIELDNAME_ is not a valid file type"; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 1 | ▶ | if ($self->must_be_readable && ! -r $self->full_path ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->must_be_readable and not -r $self->full_path
MC/DC0%0/2
$self->must_be_readable and not -r $self->full_path
$self->must_be_readable
not -r $self->full_path
Truth table33%1/3
A and not(B)
A= $self->must_be_readableB= not -r $self->full_path
| |||||||||||||||||||||||||||||
| 87 | 1 | push @errors, "_FIELDNAME_ is not readable"; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | 1 | ▶ | if ($self->maximum_size) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 1 | ▶ | if (-s $self->full_path > $self->maximum_size) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | push @errors, "_FIELDNAME_ is too large"; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 1 | return @errors; | |||||||||||||||||||||||||||
| 96 | 2 | }; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 99 | 2 | 1; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | __END__ | ||||||||||||||||||||||||||||