lib/Form/Sensible/Field/DateTime.pm

filter: Statementstmt 75.0% 66 / 88 Branchbran 66.6% 24 / 36 Conditioncond 22.2% 2 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 65.0% 13 / 20 Podpod 100% 4 / 4 totaltotal 67.4% 110 / 163 CC 22 SCAR 19.3
CC
22
Cov
69%
get_options
30.0
_validate_span
6.0
__ANON__
6.0
CRAP 6.9
« lib/Form/Sensible/Validator.pm ↑ Summary lib/Form/Sensible/Form.pm »
1 package Form::Sensible::Field::DateTime;
2  
3 16 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use DateTime::Format::Natural;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use DateTime::Span;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use Form::Sensible::DelegateConnection;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 extends 'Form::Sensible::Field';
10  
11 16 coerce 'CodeRef' => from 'Str' => via \&_string_to_code_recurrence;
12  
13 has '+value_delegate' => (
14 6 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
15 6 my $self = shift;
16 6 my $value = _validate_datetime( $self->default_value );
17 1051 ▶ my $sub = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
18 1051 my $caller = shift;
19 1051 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 38 1013 1051
20 38 my $valid = _validate_datetime($_[0]);
21 38 ▶ $value = $valid if defined $valid;
Branch: 1 branch
BranchTrueFalseTotal
if defined $valid 35 3 38
22 }
23 1051 return $value;
24 6 };
25 6 return FSConnector($sub);
26 },
27 16 );
28  
29 16 has 'recurrence' => (
30 is => 'rw',
31 isa => 'CodeRef',
32 required => 0,
33 coerce => 1,
34 );
35  
36 16 has 'span' => (
37 is => 'rw',
38 isa => 'DateTime::Span',
39 required => 0,
40 );
41  
42 44 ▶sub _validate_datetime {
Subroutine _validate_datetime: called CC=5 SCAR=16.1
43 44 my $value = shift;
44  
45 44 ▶ return if ! defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 6 38 44
46 38 ▶ return $value if 'DateTime' eq ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if "DateTime" eq ref $value 31 7 38
47  
48 7 my $parser = DateTime::Format::Natural->new;
49 7 my $date_string = $parser->extract_datetime( $value );
50  
51 7 ▶ if ( $date_string ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date_string) 4 3 7
52 4 my $dt = $parser->parse_datetime( $date_string );
53 4 ▶ return $dt if $parser->success;
Branch: 1 branch
BranchTrueFalseTotal
if $parser->success 4 0 4
54 }
55  
56 3 return;
57 }
58  
59 198 ▶around 'validate' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
60 198 my $orig = shift;
61 198 my $self = shift;
62  
63 198 my @errors;
64 198 push @errors, $self->$orig(@_);
65  
66 198 my $span_error = $self->_validate_span;
67 198 ▶ push @errors, $span_error if length $span_error;
Branch: 1 branch
BranchTrueFalseTotal
if length $span_error 56 142 198
68  
69 198 return @errors;
70 16 };
71  
72 1 ▶sub _string_to_code_recurrence {
Subroutine _string_to_code_recurrence: called CC=2 SCAR=9.9
73 1 my %recurrence;
74 %recurrence = (
75 0 ▶ yearly => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
76 0 ▶ return $_[0] if $_[0]->is_infinite;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->is_infinite 0 0 0
77 0 return $_[0]->truncate( to => 'year' )->add( years => 1 );
78 },
79 by_year => $recurrence{'yearly'},
80 0 ▶ monthly => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
81 0 ▶ return $_[0] if $_[0]->is_infinite;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->is_infinite 0 0 0
82 0 return $_[0]->truncate( to => 'month' )->add( months => 1 );
83 },
84 by_month => $recurrence{'monthly'},
85 705 ▶ daily => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
86 705 ▶ return $_[0] if $_[0]->is_infinite;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->is_infinite 33 672 705
87 672 return $_[0]->truncate( to => 'day' )->add( days => 1 );
88 },
89 by_day => $recurrence{'daily'},
90 0 ▶ '_DEFAULT' => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
91 1 );
92 1 ▶ return $recurrence{$_} if exists $recurrence{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $recurrence{$_} 1 0 1
93 0 return $recurrence{'_DEFAULT'};
94 }
95  
96 198 ▶sub _validate_span {
Subroutine _validate_span: called CC=6 SCAR=17.9
97 198 my ( $self ) = @_;
98  
99 198 ▶ return if ref($self->value) ne 'DateTime' || ! $self->span;
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->value ne "DateTime" or not $self->span 33 165 198
Condition66%2/3
ref $self->value ne "DateTime" or not $self->span
l!l&&r!l&&!r
033165
MC/DC50%1/2
ref $self->value ne "DateTime" or not $self->span
ref $self->value ne "DateTime" not $self->span
Truth table66%2/3
A or not(B)
A= ref $self->value ne "DateTime"B= not $self->span
ABresult
1X1
011
000
100  
101 165 my ($span, $recurrence ) = ( $self->span, $self->recurrence );
102  
103 165 ▶ if ( $recurrence ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recurrence) 132 33 165
104 132 $span = DateTime::Set->from_recurrence(
105 span => $span, recurrence => $recurrence,
106 );
107 }
108  
109 165 ▶ if ( ! $span->contains( $self->value ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($span->contains($self->value)) 56 109 165
110 56 my $format = '_FIELDNAME_ (%s) is not between %s and %s';
111 56 ▶ $format .= ' OR not a valid recurrence within that span'
Branch: 1 branch
BranchTrueFalseTotal
unless $recurrence 33 23 56
112 if ! $recurrence;
113 56 return sprintf $format, $self->value, $span->min, $span->max;
114 }
115 109 return;
116 }
117  
118 0 ▶sub set_selection {
Subroutine set_selection: not called CC=1 SCAR=6.9
Pod documented
119 0 my ( $self ) = @_;
120 0 return $self->value;
121 }
122  
123 0 ▶sub get_options {
Subroutine get_options: not called CC=5 SCAR=34.0
Pod documented
124 0 my ($self, $recurrence ) = @_;
125  
126 0 my $options = [ { name => $self->value, value => $self->value } ];
127 0 ▶ $recurrence ||= $self->recurrence;
Condition0%0/3
$recurrence ||= $self->recurrence
l!l&&r!l&&!r
000
MC/DC0%0/2
$recurrence ||= $self->recurrence
$recurrence $self->recurrence
Truth table0%0/3
A ||= B
A= $recurrenceB= $self->recurrence
ABresult
1X1
011
000
128 0 ▶ if ( defined $self->span && defined $recurrence ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->span and defined $recurrence) 0 0 0
Condition0%0/3
defined $self->span and defined $recurrence
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->span and defined $recurrence
defined $self->span defined $recurrence
Truth table0%0/3
A and B
A= defined $self->spanB= defined $recurrence
ABresult
0X0
100
111
129 0 my $dates = DateTime::Set->from_recurrence(
130 span => $self->span, recurrence => $recurrence,
131 );
132 0 my @dates = grep { defined } $dates->as_list;
133 0 ▶ $options = [ map { { name => $_, value => $_ } } @dates ] if @dates;
Branch: 1 branch
BranchTrueFalseTotal
if @dates 0 0 0
134 }
135 0 return $options;
136 }
137  
138 0 ▶sub accepts_multiple {
Subroutine accepts_multiple: not called CC=1 SCAR=6.9
Pod documented
139 0 my ($self) = @_;
140  
141 0 return 0;
142 }
143  
144 0 ▶sub get_additional_configuration {
Subroutine get_additional_configuration: not called CC=2 SCAR=17.9
Pod documented
145 0 my $self = shift;
146  
147 0 ▶ return { map { defined ($self->$_) ? ( $_ => $self->$_ ) : () }
Branch: 1 branch
BranchTrueFalseTotal
defined $self->$_ ? : 0 0 0
148 qw/span recurrence/ };
149 }
150  
151 16 __PACKAGE__->meta->make_immutable;
152 16 1;
153  
154 __END__
« lib/Form/Sensible/Validator.pm ↑ Summary lib/Form/Sensible/Form.pm »