Natural/Helpers.pm

filter: Statementstmt 100% 68 / 68 Branchbran 100% 12 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 14 / 14 Podpod n/a totaltotal 100% 94 / 94 CC 8 SCAR 6.7
CC
8
Cov
100%
_helper
3.0
_weekday_name
3.0
_time_meridiem
3.0
CRAP 2.0
« Natural/Compat.pm ↑ Summary Natural/Calendar.pm »
1 package DateTime::Format::Natural::Helpers;
2  
3 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 27 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 27 ▶use boolean qw(true false);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 27 ▶use constant REAL_FLAG => true;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 27 ▶use constant VIRT_FLAG => false;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 27 our ($VERSION, @EXPORT_OK, %flag);
12  
13 27 $VERSION = '0.08';
14 27 @EXPORT_OK = qw(%flag);
15  
16 27 my @flags = (
17 { weekday_name => REAL_FLAG },
18 { weekday_num => REAL_FLAG },
19 { month_name => REAL_FLAG },
20 { month_num => REAL_FLAG },
21 { time_am => REAL_FLAG },
22 { time_pm => REAL_FLAG },
23 { last_this_next => VIRT_FLAG },
24 { yes_today_tom => VIRT_FLAG },
25 { noon_midnight => VIRT_FLAG },
26 { morn_aftern_even => VIRT_FLAG },
27 { before_after_from => VIRT_FLAG },
28 { eve_day => VIRT_FLAG },
29 );
30  
31 {
32 27 my $i;
33 27 %flag = map { (keys %$_)[0] => $i++ } @flags;
34 }
35  
36 sub _helper
37 18225 ▶{
Subroutine _helper: called CC=3 SCAR=11.0
38 18225 my $self = shift;
39 18225 my ($flags, $string) = @_;
40  
41 18225 foreach my $flag (@$flags) {
42 24948 my $name = (keys %{$flags[$flag]})[0];
43 24948 ▶ if ($flags[$flag]->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags[$flag]{$name}) { } 19933 5015 24948
44 19933 my $helper = "_$name";
45 19933 $self->$helper(\$string);
46 }
47 else {
48 5015 $string = $self->{data}->{conversion}->{$name}->{lc $string};
49 }
50 }
51  
52 18225 return $string;
53 }
54  
55 sub _weekday_name
56 3645 ▶{
Subroutine _weekday_name: called CC=3 SCAR=11.0
57 3645 my $self = shift;
58 3645 my ($arg) = @_;
59  
60 3645 my $helper = $self->{data}->{helpers};
61  
62 3645 ▶ if ($$arg =~ $helper->{suffix}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$arg =~ /$helper->{"suffix"}/) 42 3603 3645
63 42 $$arg =~ s/$helper->{suffix}//;
64 }
65 3645 $helper->{normalize}->($arg);
66 3645 ▶ if ($helper->{abbreviated}->($arg)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($helper->{"abbreviated"}->($arg)) 660 2985 3645
67 660 $$arg = $self->{data}->{weekdays_abbrev}->{$$arg};
68 }
69 }
70  
71 sub _weekday_num
72 3645 ▶{
Subroutine _weekday_num: called CC=1 SCAR=0.0
73 3645 my $self = shift;
74 3645 my ($arg) = @_;
75  
76 3645 $$arg = $self->_Decode_Day_of_Week($$arg);
77 }
78  
79 sub _month_name
80 3078 ▶{
Subroutine _month_name: called CC=2 SCAR=6.9
81 3078 my $self = shift;
82 3078 my ($arg) = @_;
83  
84 3078 my $helper = $self->{data}->{helpers};
85  
86 3078 $helper->{normalize}->($arg);
87 3078 ▶ if ($helper->{abbreviated}->($arg)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($helper->{"abbreviated"}->($arg)) 2551 527 3078
88 2551 $$arg = $self->{data}->{months_abbrev}->{$$arg};
89 }
90 }
91  
92 sub _month_num
93 3078 ▶{
Subroutine _month_num: called CC=1 SCAR=0.0
94 3078 my $self = shift;
95 3078 my ($arg) = @_;
96  
97 3078 $$arg = $self->_Decode_Month($$arg);
98 }
99  
100 sub _time_am
101 3671 ▶{
Subroutine _time_am: called CC=1 SCAR=0.0
102 3671 my $self = shift;
103 3671 my ($arg) = @_;
104  
105 3671 $self->_time_meridiem($arg, 'am');
106 }
107  
108 sub _time_pm
109 2816 ▶{
Subroutine _time_pm: called CC=1 SCAR=0.0
110 2816 my $self = shift;
111 2816 my ($arg) = @_;
112  
113 2816 $self->_time_meridiem($arg, 'pm');
114 }
115  
116 sub _time_meridiem
117 6487 ▶{
Subroutine _time_meridiem: called CC=3 SCAR=11.0
118 6487 my $self = shift;
119 6487 my ($time, $period) = @_;
120  
121 6487 my ($hour) = split /[:\.]/, $$time;
122  
123 6487 ▶ my %hours = (
Branch: 2 branches
BranchTrueFalseTotal
$hour == 12 ? : 462 6025 6487
$hour == 12 ? : 462 6025 6487
124 am => $hour - (($hour == 12) ? 12 : 0),
125 pm => $hour + (($hour == 12) ? 0 : 12),
126 );
127  
128 6487 $$time =~ s/^ \d+? (?:(?=[:\.])|$)/$hours{$period}/x;
129 }
130  
131 27 1;
132 __END__
« Natural/Compat.pm ↑ Summary Natural/Calendar.pm »