Muster/Hook/DeriveFields.pm

filter: Statementstmt 26.0% 26 / 100 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 72.7% 8 / 11 Podpod 100% 2 / 2 totaltotal 18.7% 36 / 192 CC 28 SCAR 63.9
CC
28
Cov
16%
process
812.0
register
2.0
__ANON__
2.0
CRAP 598.6
« Muster/Hook/Map.pm ↑ Summary Muster/Hook/Counter.pm »
1 package Muster::Hook::DeriveFields;
2 1 $Muster::Hook::DeriveFields::VERSION = '0.96';
3 =head1 NAME
4  
5 Muster::Hook::DeriveFields - Muster hook for field derivation
6  
7 =head1 VERSION
8  
9 version 0.96
10  
11 =head1 DESCRIPTION
12  
13 L<Muster::Hook::DeriveFields> does field derivation;
14 that is, derives field values from other fields
15 (aka the meta-data for the Leaf).
16  
17 This just does a bunch of specific calculations;
18 I haven't figured out a good way of defining derivations in a config file.
19  
20 =cut
21  
22 1 ▶use Mojo::Base 'Muster::Hook';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Muster::Hooks;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Muster::LeafFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Lingua::EN::Inflexion;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use YAML::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 =head1 METHODS
32  
33 =head2 register
34  
35 Initialize, and register hooks.
36  
37 =cut
38 0 ▶sub register {
Subroutine register: not called CC=1 SCAR=6.9
Pod documented
39 0 my $self = shift;
40 0 my $hookmaster = shift;
41 0 my $conf = shift;
42  
43 0 $self->{config} = $conf->{hook_conf}->{'Muster::Hook::DeriveFields'};
44  
45 0 ▶ $hookmaster->add_hook('derivefields' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
46 0 my %args = @_;
47  
48 0 return $self->process(%args);
49 },
50 0 );
51 0 return $self;
52 } # register
53  
54 =head2 process
55  
56 Process (scan or modify) a leaf object.
57 This only does stuff in the scan phase.
58 This expects the leaf meta-data to be populated.
59  
60 my $new_leaf = $self->process(%args);
61  
62 =cut
63 0 ▶sub process {
Subroutine process: not called CC=28 SCAR=67.0
Pod documented
64 0 my $self = shift;
65 0 my %args = @_;
66  
67 0 my $leaf = $args{leaf};
68 0 my $phase = $args{phase};
69  
70 # only does derivations in scan phase
71 0 ▶ if ($phase ne $Muster::Hooks::PHASE_SCAN)
Branch: 1 branch
BranchTrueFalseTotal
if ($phase ne $Muster::Hooks::PHASE_SCAN) 0 0 0
72 {
73 0 return $leaf;
74 }
75  
76 0 my $meta = $leaf->meta;
77  
78 # -----------------------------------------
79 # Do derivations
80 # -----------------------------------------
81  
82 # split the page-name on '-'
83 # useful for project-types
84 0 my @bits = split('-', $leaf->bald_name);
85 0 ▶ for (my $i=0; $i < scalar @bits; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @bits) 0 0 0
86 {
87 0 my $p1 = sprintf('p%d', $i + 1); # page-bits start from 1 not 0
88 0 $meta->{$p1} = $bits[$i];
89 }
90  
91 # sections being the parts of the full page name
92 0 @bits = split(/\//, $leaf->pagename);
93 # remove the actual page-file from this list
94 0 pop @bits;
95 0 ▶ for (my $i=0; $i < scalar @bits; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @bits) 0 0 0
96 {
97 0 my $section = sprintf('section%d', $i + 1); # sections start from 1 not 0
98 0 $meta->{$section} = $bits[$i];
99 }
100  
101 # the first Alpha of the name; good for headers in reports
102 0 $meta->{name_a} = uc(substr($leaf->bald_name, 0, 1));
103  
104 # name-spaced
105 0 my $namespaced = $leaf->bald_name;
106 0 $namespaced =~ s#_# #g;
107 0 $namespaced =~ s#-# #g;
108 0 $namespaced =~ s/([-\w]+)/\u\L$1/g;
109 0 $meta->{namespaced} = $namespaced;
110  
111 # plural and singular
112 # assuming that the page-name is a noun...
113 0 my $noun = noun($leaf->bald_name);
114 0 ▶ if ($noun->is_plural())
Branch: 2 branches
BranchTrueFalseTotal
if ($noun->is_plural) { } 0 0 0
elsif ($noun->is_singular) { } 0 0 0
115 {
116 0 $meta->{singular} = $noun->singular();
117 0 $meta->{plural} = $leaf->bald_name;
118 }
119 elsif ($noun->is_singular())
120 {
121 0 $meta->{singular} = $leaf->bald_name;
122 0 $meta->{plural} = $noun->plural();
123 }
124 else # neither
125 {
126 0 $meta->{singular} = $leaf->bald_name;
127 0 $meta->{plural} = $leaf->bald_name;
128 }
129  
130 # ============================================
131 # DATE stuff
132 # ============================================
133  
134 # Some date adjustments.
135 # Files may have creation-date information in them;
136 # use that for the "date" of the page
137 0 ▶ if (exists $meta->{timestamp}
Branch: 4 branches
BranchTrueFalseTotal
if (exists $meta->{'timestamp'} and defined $meta->{'timestamp'} and $meta->{'timestamp'} != $meta->{'mtime'}) { } 0 0 0
elsif (exists $meta->{'creation_date'} and defined $meta->{'creation_date'} and $meta->{'creation_date'} =~ /^\d\d\d\d-\d\d-\d\d/u) { } 0 0 0
elsif (exists $meta->{'date_added'} and defined $meta->{'date_added'} and $meta->{'date_added'} =~ /^\d\d\d\d-\d\d-\d\d/u) { } 0 0 0
elsif (exists $meta->{'fetch_date'} and defined $meta->{'fetch_date'} and $meta->{'fetch_date'} =~ /^\d\d\d\d-\d\d-\d\d/u) { } 0 0 0
Condition0%0/6
exists $meta->{"timestamp"} and defined $meta->{"timestamp"} and $meta->{"timestamp"} != $meta->{"mtime"}
!ll&&!rl&&r
000
exists $meta->{"timestamp"} and defined $meta->{"timestamp"}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $meta->{"timestamp"} and defined $meta->{"timestamp"} and $meta->{"timestamp"} != $meta->{"mtime"}
exists $meta->{"timestamp"} defined $meta->{"timestamp"} $meta->{"timestamp"} != $meta->{"mtime"}
Truth table0%0/4
A and B and C
A= exists $meta->{"timestamp"}B= defined $meta->{"timestamp"}C= $meta->{"timestamp"} != $meta->{"mtime"}
ABCresult
0XX0
10X0
1100
1111
138 and defined $meta->{timestamp}
139 and $meta->{timestamp} != $meta->{mtime})
140 {
141 0 $meta->{date} = strftime('%Y-%m-%d %H:%M', localtime($meta->{timestamp}));
142 }
143 ▶ elsif (exists $meta->{creation_date}
Condition0%0/6
exists $meta->{"creation_date"} and defined $meta->{"creation_date"} and $meta->{"creation_date"} =~ /^\d\d\d\d-\d\d-\d\d/u
!ll&&!rl&&r
000
exists $meta->{"creation_date"} and defined $meta->{"creation_date"}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $meta->{"creation_date"} and defined $meta->{"creation_date"} and $meta->{"creation_date"} =~ /^\d\d\d\d-\d\d-\d\d/u
exists $meta->{"creation_date"} defined $meta->{"creation_date"} $meta->{"creation_date"} =~ /^\d\d\d\d-\d\d-\d\d/u
Truth table0%0/4
A and B and C
A= exists $meta->{"creation_date"}B= defined $meta->{"creation_date"}C= $meta->{"creation_date"} =~ /^\d\d\d\d-\d\d-\d\d/u
ABCresult
0XX0
10X0
1100
1111
144 and defined $meta->{creation_date}
145 and $meta->{creation_date} =~ /^\d\d\d\d-\d\d-\d\d/)
146 {
147 0 $meta->{date} = $meta->{creation_date};
148 }
149 ▶ elsif (exists $meta->{date_added}
Condition0%0/6
exists $meta->{"date_added"} and defined $meta->{"date_added"} and $meta->{"date_added"} =~ /^\d\d\d\d-\d\d-\d\d/u
!ll&&!rl&&r
000
exists $meta->{"date_added"} and defined $meta->{"date_added"}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $meta->{"date_added"} and defined $meta->{"date_added"} and $meta->{"date_added"} =~ /^\d\d\d\d-\d\d-\d\d/u
exists $meta->{"date_added"} defined $meta->{"date_added"} $meta->{"date_added"} =~ /^\d\d\d\d-\d\d-\d\d/u
Truth table0%0/4
A and B and C
A= exists $meta->{"date_added"}B= defined $meta->{"date_added"}C= $meta->{"date_added"} =~ /^\d\d\d\d-\d\d-\d\d/u
ABCresult
0XX0
10X0
1100
1111
150 and defined $meta->{date_added}
151 and $meta->{date_added} =~ /^\d\d\d\d-\d\d-\d\d/)
152 {
153 0 $meta->{date} = $meta->{creation_date};
154 }
155 ▶ elsif (exists $meta->{fetch_date}
Condition0%0/6
exists $meta->{"fetch_date"} and defined $meta->{"fetch_date"} and $meta->{"fetch_date"} =~ /^\d\d\d\d-\d\d-\d\d/u
!ll&&!rl&&r
000
exists $meta->{"fetch_date"} and defined $meta->{"fetch_date"}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $meta->{"fetch_date"} and defined $meta->{"fetch_date"} and $meta->{"fetch_date"} =~ /^\d\d\d\d-\d\d-\d\d/u
exists $meta->{"fetch_date"} defined $meta->{"fetch_date"} $meta->{"fetch_date"} =~ /^\d\d\d\d-\d\d-\d\d/u
Truth table0%0/4
A and B and C
A= exists $meta->{"fetch_date"}B= defined $meta->{"fetch_date"}C= $meta->{"fetch_date"} =~ /^\d\d\d\d-\d\d-\d\d/u
ABCresult
0XX0
10X0
1100
1111
156 and defined $meta->{fetch_date}
157 and $meta->{fetch_date} =~ /^\d\d\d\d-\d\d-\d\d/)
158 {
159 0 $meta->{date} = $meta->{fetch_date};
160 }
161  
162 # Derived date-related info using DateTime
163 # Look for existing fields which end with _date
164 0 foreach my $field (keys %{$meta})
165 {
166 0 ▶ if (($field =~ /_date$/
Branch: 1 branch
BranchTrueFalseTotal
if ($field =~ /_date$/u || $field =~ /^date_/u || $field eq "date" and defined $meta->{$field} and $meta->{$field} =~ /^(\d\d\d\d)-(\d\d)-(\d\d)/u) 0 0 0
Condition0%0/12
$field =~ /_date$/u || $field =~ /^date_/u || $field eq "date" and defined $meta->{$field} and $meta->{$field} =~ /^(\d\d\d\d)-(\d\d)-(\d\d)/u
!ll&&!rl&&r
000
$field =~ /_date$/u || $field =~ /^date_/u || $field eq "date" and defined $meta->{$field}
!ll&&!rl&&r
000
$field =~ /_date$/u or $field =~ /^date_/u or $field eq "date"
l!l&&r!l&&!r
000
$field =~ /_date$/u or $field =~ /^date_/u
l!l&&r!l&&!r
000
MC/DC0%0/5
$field =~ /_date$/u || $field =~ /^date_/u || $field eq "date" and defined $meta->{$field} and $meta->{$field} =~ /^(\d\d\d\d)-(\d\d)-(\d\d)/u
$field =~ /_date$/u $field =~ /^date_/u $field eq "date" defined $meta->{$field} $meta->{$field} =~ /^(\d\d\d\d)-(\d\d)-(\d\d)/u
Truth table0%0/10
A or B or C and D and E
A= $field =~ /_date$/uB= $field =~ /^date_/uC= $field eq "date"D= defined $meta->{$field}E= $meta->{$field} =~ /^(\d\d\d\d)-(\d\d)-(\d\d)/u
ABCDEresult
000XX0
1XX0X0
01X0X0
0010X0
1XX100
01X100
001100
1XX111
01X111
001111
167 or $field =~ /^date_/
168 or $field eq 'date')
169 and defined $meta->{$field}
170 and $meta->{$field} =~ /^(\d\d\d\d)-(\d\d)-(\d\d)/)
171 {
172 0 my $year = $1;
173 0 my $month = $2;
174 0 my $day = $3;
175 0 my $hour = 0;
176 0 my $min = 0;
177 # The date MAY have time info in it too
178 0 ▶ if ($meta->{$field} =~ /^\d\d\d\d-\d\d-\d\d (\d+):(\d\d)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->{$field} =~ /^\d\d\d\d-\d\d-\d\d (\d+):(\d\d)/u) 0 0 0
179 {
180 0 $hour = $1;
181 0 $min = $2;
182 }
183 0 my $dt = DateTime->new(year=>$year,month=>$month,day=>$day,
184 hour=>$hour,minute=>$min);
185 0 my $new_fn = $field;
186 0 $new_fn =~ s/date/datetime/;
187 0 $meta->{$new_fn} = $dt->epoch();
188 0 $new_fn = $field; $new_fn =~ s/date/date_year/;
189 0 $meta->{$new_fn} = $dt->year();
190 0 $new_fn = $field; $new_fn =~ s/date/date_month/;
191 0 $meta->{$new_fn} = $dt->month();
192 0 $new_fn = $field; $new_fn =~ s/date/date_monthname/;
193 0 $meta->{$new_fn} = $dt->month_name();
194 }
195 }
196  
197 # -----------------------------------------
198 # Default field-values
199 # set on a per-extension basis.
200 # These are set in the config for this hook.
201 # They will not clobber existing values.
202 # -----------------------------------------
203 0 ▶ if (defined $self->{config}->{ext}->{$meta->{extension}})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"config"}{"ext"}{$meta->{"extension"}}) 0 0 0
204 {
205 0 foreach my $field (keys %{$self->{config}->{ext}->{$meta->{extension}}})
206 {
207 0 ▶ if (!defined $meta->{$field})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $meta->{$field}) 0 0 0
208 {
209 0 $meta->{$field} = $self->{config}->{ext}->{$meta->{extension}}->{$field};
210 }
211 }
212 }
213  
214 0 $leaf->{meta} = $meta;
215  
216 0 return $leaf;
217 } # process
218  
219  
220 1 1;
« Muster/Hook/Map.pm ↑ Summary Muster/Hook/Counter.pm »