Muster/Hook/DynamicField.pm

filter: Statementstmt 24.3% 20 / 82 Branchbran 0.0% 0 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 54.5% 6 / 11 Podpod 100% 4 / 4 totaltotal 24.0% 30 / 125 CC 15 SCAR 36.8
CC
15
Cov
18%
get_dynamic_value
72.0
get_function_result
56.0
process
6.0
CRAP 39.6
« Muster/Hook/GraphViz.pm ↑ Summary Muster/Hook/Template.pm »
1 package Muster::Hook::DynamicField;
2 1 $Muster::Hook::DynamicField::VERSION = '0.96';
3 =head1 NAME
4  
5 Muster::Hook::DynamicField - Muster hook for dynamic fields
6  
7 =head1 VERSION
8  
9 version 0.96
10  
11 =head1 SYNOPSIS
12  
13 # CamelCase plugin name
14 package Muster::Hook::DynamicField;
15 use Mojo::Base 'Muster::Hook';
16  
17 =head1 DESCRIPTION
18  
19 L<Muster::Hook::DynamicField> does dynamic fields:
20 field-substitution for derived values which are not always constant, such as the current date.
21  
22 The pattern for dynamic fields is "{{!I<fieldname>}}".
23  
24 =cut
25  
26 1 ▶use Mojo::Base 'Muster::Hook';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use Muster::Hooks;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use Muster::LeafFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use YAML::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use Math::Calc::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 =head1 METHODS
34  
35 =head2 register
36  
37 Initialize, and register hooks.
38  
39 =cut
40 0 ▶sub register {
Subroutine register: not called CC=1 SCAR=6.9
Pod documented
41 0 my $self = shift;
42 0 my $hookmaster = shift;
43 0 my $conf = shift;
44  
45 # we need to be able to look things up in the database
46 0 $self->{metadb} = $hookmaster->{metadb};
47  
48 0 ▶ $hookmaster->add_hook('dynamicfield' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 0 my %args = @_;
50  
51 0 return $self->process(%args);
52 },
53 0 );
54 0 return $self;
55 } # register
56  
57 =head2 process
58  
59 Process (modify) a leaf object.
60 In scanning phase, this will do nothing, because it's pointless.
61 In assembly phase, it will do simple substitutions of calculated data (which may or may not be derived from the leaf data).
62  
63 my $new_leaf = $self->process(%args);
64  
65 =cut
66 0 ▶sub process {
Subroutine process: not called CC=2 SCAR=17.9
Pod documented
67 0 my $self = shift;
68 0 my %args = @_;
69  
70 0 my $leaf = $args{leaf};
71 0 my $phase = $args{phase};
72  
73 0 ▶ if ($phase ne $Muster::Hooks::PHASE_BUILD)
Branch: 1 branch
BranchTrueFalseTotal
if ($phase ne $Muster::Hooks::PHASE_BUILD) 0 0 0
74 {
75 0 return $leaf;
76 }
77  
78 0 my $content = $leaf->cooked();
79  
80 # substitute {{!var}} variables
81 0 $content =~ s/(\\?)\{\{\!([-\w]+)\}\}/$self->get_dynamic_value($1,$2,$leaf)/eg;
82
83 # substitute {{!fn(...)}} functions
84 0 $content =~ s/(\\?)\{\{\!([-\w]+)\(([^)]+)\)\}\}/$self->get_function_result($1,$2,$3,$leaf)/eg;
85 # substitute {{!fn[...]}} functions for functions that need parens as args
86 0 $content =~ s/(\\?)\{\{\!([-\w]+)\[([^\]]+)\]\}\}/$self->get_function_result($1,$2,$3,$leaf)/eg;
87  
88 0 $leaf->{cooked} = $content;
89 0 return $leaf;
90 } # process
91  
92 =head2 get_dynamic_value
93  
94 Get the dynamic value for this page.
95  
96 =cut
97 0 ▶sub get_dynamic_value {
Subroutine get_dynamic_value: not called CC=8 SCAR=42.8
Pod documented
98 0 my $self = shift;
99 0 my $escape = shift;
100 0 my $field = shift;
101 0 my $leaf = shift;
102  
103 0 ▶ if (length $escape)
Branch: 1 branch
BranchTrueFalseTotal
if (length $escape) 0 0 0
104 {
105 0 return "{{\!${field}}}";
106 }
107  
108 # force all fields to lower-case
109 0 $field = lc($field);
110  
111 0 my $value;
112  
113 0 ▶ if ($field eq 'now')
Branch: 3 branches
BranchTrueFalseTotal
if ($field eq 'now') { } 0 0 0
elsif ($field eq 'today') { } 0 0 0
elsif ($field eq 'thisyear') { } 0 0 0
114 {
115 0 $value = strftime '%H:%M:%S', localtime;
116 }
117 elsif ($field eq 'today')
118 {
119 0 $value = strftime '%Y-%m-%d', localtime;
120 }
121 elsif ($field eq 'thisyear')
122 {
123 0 $value = strftime '%Y', localtime;
124 }
125  
126  
127 0 ▶ if (!defined $value)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
128 {
129 0 return '';
130 }
131 0 ▶ if (ref $value eq 'ARRAY')
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 0 0
elsif (ref $value eq 'HASH') { } 0 0 0
132 {
133 0 $value = join(' ', @{$value});
134 }
135 elsif (ref $value eq 'HASH')
136 {
137 0 $value = Dump($value);
138 }
139 0 return $value;
140 } # get_dynamic_value
141  
142 =head2 get_function_result
143  
144 Process the given function for this page.
145  
146 =cut
147 0 ▶sub get_function_result {
Subroutine get_function_result: not called CC=7 SCAR=40.3
Pod documented
148 0 my $self = shift;
149 0 my $escape = shift;
150 0 my $func = shift;
151 0 my $argvals = shift;
152 0 my $leaf = shift;
153  
154 0 ▶ if (length $escape)
Branch: 1 branch
BranchTrueFalseTotal
if (length $escape) 0 0 0
155 {
156 0 return "{{\!${func}(${argvals})}}";
157 }
158  
159 0 my $value;
160  
161 0 ▶ if ($func eq 'math')
Branch: 2 branches
BranchTrueFalseTotal
if ($func eq 'math') { } 0 0 0
elsif ($func eq 'matheq') { } 0 0 0
162 {
163 0 my $result = Math::Calc::Parser->evaluate($argvals);
164 # round the result for niceness
165 0 $value = sprintf("%.2f",$result);
166 }
167 elsif ($func eq 'matheq')
168 {
169 0 my $result = Math::Calc::Parser->evaluate($argvals);
170 0 $value = "${argvals} = ${result}";
171 }
172  
173 0 ▶ if (!defined $value)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
174 {
175 0 return '';
176 }
177 0 ▶ if (ref $value eq 'ARRAY')
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 0 0
elsif (ref $value eq 'HASH') { } 0 0 0
178 {
179 0 $value = join(' ', @{$value});
180 }
181 elsif (ref $value eq 'HASH')
182 {
183 0 $value = Dump($value);
184 }
185 0 return $value;
186 } # get_function_result
187  
188  
189 1 1;
« Muster/Hook/GraphViz.pm ↑ Summary Muster/Hook/Template.pm »