blib/lib/HTML/FormHandlerX/Field/DateTimeNatural.pm

filter: Statementstmt 100% 49 / 49 Branchbran 100% 8 / 8 Conditioncond 83.3% 5 / 6 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 8 / 8 Podpod 50.0% 1 / 2 totaltotal 96.0% 73 / 76 CC 8 SCAR 13.7
CC
8
Cov
98%
_build_datetime_format_natural
7.0
validate
2.0
BEGIN
1.0
CRAP 3.9
↑ Summary
1 package HTML::FormHandlerX::Field::DateTimeNatural;
2  
3 # ABSTRACT: a datetime field with natural language parsing.
4  
5 1 ▶use version; our $VERSION = version->declare('v0.6');
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use HTML::FormHandler::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 extends 'HTML::FormHandler::Field::Text';
9  
10 1 ▶use MooseX::Types::DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use DateTime::Format::Natural;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 has 'datetime_format_natural' => (
14 is => 'ro',
15 isa => 'DateTime::Format::Natural',
16 lazy_build => 1,
17 required => 1,
18 );
19  
20 1 has 'datetime' => (
21 is => 'rw',
22 isa => 'DateTime',
23 );
24  
25 1 has 'lang' => (
26 is => 'rw',
27 isa => 'Str',
28 );
29  
30 1 has 'format' => (
31 is => 'rw',
32 isa => 'Str',
33 );
34  
35 1 has 'prefer_future' => (
36 is => 'rw',
37 isa => 'Bool',
38 );
39  
40 1 has 'time_zone' => (
41 is => 'rw',
42 isa => 'DateTime::TimeZone',
43 coerce => 1,
44 );
45  
46 1 has 'daytime' => (
47 is => 'rw',
48 isa => 'HashRef',
49 );
50  
51 1 our $class_messages = { 'date_invalid' => 'Date is invalid.', };
52  
53 2 ▶sub get_class_messages {
Subroutine get_class_messages: called CC=1 SCAR=0.0
Pod undocumented
54 2 my $self = shift;
55 2 return { %{ $self->next::method }, %{$class_messages}, };
56 }
57  
58 7 ▶sub validate {
Subroutine validate: called CC=2 SCAR=6.9
Pod documented
59 7 my $self = shift;
60 7 my $value = $self->value;
61  
62 ## validate
63 7 my $parser = $self->datetime_format_natural;
64 7 my $dt = $parser->parse_datetime($value);
65  
66 ## update to inflated value or set error
67 7 ▶ if ($parser->success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parser->success) { } 5 2 7
68 5 $self->_set_value($dt);
69 } else {
70 2 $self->add_error($self->get_message('date_invalid'));
71 }
72  
73 ## return
74 7 return $parser->success;
75 }
76  
77 3 ▶sub _build_datetime_format_natural {
Subroutine _build_datetime_format_natural: called CC=7 SCAR=19.5
78 3 my $self = shift;
79  
80 3 my %attributes;
81 3 my $form = $self->form;
82 3 foreach
83 my $attr (qw/datetime time_zone lang format prefer_future daytime/)
84 {
85 18 ▶ if (defined $self->$attr) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->$attr) { } 1 17 18
elsif ($form and $form->meta->find_attribute_by_name($attr) and defined $form->$attr) { } 1 32 33
86 1 $attributes{$attr} = $self->$attr;
87 ▶ } elsif ($form
Condition83%5/6
$form and $form->meta->find_attribute_by_name($attr) and defined $form->$attr
!ll&&!rl&&r
1601
$form and $form->meta->find_attribute_by_name($attr)
!ll&&!rl&&r
1151
MC/DC66%2/3
$form and $form->meta->find_attribute_by_name($attr) and defined $form->$attr
$form $form->meta->find_attribute_by_name($attr) defined $form->$attr
Truth table75%3/4
A and B and C
A= $formB= $form->meta->find_attribute_by_name($attr)C= defined $form->$attr
ABCresult
0XX0
10X0
1100
1111
88 && $form->meta->find_attribute_by_name($attr)
89 && defined $form->$attr)
90 {
91 1 $attributes{$attr} = $form->$attr;
92 }
93 }
94  
95 ## Fix time_zone if set, because DT::F::N can only accept time zone
96 ## names and not objects, at the time of writing this module.
97 3 ▶ if ($attributes{time_zone}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes{"time_zone"}) 2 1 3
98 2 $attributes{time_zone} = $attributes{time_zone}->name;
99 }
100  
101 3 return DateTime::Format::Natural->new(%attributes);
102 }
103  
104 1 __PACKAGE__->meta->make_immutable;
105 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
106 1 1;
107  
108 __END__
↑ Summary