Su.pm

filter: Statementstmt 48.6% 364 / 748 Branchbran 33.6% 159 / 472 Conditioncond 31.7% 52 / 164 MC/DCmcdc 17.7% 16 / 90 Subroutinesub 59.0% 36 / 61 Podpod 92.3% 24 / 26 totaltotal 41.7% 651 / 1561 CC 305 SCAR 49.2
CC
305
Cov
42%
resolve
406.0
resolve
406.0
_gen_defs_internal
115.9
CRAP 136.6
↑ Summary Su/Build/CustomBuild.pm »
1 package Su;
2  
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 13 ▶use Su::Process;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use Su::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use Su::Model;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use Su::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 13 ▶use Fatal qw(mkpath open);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 13 our $VERSION = '0.110';
19  
20 13 our @ISA = qw(Exporter);
21  
22 13 our @EXPORT = qw(resolve setup gen_defs gen_model gen_proc);
23  
24 13 our $info_href = {};
25  
26 13 our $BASE_DIR = './';
27  
28 13 our $DEFS_DIR = 'Defs';
29  
30 13 our $DEFS_MODULE_NAME = "Defs";
31  
32 13 our $DEFAULT_MODEL_NAME = 'Model';
33  
34 13 our $DEFAULT_PROC_NAME = 'MainProc';
35  
36 # A relative path to place the .tmpl file.
37 13 our $SU_TMPL_DIR = '/Su/templates/';
38  
39 # The field name to define the global fields in Defs file.
40 13 our $GLOBAL_MODEL_FIELD = 'global_model_field';
41  
42 =head1 NAME
43  
44 Su - A simple application layer to divide and integrate data and processes in the Perl program.
45  
46 =head1 SYNOPSIS
47  
48 my $su = Su->new;
49 my $proc_result = $su->resolve('process_id');
50 print $proc_result;
51  
52 =head1 DESCRIPTION
53  
54 Su is a simple application framework that works as a thin layer to
55 divide data and process in your Perl program. This framework aims an
56 ease of maintenance and extension of your application.
57  
58 Su is a thin application layer, so you can use Su with many other
59 frameworks you prefer in many cases.
60  
61 Note that Su framework has nothing to do with unix C<su> (switch
62 user) command.
63  
64 =head3 Prepare Data and Process file
65  
66 Su provides the method to generate the template of Model and Process.
67 You can use method C<generate()> like the following:
68  
69 perl -MSu -e 'Su::generate("Pkg::SomeProc")'
70  
71 Then, the file F<Pkg/SomeProc.pm> and F<Pkg/SomeModel.pm> should be
72 generated.
73  
74 Now describe your data to the C<$model> field of the generated Model
75 file.
76  
77 my $model=
78 {
79 field_a =>'value_a'
80 };
81  
82 And describe your process code to the C<process> method defined in the
83 generated Process file like the following:
84  
85 sub process{
86 my $self = shift if ref $_[0] eq __PACKAGE__;
87 my $param = shift;
88  
89 my $ret = "param:" . $param . " and model:" . $model->{field_a};
90 return $ret;
91 }
92  
93 =head3 Integrate Model and Process
94  
95 Su integrates Model and Processes using the definition file.
96 To generate the definition file, type the following command.
97  
98 perl -MSu -e 'Su::gen_defs()'
99  
100 Then describe your setting to the C<$defs> field defined in the
101 generated F<Defs.pm> file.
102  
103 my $defs =
104 {
105 some_entry_id =>
106 {
107 proc=>'Pkg::SomeProc',
108 model=>'Pkg::SomeModel',
109 },
110 };
111  
112 You can also generate F<Defs.pm> using the C<generate> method by
113 passing the parameter C<1> as a second parameter.
114  
115 perl -MSu=base,lib -e 'Su::generate("Pkg::SomeProc", 1)'
116  
117 Then the file F<Defs.pm> will be generated with Model and Process file.
118  
119 =head3 Run the process using Su
120  
121 You can call the process via Su by passing the entry id which defined in
122 the definition file F<Defs.pm>.
123  
124 my $su = Su->new;
125 my $result = $su->resolve('some_entry_id');
126  
127 To pass the parameters to the C<process> method in Process, then pass
128 the additional parameter to the C<resolve> method.
129  
130 my $result = $su->resolve('some_entry_id', 'param1');
131  
132 =head3 Other features Su provides
133  
134 Logging and string template are the feature su provides for
135 convinience. These features are frequently used in many kinds of
136 applications and you can use these features without any other
137 dependencies. Surely you can use other modules you prefer with Su
138 framework.
139  
140 =head2 Additional usage - Filters
141  
142 The map, reduce and scalar filters can be defined in the definition file.
143  
144 These filters are Perl module which has the method for filtering the
145 result of the process. (In case of C<map> filter, method name is
146 C<map_filter>.) You can chain filter modules. The following code is a
147 sample definition which uses these filters.
148  
149 my $defs =
150 {
151 some_proc_id =>
152 {
153 proc=>'MainProc',
154 model=>'Pkg::MainModel',
155 map_filter=>'Pkg::FilterProc', # or ['Filter01','Filter02']
156 reduce_filter=>'Pkg::ReduceProc', # reduce filter can only apply at once.
157 scalar_filter=>'Pkg::ScalarProc', # or ['Filter01','Filter02']
158 }
159 };
160  
161 The filters Su recognizes are the followings.
162  
163 =over
164  
165 =item map_filter
166  
167 The perl module which has C<map_filter> method.
168 The parameter of this method is an array which is a result of the
169 'process' method of the Process or the chained map filter.
170 The C<map_filter> method must return the array data type.
171  
172 =item reduce_filter
173  
174 The perl module which has C<reduce_filter> method.
175 The parameter of this method is an array which is a result of the
176 'process' method of the Process.
177 If the map filters are defined in the C<Defs.pm>, then the map_filters
178 are applied to the result of the process before passed to the reduce
179 filter.
180 The C<reduce_filter> method must return the scalar data type.
181 Note that this method can't chain.
182  
183 =item scalar_filter
184  
185 The perl module which has C<scalar_filter> method.
186 The parameter of this method is a scalar which is a result of the
187 'process' method of the Process.
188 If the C<map_filters> and C<recude_filters> are defined in the
189 C<Defs.pm>, then these filters are applied to the result of the
190 process before passed to the scalar filter.
191  
192 The C<scalar_filter> method must return the scalar data type.
193  
194 =back
195  
196 =head1 ATTRIBUTES
197  
198 =head2 C<$MODEL_LOCALE>
199  
200 Set the locale string like 'ja_JP' to load locale specific Model
201 module. Locale specific Model has the postfix in it's name like
202 'Pkg::ModelName__ja_JP'.
203  
204 Then you should set the locale like this.
205  
206 $Su::MODEL_LOCALE = 'ja_JP';
207  
208 =cut
209  
210 13 our $MODEL_LOCALE = '';
211  
212 =head2 C<$MODEL_KEY_PREFIX>
213  
214 The hash reference which contains the key prefixes of the Model.
215 The key of this hash is a name of the model to apply this prefix.
216  
217 $MODEL_KEY_PREFIX = {
218 'pkg::SomeModel'=>'pre1',
219 };
220  
221 In this example, the key string 'pre1_key1' defined in
222 'pkg::SomeModel' is automatically converted to 'key1'. So you can
223 access customized value using key 'key1'.
224  
225 If the modified key is not exist, then the value of original key
226 should used.
227  
228 =cut
229  
230 13 our $MODEL_KEY_PREFIX = {};
231  
232 =head2 C<$MODEL_KEY_POSTFIX>
233  
234 The hash reference which contains the key postfixes of the Model.
235 The key of this hash is a name of the model to apply this postfix.
236  
237 This variable work same as $MODEL_KEY_PREFIX.
238  
239 =cut
240  
241 13 our $MODEL_KEY_POSTFIX = {};
242  
243 =begin comment
244  
245 The flag to use global defs setting set by the method Su::setup() directly, instead of read from Defs file.
246  
247 =end comment
248  
249 =cut
250  
251 13 our $USE_GLOBAL_SETUP = undef;
252  
253 =head1 METHODS
254  
255 =over
256  
257 =item import()
258  
259 use Su base=>'./base', proc=>'tmpls', model=>'models', defs=>'defs';
260  
261 If you want to specify some parameters from the command line, then it becomes like the following.
262  
263 perl -Ilib -MSu=base,./base,proc,tmpls,defs,models -e '{print "do some work";}'
264  
265 =cut
266  
267 13 ▶sub import {
Subroutine import: called CC=9 SCAR=31.2
268 0 ▶ my $self = shift;
Subroutine import: not called CC=9 SCAR=31.2
269  
270 # Save import list and remove from hash.
271 0 my %tmp_h = @_;
272 0 my $imports_aref = $tmp_h{import};
273  
274 0 delete $tmp_h{import};
275 0 my $base = $tmp_h{base};
276 0 my $template = $tmp_h{template};
277 0 my $defs = $tmp_h{defs};
278 0 my $model = $tmp_h{model};
279  
280 # print "base:" . Dumper($base) . "\n";
281 # print "template:" . Dumper($template) . "\n";
282 # print "model:" . Dumper($model) . "\n";
283 # print "defs:" . Dumper($defs) . "\n";
284 # $self->{logger}->trace( "base:" . Dumper($base) );
285 # $self->{logger}->trace( "template:" . Dumper($template) );
286 # $self->{logger}->trace( "model:" . Dumper($model) );
287 # $self->{logger}->trace( "defs:" . Dumper($defs) );
288 0 Su::Log->trace( "base:" . Dumper($base) );
289 0 Su::Log->trace( "template:" . Dumper($template) );
290 0 Su::Log->trace( "model:" . Dumper($model) );
291 0 Su::Log->trace( "defs:" . Dumper($defs) );
292  
293 0 ▶ $DEFS_DIR = $defs if $defs;
Branch: 2 branches
BranchTrueFalseTotal
if $defs 0 13 13
if $defs 0 0 0
294 0 ▶ $Su::Template::TEMPLATE_DIR = $template if $template;
Branch: 2 branches
BranchTrueFalseTotal
if $template 0 13 13
if $template 0 0 0
295 0 ▶ $Su::Model::MODEL_DIR = $model if $model;
Branch: 2 branches
BranchTrueFalseTotal
if $model 0 13 13
if $model 0 0 0
296  
297 # If base is specified, then this setting effects to the all modules in Su package.
298 0 ▶ if ($base) {
Branch: 2 branches
BranchTrueFalseTotal
if ($base) 5 8 13
if ($base) 0 0 0
299 13 ▶ no warnings qw(once);
Subroutine BEGIN: called CC=1 SCAR=0.0
300 0 $BASE_DIR = $base;
301 0 $Su::Template::TEMPLATE_BASE_DIR = $base;
302 0 $Su::Model::MODEL_BASE_DIR = $base;
303 } ## end if ($base)
304  
305 0 ▶ if ( $base || $template || $model || $defs ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($base or $template or $model or $defs) { } 5 8 13
if ($base or $template or $model or $defs) { } 0 0 0
Condition33%6/18
$base or $template or $model or $defs
l!l&&r!l&&!r
508
$base or $template or $model
l!l&&r!l&&!r
508
$base or $template
l!l&&r!l&&!r
508
$base or $template or $model or $defs
l!l&&r!l&&!r
000
$base or $template or $model
l!l&&r!l&&!r
000
$base or $template
l!l&&r!l&&!r
000
MC/DC25%1/4
$base or $template or $model or $defs
$base $template $model $defs
MC/DC0%0/4
$base or $template or $model or $defs
$base $template $model $defs
Truth table40%2/5
A or B or C or D
A= $baseB= $templateC= $modelD= $defs
ABCDresult
1XXX1
01XX1
001X1
00011
00000
Truth table0%0/5
A or B or C or D
A= $baseB= $templateC= $modelD= $defs
ABCDresult
1XXX1
01XX1
001X1
00011
00000
306 0 $self->export_to_level( 1, $self, @{$imports_aref} );
307 } else {
308  
309 # If '' or '' is not passed, then all of the parameters are required method names.
310 0 $self->export_to_level( 1, $self, @_ );
311 }
312  
313 } ## end sub import
314  
315 =begin comment
316  
317 Load the definition file which binds process and model to the single entry.
318 The default definition file loaded by Su is F<Defs::Defs.pm>.
319 You can specify the loading definition file as a parameter of this method.
320  
321 $su->_load_defs_file();
322 $su->_load_defs_file('Defs::CustomDefs');
323  
324 If the Defs file is already loaded, do nothing and just return it's hash.
325  
326 If you want to reload defs file force, then pass the second parameter
327 as reload option.
328  
329 $su->_load_defs_file( "Defs::Defs", 1 );
330  
331 =end comment
332  
333 =cut
334  
335 13 our $defs_module_name;
336  
337 36 ▶sub _load_defs_file {
Subroutine _load_defs_file: called CC=15 SCAR=42.8
338 0 ▶ my $self = shift if ( ref $_[0] eq __PACKAGE__ );
Subroutine _load_defs_file: not called CC=15 SCAR=42.8
Branch: 2 branches
BranchTrueFalseTotal
if ref $_[0] eq __PACKAGE__ 31 5 36
if ref $_[0] eq __PACKAGE__ 0 0 0
339  
340 0 ▶ my $BASE_DIR = $self->{base} ? $self->{base} : $BASE_DIR;
Branch: 2 branches
BranchTrueFalseTotal
$self->{'base'} ? : 13 23 36
$self->{'base'} ? : 0 0 0
341 0 ▶ my $DEFS_DIR = $self->{defs} ? $self->{defs} : $DEFS_DIR;
Branch: 2 branches
BranchTrueFalseTotal
$self->{'defs'} ? : 0 36 36
$self->{'defs'} ? : 0 0 0
342  
343 # Nothing to do if info is already set or loaded.
344 # if ( $info_href && keys %{$info_href} ) {
345 # return;
346 # }
347  
348 0 ▶ my $defs_mod_name = shift || "Defs::Defs";
Condition50%2/4
shift() || 'Defs::Defs'
l!l
333
shift() || 'Defs::Defs'
l!l
00
MC/DC100%1/1
shift() || 'Defs::Defs'
shift()
MC/DC0%0/1
shift() || 'Defs::Defs'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
Truth table0%0/2
A
A= shift()
Aresult
11
00
349 0 ▶ my $b_force_reload = shift || undef;
Condition50%2/4
shift() || undef
l!l
333
shift() || undef
l!l
00
MC/DC100%1/1
shift() || undef
shift()
MC/DC0%0/1
shift() || undef
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
Truth table0%0/2
A
A= shift()
Aresult
11
00
350  
351 0 ▶ if ( !$b_force_reload ) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($b_force_reload) 33 3 36
unless ($b_force_reload) 0 0 0
352  
353 # Defs file tring to load is already loaded.
354 0 ▶ if ($self) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self) { } 33 0 33
if ($self) { } 0 0 0
355 0 ▶ if ( defined $self->{defs_module_name}
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{"defs_module_name"} and $self->{"defs_module_name"} eq $defs_mod_name) 21 12 33
if (defined $self->{"defs_module_name"} and $self->{"defs_module_name"} eq $defs_mod_name) 0 0 0
Condition33%2/6
defined $self->{"defs_module_name"} and $self->{"defs_module_name"} eq $defs_mod_name
!ll&&!rl&&r
12021
defined $self->{"defs_module_name"} and $self->{"defs_module_name"} eq $defs_mod_name
!ll&&!rl&&r
000
MC/DC50%1/2
defined $self->{"defs_module_name"} and $self->{"defs_module_name"} eq $defs_mod_name
defined $self->{"defs_module_name"} $self->{"defs_module_name"} eq $defs_mod_name
MC/DC0%0/2
defined $self->{"defs_module_name"} and $self->{"defs_module_name"} eq $defs_mod_name
defined $self->{"defs_module_name"} $self->{"defs_module_name"} eq $defs_mod_name
Truth table66%2/3
A and B
A= defined $self->{"defs_module_name"}B= $self->{"defs_module_name"} eq $defs_mod_name
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $self->{"defs_module_name"}B= $self->{"defs_module_name"} eq $defs_mod_name
ABresult
0X0
100
111
356 && $self->{defs_module_name} eq $defs_mod_name )
357 {
358 0 return $self->{defs_href};
359 }
360 } else {
361 0 ▶ if ( defined $defs_module_name && $defs_module_name eq $defs_mod_name ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $Su::defs_module_name and $Su::defs_module_name eq $defs_mod_name) 0 0 0
if (defined $Su::defs_module_name and $Su::defs_module_name eq $defs_mod_name) 0 0 0
Condition0%0/6
defined $Su::defs_module_name and $Su::defs_module_name eq $defs_mod_name
!ll&&!rl&&r
000
defined $Su::defs_module_name and $Su::defs_module_name eq $defs_mod_name
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Su::defs_module_name and $Su::defs_module_name eq $defs_mod_name
defined $Su::defs_module_name $Su::defs_module_name eq $defs_mod_name
MC/DC0%0/2
defined $Su::defs_module_name and $Su::defs_module_name eq $defs_mod_name
defined $Su::defs_module_name $Su::defs_module_name eq $defs_mod_name
Truth table0%0/3
A and B
A= defined $Su::defs_module_nameB= $Su::defs_module_name eq $defs_mod_name
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $Su::defs_module_nameB= $Su::defs_module_name eq $defs_mod_name
ABresult
0X0
100
111
362 0 return $info_href;
363 }
364 }
365 } ## end if ( !$b_force_reload )
366  
367 # Back up the Defs module name.
368 0 ▶ if ($self) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self) { } 15 0 15
if ($self) { } 0 0 0
369 0 $self->{defs_module_name} = $defs_mod_name;
370 } else {
371 0 $defs_module_name = $defs_mod_name;
372 }
373  
374 # my $info_path;
375 # if ( $BASE_DIR eq './' ) {
376 # $info_path = $DEFS_DIR . "/" . $DEFS_MOD_NAME . ".pm";
377 # } else {
378 # $info_path = $BASE_DIR . "/" . $DEFS_DIR . "/" . $DEFS_MOD_NAME . ".pm";
379 # }
380  
381 # Unload Defs module.
382 0 ▶ if ($b_force_reload) {
Branch: 2 branches
BranchTrueFalseTotal
if ($b_force_reload) 3 12 15
if ($b_force_reload) 0 0 0
383 0 _unload_module($defs_mod_name);
384 }
385 0 my $proc = Su::Process->new;
386 0 $proc->load_module($defs_mod_name);
387  
388 # require $defs_mod_name;
389  
390 0 ▶ if ($self) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self) { } 15 0 15
if ($self) { } 0 0 0
391 0 $self->{defs_href} = $defs_mod_name->defs;
392 } else {
393 0 $info_href = $defs_mod_name->defs;
394 }
395 0 return $defs_mod_name->defs;
396  
397 } ## end sub _load_defs_file
398  
399 =item setup()
400  
401 Instead of loading the definition form the Definition file, this method set the definition directly.
402  
403 Su::setup(
404 menu =>{proc=>'MenuTmpl', model=>qw(main sites about)},
405 book_comp =>{proc=>'BookTmpl', model=>'MenuModel'},
406 menuWithArg =>{proc=>'MenuTmplWithArg', model=>{field1=>{type=>'string'},field2=>{type=>'number'}}},
407 );
408  
409 =cut
410  
411 4 ▶sub setup {
Subroutine setup: called CC=3 SCAR=15.1
Pod documented
412 0 ▶ my $self = shift if ( ref $_[0] eq __PACKAGE__ );
Subroutine setup: not called CC=3 SCAR=15.1
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if ref $_[0] eq __PACKAGE__ 0 4 4
if ref $_[0] eq __PACKAGE__ 0 0 0
413  
414 0 ▶ if ( ref $_[0] eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 2 2 4
if (ref $_[0] eq 'HASH') { } 0 0 0
415 0 $info_href = shift;
416 } else {
417 0 my %h = @_;
418 0 $info_href = \%h;
419 }
420  
421 } ## end sub setup
422  
423 =item new()
424  
425 Instantiate the Su instance.
426 To make Su instance recognize the custom definition module, you can
427 pass the package name of the definition file as a parameter.
428  
429 my $su = Su->new;
430  
431 my $su = Su->new('Pkg::Defs');
432  
433 my $su = Su->new(defs_module=>'Pkg::Defs');
434  
435 =cut
436  
437 16 ▶sub new {
Subroutine new: called CC=3 SCAR=20.5
Pod documented
438 0 ▶ my $self = shift;
Subroutine new: not called CC=3 SCAR=20.5
Pod documented
439  
440 0 ▶ if ( scalar @_ == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 1) { } 0 16 16
if (scalar @_ == 1) { } 0 0 0
441 0 my $defs_id = $_[0];
442 0 my $tmp_ref = \$defs_id;
443 0 ▶ if ( ref $tmp_ref eq 'SCALAR' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $tmp_ref eq "SCALAR") 0 0 0
if (ref $tmp_ref eq "SCALAR") 0 0 0
444 0 return bless { defs_module => $defs_id }, $self;
445 }
446 0 croak "invalid new parameter:" . @_;
447 } ## end if ( scalar @_ == 1 )
448 else {
449 0 my %h = @_;
450 0 return bless \%h, $self;
451 }
452  
453 } ## end sub new
454  
455 =item resolve()
456  
457 Find the passed id from the definition file and execute the
458 corresponding Process after the injection of the corresponding Model to
459 the Process.
460  
461 An example of the definition in F<Defs.pm> is like the following.
462  
463 my $defs =
464 {
465 entry_id =>
466 {
467 proc=>'Pkg::SomeProc',
468 model=>'Pkg::SomeModel',
469 },
470 };
471  
472 Note that C<proc> field in the definition file is required, but
473 C<model> field can omit. To execute the process descired in this
474 example, your code will become like the following.
475  
476 my $ret = $su->resolve('entry_id');
477  
478 If you pass the additional parameters to the resolve method, these
479 parameters are passed to the C<process> method of the Process.
480  
481 my $ret = $su->resolve('entry_id', 'param_A', 'param_B');
482  
483 If the passed entry id is not defined in Defs file, then the error is thorwn.
484  
485 Definition can be also specified as a parameter of the C<resolve> method like the following.
486  
487 $su->resolve({
488 proc=>'MainProc',
489 model=>['Model01','Model02','Model03'],
490 });
491  
492 $su->resolve(
493 {
494 proc => 'Sample::Procs::SomeModule',
495 model => { key1 => { 'nestkey1' => ['value'] } },
496 },
497 'arg1',
498 'arg2');
499  
500 B<Optional Usage - Model Definition>
501  
502 This method works differently according to the style of the model definition.
503  
504 If the C<model> field is a string, then Su treat it as a name of the Model, load
505 it's class and set it's C<model> field to the Process.
506  
507 some_entry_id =>{proc=>'ProcModule', model=>'ModelModule'},
508  
509 If the C<model> field is a hash, Su set it's hash to the C<model> field of
510 the Process directly.
511  
512 some_entry_id =>{proc=>'ProcModule', model=>{key1=>'value1',key2=>'value2'}},
513  
514 If the C<model> field is a reference of the string array, then Su load each
515 element as Model module and execute Process with each model.
516  
517 some_entry_id =>{proc=>'TmplModule', model=>['ModelA', 'ModelB', 'ModelC']},
518  
519 In this case, Process is executed with each Model, and the array of
520 each result is returned.
521  
522 B<Optional Usage - Filters>
523  
524 If a definition has any filter related fields, then these filter
525 methods are applied before Su return the result of the process method.
526 The module specified as a filter must has the method which corresponds
527 to the filter type. About usable filter types, see the section of
528 C<map_filter>, C<reduce_filter>, C<scalar_filter>.
529  
530 These filter methods receive the result of the process or previous
531 filter as a parameter, and return the filtered result to the caller or
532 next filter.
533  
534 Following is an example of the definition file to use post filter.
535  
536 my $defs =
537 {
538 exec_post_filter =>
539 {
540 proc=>'MainProc',
541 model=>['Model01','Model02','Model03'],
542 post_filter=>'FilterProc'
543 },
544  
545 Multiple filters can be set to the definition file.
546  
547 exec_post_filter_chain =>
548 {
549 proc=>'MainProc',
550 model=>['Model01','Model02','Model03'],
551 post_filter=>['FilterProc1', 'FilterProc1']
552 }
553 };
554  
555 An example of the C<map_filter> method in the filter class is the following.
556 The C<map_filter> receives an array of previous result as a parameter and
557 return the result as an array.
558  
559 sub map_filter {
560 my $self = shift if ref $_[0] eq __PACKAGE__;
561 my @results = @_;
562
563 for (@results) {
564 # Do some filter process.
565 }
566 return @results;
567 }
568  
569 An example of the C<reduce_filter> method in the filter class is the
570 following. The C<reduce_filter> receives an array as a parameter and
571 return the result as a scalar.
572  
573 sub reduce_filter {
574 my $self = shift if ref $_[0] eq __PACKAGE__;
575 my @results = @_;
576
577 # For example, just join the result and return.
578 return join( ',', @results );
579 }
580  
581 An example of the C<scalar_filter> method in the filter class is the
582 following. The C<scalar_filter> receives a scalar as a parameter and
583 return the result as a scalar.
584  
585 sub scalar_filter {
586 my $self = shift if ref $_[0] eq __PACKAGE__;
587 my $result = shift;
588
589 # Do some filter process to the $result.
590
591 return $result;
592 }
593  
594 This method change the Model file to load by the specified locale.
595  
596 If you specify the resource locale to $Su::MODEL_LOCALE, this method
597 load locale specific Model automatically, and locale specific Model
598 is not exist, then this method tring to load default Model.
599  
600 Set the locale variable like the following:
601  
602 $Su::MODEL_LOCALE = 'ja_JP';
603  
604 The name of locale specific Model is like the following:
605  
606 pkg::SomeModel__ja_JP
607  
608 And the file name becomes like this.
609  
610 pkg/SomeModel__ja_JP.pm
611  
612 =cut
613  
614 45 ▶sub resolve {
Subroutine resolve: called CC=47 SCAR=60.1
Pod documented
615 0 ▶ my $self = shift if ( ref $_[0] eq __PACKAGE__ );
Subroutine resolve: not called CC=47 SCAR=60.1
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if ref $_[0] eq __PACKAGE__ 39 6 45
if ref $_[0] eq __PACKAGE__ 0 0 0
616  
617 0 my $comp_id = shift;
618 0 my @ctx = @_;
619  
620 my ( $info_href, $new_comp_id ) =
621 0 ▶ ( $self && eval { $self->isa('Su') } )
Branch: 2 branches
BranchTrueFalseTotal
$self && eval { do { $self->isa('Su') } } ? : 39 6 45
$self && eval { do { $self->isa('Su') } } ? : 0 0 0
Condition50%3/6
$self and eval { do { $self->isa("Su") } }
!ll&&!rl&&r
2439
$self and eval { do { $self->isa("Su") } }
!ll&&!rl&&r
000
MC/DC100%2/2
$self and eval { do { $self->isa("Su") } }
$self eval { do { $self->isa("Su") } }
MC/DC0%0/2
$self and eval { do { $self->isa("Su") } }
$self eval { do { $self->isa("Su") } }
Truth table100%3/3
A and B
A= $selfB= eval { do { $self->isa("Su") } }
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $selfB= eval { do { $self->isa("Su") } }
ABresult
0X0
100
111
622 ? $self->_get_info($comp_id)
623 : _get_info($comp_id);
624 0 ▶ $comp_id = $new_comp_id if $new_comp_id;
Branch: 2 branches
BranchTrueFalseTotal
if $new_comp_id 7 36 43
if $new_comp_id 0 0 0
625  
626 # If Su->{base} is specified, this effects to Template and Model, else used own value Template and Model has.
627 0 my $BASE_DIR = $self->{base};
628 0 ▶ my $MODEL_DIR = $self->{model} ? $self->{model} : $Su::Model::MODEL_DIR;
Branch: 2 branches
BranchTrueFalseTotal
$self->{'model'} ? : 0 43 43
$self->{'model'} ? : 0 0 0
629 my $TEMPLATE_DIR =
630 0 ▶ $self->{template} ? $self->{template} : $Su::Template::TEMPLATE_DIR;
Branch: 2 branches
BranchTrueFalseTotal
$self->{'template'} ? : 0 43 43
$self->{'template'} ? : 0 0 0
631  
632 0 my $proc = Su::Process->new( base => $BASE_DIR, dir => $TEMPLATE_DIR );
633 0 my $proc_id = $info_href->{$comp_id}->{proc};
634 croak 'proc not set in '
635 . (
636 $self->{defs_module_name}
637 0 ▶ ? $self->{defs_module_name} . ":${comp_id}"
Branch: 4 branches
BranchTrueFalseTotal
unless $proc_id 1 42 43
$self->{'defs_module_name'} ? : 0 1 1
unless $proc_id 0 0 0
$self->{'defs_module_name'} ? : 0 0 0
638 : 'the passed definition'
639 )
640 . '.'
641 unless $proc_id;
642 0 my $tmpl_module = $proc->load_module($proc_id);
643  
644 # Save executed module to the instance.
645 0 ▶ $self->{module} = $tmpl_module if $self;
Branch: 2 branches
BranchTrueFalseTotal
if $self 42 0 42
if $self 0 0 0
646  
647 0 my @ret_arr = ();
648  
649 # Still not refactored!
650  
651 # If the setter method of the field 'model' exists.
652 0 ▶ if ( $tmpl_module->can('model') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tmpl_module->can("model")) 37 5 42
if ($tmpl_module->can("model")) 0 0 0
653  
654 # model is hash reference. so pass it direct.
655 0 ▶ if ( ref $info_href->{$comp_id}->{model} eq 'HASH' ) {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $info_href->{$comp_id}{'model'} eq 'HASH') { } 4 33 37
elsif (ref $info_href->{$comp_id}{'model'} eq 'ARRAY') { } 3 30 33
if (ref $info_href->{$comp_id}{'model'} eq 'HASH') { } 0 0 0
elsif (ref $info_href->{$comp_id}{'model'} eq 'ARRAY') { } 0 0 0
656 0 $tmpl_module->model( $info_href->{$comp_id}->{model} );
657 } elsif ( ref $info_href->{$comp_id}->{model} eq 'ARRAY' ) {
658  
659 } else {
660  
661 # this should be model class name.
662  
663 0 my $mdl = Su::Model->new( base => $BASE_DIR, dir => $MODEL_DIR );
664 0 my $loading_model = $info_href->{$comp_id}->{model};
665 0 chomp $loading_model;
666  
667 # Add locale postfix if postfix is specified.
668 0 my $base_loading_model = $loading_model;
669 0 ▶ $loading_model .= '__' . $Su::MODEL_LOCALE if ($Su::MODEL_LOCALE);
Branch: 2 branches
BranchTrueFalseTotal
if $Su::MODEL_LOCALE 5 25 30
if $Su::MODEL_LOCALE 0 0 0
670  
671 # If locale specific model is not exist, then load default model file.
672 0 Su::Log->trace( 'loading model:' . $loading_model );
673 0 ▶ if ($loading_model) {
Branch: 2 branches
BranchTrueFalseTotal
if ($loading_model) 30 0 30
if ($loading_model) 0 0 0
674 0 my $model = $mdl->load_model( $loading_model, { suppress_error => 1 } );
675 0 ▶ $model = $mdl->load_model($base_loading_model) unless $model;
Branch: 2 branches
BranchTrueFalseTotal
unless $model 3 27 30
unless $model 0 0 0
676  
677 # Load global field from Defs file.
678 0 %{$model} = ( %{$model}, %{ $info_href->{$GLOBAL_MODEL_FIELD} } )
679 0 ▶ if defined $info_href->{$GLOBAL_MODEL_FIELD};
Branch: 2 branches
BranchTrueFalseTotal
if defined $info_href->{$Su::GLOBAL_MODEL_FIELD} 2 28 30
if defined $info_href->{$Su::GLOBAL_MODEL_FIELD} 0 0 0
680  
681 # Get the prefix or postfix setting for loading model.
682 0 ▶ my $MODEL_KEY_PREFIX = $MODEL_KEY_PREFIX->{$loading_model} || '';
Condition50%2/4
$Su::MODEL_KEY_PREFIX->{$loading_model} || ''
l!l
228
$Su::MODEL_KEY_PREFIX->{$loading_model} || ''
l!l
00
MC/DC100%1/1
$Su::MODEL_KEY_PREFIX->{$loading_model} || ''
$Su::MODEL_KEY_PREFIX->{$loading_model}
MC/DC0%0/1
$Su::MODEL_KEY_PREFIX->{$loading_model} || ''
$Su::MODEL_KEY_PREFIX->{$loading_model}
Truth table100%2/2
A
A= $Su::MODEL_KEY_PREFIX->{$loading_model}
Aresult
11
00
Truth table0%0/2
A
A= $Su::MODEL_KEY_PREFIX->{$loading_model}
Aresult
11
00
683 0 ▶ my $MODEL_KEY_POSTFIX = $MODEL_KEY_POSTFIX->{$loading_model} || '';
Condition50%2/4
$Su::MODEL_KEY_POSTFIX->{$loading_model} || ''
l!l
228
$Su::MODEL_KEY_POSTFIX->{$loading_model} || ''
l!l
00
MC/DC100%1/1
$Su::MODEL_KEY_POSTFIX->{$loading_model} || ''
$Su::MODEL_KEY_POSTFIX->{$loading_model}
MC/DC0%0/1
$Su::MODEL_KEY_POSTFIX->{$loading_model} || ''
$Su::MODEL_KEY_POSTFIX->{$loading_model}
Truth table100%2/2
A
A= $Su::MODEL_KEY_POSTFIX->{$loading_model}
Aresult
11
00
Truth table0%0/2
A
A= $Su::MODEL_KEY_POSTFIX->{$loading_model}
Aresult
11
00
684  
685 # If the key prefix or postfix is specified, copy the value of the
686 # modified key to original key value.
687 0 ▶ if ( $MODEL_KEY_PREFIX || $MODEL_KEY_POSTFIX ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($MODEL_KEY_PREFIX or $MODEL_KEY_POSTFIX) 3 27 30
if ($MODEL_KEY_PREFIX or $MODEL_KEY_POSTFIX) 0 0 0
Condition50%3/6
$MODEL_KEY_PREFIX or $MODEL_KEY_POSTFIX
l!l&&r!l&&!r
2127
$MODEL_KEY_PREFIX or $MODEL_KEY_POSTFIX
l!l&&r!l&&!r
000
MC/DC100%2/2
$MODEL_KEY_PREFIX or $MODEL_KEY_POSTFIX
$MODEL_KEY_PREFIX $MODEL_KEY_POSTFIX
MC/DC0%0/2
$MODEL_KEY_PREFIX or $MODEL_KEY_POSTFIX
$MODEL_KEY_PREFIX $MODEL_KEY_POSTFIX
Truth table100%3/3
A or B
A= $MODEL_KEY_PREFIXB= $MODEL_KEY_POSTFIX
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $MODEL_KEY_PREFIXB= $MODEL_KEY_POSTFIX
ABresult
1X1
011
000
688 0 my $new_model = {};
689 0 foreach my $key ( keys %{$model} ) {
690 0 ▶ if (
Branch: 6 branches
BranchTrueFalseTotal
if (exists $$model{$MODEL_KEY_PREFIX . '__' . $key . '__' . $MODEL_KEY_POSTFIX}) { } 1 11 12
elsif (exists $$model{$MODEL_KEY_PREFIX . '__' . $key}) { } 3 8 11
elsif (exists $$model{$key . '__' . $MODEL_KEY_POSTFIX}) { } 3 5 8
if (exists $$model{$MODEL_KEY_PREFIX . '__' . $key . '__' . $MODEL_KEY_POSTFIX}) { } 0 0 0
elsif (exists $$model{$MODEL_KEY_PREFIX . '__' . $key}) { } 0 0 0
elsif (exists $$model{$key . '__' . $MODEL_KEY_POSTFIX}) { } 0 0 0
691 exists $model->{ $MODEL_KEY_PREFIX . '__'
692 . $key . '__'
693 . $MODEL_KEY_POSTFIX } )
694 {
695 $new_model->{$key} =
696 0 $model->{ $MODEL_KEY_PREFIX . '__'
697 . $key . '__'
698 . $MODEL_KEY_POSTFIX };
699 } elsif ( exists $model->{ $MODEL_KEY_PREFIX . '__' . $key } ) {
700 0 $new_model->{$key} = $model->{ $MODEL_KEY_PREFIX . '__' . $key };
701 } elsif ( exists $model->{ $key . '__' . $MODEL_KEY_POSTFIX } ) {
702 0 $new_model->{$key} = $model->{ $key . '__' . $MODEL_KEY_POSTFIX };
703 } else {
704 0 $new_model->{$key} = $model->{$key};
705 }
706 } ## end foreach my $key ( keys %{$model...})
707 0 $model = $new_model;
708 } ## end if ( $MODEL_KEY_PREFIX...)
709 0 $tmpl_module->model($model);
710  
711 } ## end if ($loading_model)
712 } ## end else [ if ( ref $info_href->{...})]
713 } ## end if ( $tmpl_module->can...)
714  
715 # Just return proc instance.
716 0 ▶ if ( $self->{just_return_module} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{"just_return_module"}) 3 39 42
if ($self->{"just_return_module"}) 0 0 0
717 0 return $tmpl_module;
718 }
719  
720 0 ▶ if ( $tmpl_module->can('model') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tmpl_module->can("model")) 34 5 39
if ($tmpl_module->can("model")) 0 0 0
721 0 ▶ if ( ref $info_href->{$comp_id}->{model} eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $info_href->{$comp_id}{"model"} eq "ARRAY") 3 31 34
if (ref $info_href->{$comp_id}{"model"} eq "ARRAY") 0 0 0
722  
723 # Call module method with each of models.
724 0 my $mdl = Su::Model->new( base => $BASE_DIR, dir => $MODEL_DIR );
725  
726 0 for my $loaded_model ( @{ $info_href->{$comp_id}->{model} } ) {
727  
728 #diag("model:" . $info_href->{$comp_id}->{model});
729 #diag("loaded:" . $mdl->load_model($info_href->{$comp_id}->{model}));
730 0 chomp $loaded_model;
731 0 ▶ if ($loaded_model) {
Branch: 2 branches
BranchTrueFalseTotal
if ($loaded_model) 9 0 9
if ($loaded_model) 0 0 0
732 0 $tmpl_module->model( $mdl->load_model($loaded_model) );
733 0 push @ret_arr, $tmpl_module->process(@ctx);
734 }
735 } ## end for my $loaded_model ( ...)
736  
737 } ## end if ( ref $info_href->{...})
738 } ## end if ( $tmpl_module->can...)
739  
740 0 my @filters = ();
741 0 my $reduce_filter;
742 0 my @scalar_filters = ();
743  
744 # Collect post filters.
745 0 ▶ if ( $info_href->{$comp_id}->{map_filter} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($info_href->{$comp_id}{"map_filter"}) 5 34 39
if ($info_href->{$comp_id}{"map_filter"}) 0 0 0
746  
747 # The single filter is set as class name string.
748 0 ▶ if ( ref $info_href->{$comp_id}->{map_filter} eq '' ) {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $info_href->{$comp_id}{'map_filter'} eq '') { } 5 0 5
elsif (ref $info_href->{$comp_id}{'map_filter'} eq 'ARRAY') { } 0 0 0
if (ref $info_href->{$comp_id}{'map_filter'} eq '') { } 0 0 0
elsif (ref $info_href->{$comp_id}{'map_filter'} eq 'ARRAY') { } 0 0 0
749 0 push @filters, $info_href->{$comp_id}->{map_filter};
750 } elsif ( ref $info_href->{$comp_id}->{map_filter} eq 'ARRAY' ) {
751  
752 # The filters are set as array reference.
753 0 @filters = @{ $info_href->{$comp_id}->{map_filter} };
754 }
755  
756 } ## end if ( $info_href->{$comp_id...})
757  
758 # Collect reduce filter.
759 # Note:Multiple reduce filter not permitted to set.
760 0 ▶ if ( $info_href->{$comp_id}->{reduce_filter} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($info_href->{$comp_id}{"reduce_filter"}) 2 37 39
if ($info_href->{$comp_id}{"reduce_filter"}) 0 0 0
761  
762 # The single filter is set as class name string.
763 0 ▶ if ( ref $info_href->{$comp_id}->{reduce_filter} eq '' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $info_href->{$comp_id}{"reduce_filter"} eq "") 2 0 2
if (ref $info_href->{$comp_id}{"reduce_filter"} eq "") 0 0 0
764 0 $reduce_filter = $info_href->{$comp_id}->{reduce_filter};
765 }
766 } ## end if ( $info_href->{$comp_id...})
767  
768 # Collect scalar filters
769 0 ▶ if ( $info_href->{$comp_id}->{scalar_filter} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($info_href->{$comp_id}{"scalar_filter"}) 3 36 39
if ($info_href->{$comp_id}{"scalar_filter"}) 0 0 0
770  
771 # The single filter is set as class name string.
772 0 ▶ if ( ref $info_href->{$comp_id}->{scalar_filter} eq '' ) {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $info_href->{$comp_id}{'scalar_filter'} eq '') { } 3 0 3
elsif (ref $info_href->{$comp_id}{'scalar_filter'} eq 'ARRAY') { } 0 0 0
if (ref $info_href->{$comp_id}{'scalar_filter'} eq '') { } 0 0 0
elsif (ref $info_href->{$comp_id}{'scalar_filter'} eq 'ARRAY') { } 0 0 0
773 0 push @scalar_filters, $info_href->{$comp_id}->{scalar_filter};
774 } elsif ( ref $info_href->{$comp_id}->{scalar_filter} eq 'ARRAY' ) {
775  
776 # The filters are set as array reference.
777 0 @scalar_filters = $info_href->{$comp_id}->{scalar_filter};
778 }
779  
780 } ## end if ( $info_href->{$comp_id...})
781  
782 # Multiple data process return it's result array.
783 0 ▶ if (@ret_arr) {
Branch: 2 branches
BranchTrueFalseTotal
if (@ret_arr) 3 36 39
if (@ret_arr) 0 0 0
784 0 for my $elm (@filters) {
785 my $tmpl_filter_module =
786 0 $proc->load_module( $info_href->{$comp_id}->{map_filter} );
787 0 @ret_arr = $tmpl_filter_module->map_filter(@ret_arr);
788 }
789  
790 #Todo: Multiple data process not implemented to apply reduce filter and scalar filter.
791 0 return @ret_arr;
792 } ## end if (@ret_arr)
793  
794 0 my @single_ret_arr = ( $tmpl_module->process(@ctx) );
795  
796 # Apply map filters.
797 0 for my $elm (@filters) {
798 0 my $tmpl_filter_module = $proc->load_module($elm);
799 0 @single_ret_arr = $tmpl_filter_module->map_filter(@single_ret_arr);
800 }
801  
802 0 ▶ return ( scalar @single_ret_arr == 1 ? $single_ret_arr[0] : @single_ret_arr )
Branch: 4 branches
BranchTrueFalseTotal
unless $reduce_filter or @scalar_filters 32 4 36
scalar @single_ret_arr == 1 ? : 31 1 32
unless $reduce_filter or @scalar_filters 0 0 0
scalar @single_ret_arr == 1 ? : 0 0 0
Condition50%3/6
$reduce_filter or @scalar_filters
l!l&&r!l&&!r
2232
$reduce_filter or @scalar_filters
l!l&&r!l&&!r
000
MC/DC100%2/2
$reduce_filter or @scalar_filters
$reduce_filter @scalar_filters
MC/DC0%0/2
$reduce_filter or @scalar_filters
$reduce_filter @scalar_filters
Truth table100%3/3
A or B
A= $reduce_filterB= @scalar_filters
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $reduce_filterB= @scalar_filters
ABresult
1X1
011
000
803 unless ( $reduce_filter or @scalar_filters );
804  
805 0 my $reduced_result = '';
806  
807 # Apply reduce filter once.
808 0 ▶ if ($reduce_filter) {
Branch: 4 branches
BranchTrueFalseTotal
if ($reduce_filter) { } 2 2 4
elsif (scalar @single_ret_arr == 1) { } 1 1 2
if ($reduce_filter) { } 0 0 0
elsif (scalar @single_ret_arr == 1) { } 0 0 0
809 0 my $reduce_filter_module = $proc->load_module($reduce_filter);
810 0 $reduced_result = $reduce_filter_module->reduce_filter(@single_ret_arr);
811 } elsif ( scalar @single_ret_arr == 1 ) {
812 0 $reduced_result = $single_ret_arr[0];
813 } else {
814 0 croak
815 "[ERROR]Can't apply scalar filter(s), because the result of the process is multiple and not reduced by the reduce filter";
816 }
817  
818 #Apply scalar filter to the single process result.
819 0 for my $elm (@scalar_filters) {
820 0 my $tmpl_filter_module = $proc->load_module($elm);
821 0 $reduced_result = $tmpl_filter_module->scalar_filter($reduced_result);
822 }
823 0 return $reduced_result;
824  
825 } ## end sub resolve
826  
827 =begin comment
828  
829 Read Process and Model infomation from Defs file.
830  
831 =end comment
832  
833 =cut
834  
835 45 ▶sub _get_info {
Subroutine _get_info: called CC=16 SCAR=41.3
836 0 ▶ my $self = shift if ( ref $_[0] eq __PACKAGE__ );
Subroutine _get_info: not called CC=16 SCAR=41.3
Branch: 2 branches
BranchTrueFalseTotal
if ref $_[0] eq __PACKAGE__ 39 6 45
if ref $_[0] eq __PACKAGE__ 0 0 0
837 0 my $comp_id = shift;
838 0 my $info_href;
839  
840 # If the flag $USE_GLOBAL_SETUP is set, use the setting set by the
841 # method Su::setup.
842 0 ▶ if ($USE_GLOBAL_SETUP) {
Branch: 4 branches
BranchTrueFalseTotal
if ($Su::USE_GLOBAL_SETUP) { } 8 37 45
elsif ($self and UNIVERSAL::isa($self, 'UNIVERSAL') and $self->isa('Su')) { } 31 6 37
if ($Su::USE_GLOBAL_SETUP) { } 0 0 0
elsif ($self and UNIVERSAL::isa($self, 'UNIVERSAL') and $self->isa('Su')) { } 0 0 0
843 0 $info_href = $Su::info_href;
844 ▶ } elsif ( $self
Condition33%4/12
$self and UNIVERSAL::isa($self, "UNIVERSAL") and $self->isa("Su")
!ll&&!rl&&r
6031
$self and UNIVERSAL::isa($self, "UNIVERSAL")
!ll&&!rl&&r
6031
$self and UNIVERSAL::isa($self, "UNIVERSAL") and $self->isa("Su")
!ll&&!rl&&r
000
$self and UNIVERSAL::isa($self, "UNIVERSAL")
!ll&&!rl&&r
000
MC/DC33%1/3
$self and UNIVERSAL::isa($self, "UNIVERSAL") and $self->isa("Su")
$self UNIVERSAL::isa($self, "UNIVERSAL") $self->isa("Su")
MC/DC0%0/3
$self and UNIVERSAL::isa($self, "UNIVERSAL") and $self->isa("Su")
$self UNIVERSAL::isa($self, "UNIVERSAL") $self->isa("Su")
Truth table50%2/4
A and B and C
A= $selfB= UNIVERSAL::isa($self, "UNIVERSAL")C= $self->isa("Su")
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and B and C
A= $selfB= UNIVERSAL::isa($self, "UNIVERSAL")C= $self->isa("Su")
ABCresult
0XX0
10X0
1100
1111
845 && UNIVERSAL::isa( $self, 'UNIVERSAL' )
846 && $self->isa('Su') )
847 {
848  
849 # If hash is passed, just use passed info, and not load defs file.
850 $self->_load_defs_file( $self->{defs_module} )
851 0 ▶ unless ref $comp_id eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $comp_id eq "HASH" 25 6 31
unless ref $comp_id eq "HASH" 0 0 0
852 } else {
853  
854 # called as global method like 'Su::resolve("id")'.
855 0 ▶ unless ( ref $comp_id eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
unless (ref $comp_id eq "HASH") 6 0 6
unless (ref $comp_id eq "HASH") 0 0 0
856  
857 # If Su::setup is called, then use global setting, else load setting
858 # from defs file.
859 $info_href =
860 0 ▶ keys %{$Su::info_href} ? $Su::info_href : _load_defs_file();
Branch: 2 branches
BranchTrueFalseTotal
keys %{$Su::info_href;} ? : 1 5 6
keys %{$Su::info_href;} ? : 0 0 0
861  
862 # _load_defs_file();
863 0 Su::Log->trace( 'comp_id:' . $comp_id );
864 0 Su::Log->trace( 'new set:' . Dumper($info_href) );
865 } ## end unless ( ref $comp_id eq 'HASH')
866  
867 # _load_defs_file();
868 } ## end else [ if ($USE_GLOBAL_SETUP)]
869  
870 # If defs info is passed as paramter, then use it.
871 0 ▶ if ( ref $comp_id eq 'HASH' ) {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $comp_id eq 'HASH') { } 7 38 45
elsif (not $info_href) { } 25 13 38
if (ref $comp_id eq 'HASH') { } 0 0 0
elsif (not $info_href) { } 0 0 0
872 0 $info_href = { 'dmy_id' => $comp_id };
873  
874 # Set dummy id to use passed parameter.
875 0 $comp_id = 'dmy_id';
876 } elsif ( !$info_href ) {
877  
878 # $self->{defs_href} and $Su::info_href is set by _load_defs_file().
879 0 ▶ $info_href = $self->{defs_href} ? $self->{defs_href} : $Su::info_href;
Branch: 2 branches
BranchTrueFalseTotal
$self->{'defs_href'} ? : 25 0 25
$self->{'defs_href'} ? : 0 0 0
880 }
881  
882 0 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
unless ($info_href->{$comp_id} and (ref $info_href->{$comp_id} eq "HASH" and keys %{$$info_href{$comp_id};})) 2 43 45
unless ($info_href->{$comp_id} and (ref $info_href->{$comp_id} eq "HASH" and keys %{$$info_href{$comp_id};})) 0 0 0
Condition25%3/12
$info_href->{$comp_id} and (ref $info_href->{$comp_id} eq "HASH" and keys %{$$info_href{$comp_id};})
!ll&&!rl&&r
2043
ref $info_href->{$comp_id} eq "HASH" and keys %{$$info_href{$comp_id};}
!ll&&!rl&&r
0043
$info_href->{$comp_id} and (ref $info_href->{$comp_id} eq "HASH" and keys %{$$info_href{$comp_id};})
!ll&&!rl&&r
000
ref $info_href->{$comp_id} eq "HASH" and keys %{$$info_href{$comp_id};}
!ll&&!rl&&r
000
MC/DC33%1/3
$info_href->{$comp_id} and (ref $info_href->{$comp_id} eq "HASH" and keys %{$$info_href{$comp_id};})
$info_href->{$comp_id} ref $info_href->{$comp_id} eq "HASH" keys %{$$info_href{$comp_id};}
MC/DC0%0/3
$info_href->{$comp_id} and (ref $info_href->{$comp_id} eq "HASH" and keys %{$$info_href{$comp_id};})
$info_href->{$comp_id} ref $info_href->{$comp_id} eq "HASH" keys %{$$info_href{$comp_id};}
Truth table50%2/4
A and B and C
A= $info_href->{$comp_id}B= ref $info_href->{$comp_id} eq "HASH"C= keys %{$$info_href{$comp_id};}
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and B and C
A= $info_href->{$comp_id}B= ref $info_href->{$comp_id} eq "HASH"C= keys %{$$info_href{$comp_id};}
ABCresult
0XX0
10X0
1100
1111
883 !$info_href->{$comp_id}
884 || !(
885 0 ref $info_href->{$comp_id} eq 'HASH' && keys %{ $info_href->{$comp_id} }
886 )
887 )
888 {
889 0 croak "Entry id '$comp_id' is not found in Defs file:"
890 . Dumper($info_href);
891 } ## end if ( !$info_href->{$comp_id...})
892  
893 0 ▶ return ( $info_href, $comp_id eq 'dmy_id' ? $comp_id : 0 );
Branch: 2 branches
BranchTrueFalseTotal
$comp_id eq 'dmy_id' ? : 7 36 43
$comp_id eq 'dmy_id' ? : 0 0 0
894  
895 } ## end sub _get_info
896  
897 =item get_proc()
898  
899 This function is just a synonym of the method L<get_instance>.
900  
901 =cut
902  
903 1 ▶sub get_proc {
Subroutine get_proc: called CC=3 SCAR=17.0
Pod documented
904 0 ▶ my $self = shift if ( ref $_[0] eq __PACKAGE__ );
Subroutine get_proc: not called CC=3 SCAR=17.0
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if ref $_[0] eq __PACKAGE__ 1 0 1
if ref $_[0] eq __PACKAGE__ 0 0 0
905 0 my $ret;
906 0 ▶ if ($self) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self) { } 1 0 1
if ($self) { } 0 0 0
907 0 $ret = $self->get_instance(@_);
908 } else {
909 0 $ret = get_instance(@_);
910 }
911 0 return $ret;
912 } ## end sub get_proc
913  
914 =item
915  
916 Just return the instance of the Process which defined in Defs
917 file. Model data is set to that returned Process.
918  
919 my $proc = $su->get_instance('main_proc');
920  
921 =cut
922  
923 3 ▶sub get_instance {
Subroutine get_instance: called CC=2 SCAR=10.0
Pod undocumented
924 0 ▶ my $self = shift if ( ref $_[0] eq __PACKAGE__ );
Subroutine get_instance: not called CC=2 SCAR=10.0
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
if ref $_[0] eq __PACKAGE__ 3 0 3
if ref $_[0] eq __PACKAGE__ 0 0 0
925 0 my $comp_id = shift;
926  
927 # just_return_module is a flag not to execute process and just return
928 # the instance of the process itself.
929 0 $self->{just_return_module} = 1;
930 0 my $proc = $self->resolve($comp_id);
931 0 $self->{just_return_module} = undef;
932 0 return $proc;
933 } ## end sub get_instance
934  
935 =item get_inst()
936  
937 This function is just a synonym of the method L<get_instance>.
938  
939 =cut
940  
941 0 ▶sub get_inst {
Subroutine get_inst: not called CC=3 SCAR=24.8
Pod documented
942 0 ▶ my $self = shift if ( ref $_[0] eq __PACKAGE__ );
Subroutine get_inst: not called CC=3 SCAR=24.8
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if ref $_[0] eq __PACKAGE__ 0 0 0
if ref $_[0] eq __PACKAGE__ 0 0 0
943 0 my $ret;
944 0 ▶ if ($self) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self) { } 0 0 0
if ($self) { } 0 0 0
945 0 $ret = $self->get_instance(@_);
946 } else {
947 0 $ret = get_instance(@_);
948 }
949 0 return $ret;
950 } ## end sub get_inst
951  
952 =item retr()
953  
954 This function is just a synonym of the method L<get_instance>.
955  
956 =cut
957  
958 1 ▶sub retr {
Subroutine retr: called CC=3 SCAR=17.0
Pod documented
959 0 ▶ my $self = shift if ( ref $_[0] eq __PACKAGE__ );
Subroutine retr: not called CC=3 SCAR=17.0
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if ref $_[0] eq __PACKAGE__ 1 0 1
if ref $_[0] eq __PACKAGE__ 0 0 0
960 0 my $ret;
961 0 ▶ if ($self) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self) { } 1 0 1
if ($self) { } 0 0 0
962 0 $ret = $self->get_instance(@_);
963 } else {
964 0 $ret = get_instance(@_);
965 }
966 0 return $ret;
967 } ## end sub retr
968  
969 =item inst()
970  
971 This function is just a synonym of the method L<get_instance>.
972  
973 =cut
974  
975 0 ▶sub inst {
Subroutine inst: not called CC=3 SCAR=24.8
Pod documented
976 0 ▶ my $self = shift if ( ref $_[0] eq __PACKAGE__ );
Subroutine inst: not called CC=3 SCAR=24.8
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if ref $_[0] eq __PACKAGE__ 0 0 0
if ref $_[0] eq __PACKAGE__ 0 0 0
977 0 my $ret;
978 0 ▶ if ($self) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self) { } 0 0 0
if ($self) { } 0 0 0
979 0 $ret = $self->get_instance(@_);
980 } else {
981 0 $ret = get_instance(@_);
982 }
983 0 return $ret;
984 } ## end sub inst
985  
986 =item init()
987  
988 Generate the initial files at once. The initial files are composed of
989 Defs, Model and Process module.
990  
991 Su::init('PkgName');
992  
993 This method can be called from command line like the following:
994  
995 perl -MSu=base,base/directory -e 'Su::init("Pkg::SomeModule")'
996  
997 =cut
998  
999 0 ▶sub init {
Subroutine init: not called CC=4 SCAR=27.9
Pod documented
1000 0 ▶ my $self = shift if ( ref $_[0] eq __PACKAGE__ );
Subroutine init: not called CC=4 SCAR=27.9
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if ref $_[0] eq __PACKAGE__ 0 0 0
if ref $_[0] eq __PACKAGE__ 0 0 0
1001 0 my $pkg = shift;
1002  
1003 0 ▶ die "The parameter package name is requqired." unless $pkg;
Branch: 2 branches
BranchTrueFalseTotal
unless $pkg 0 0 0
unless $pkg 0 0 0
1004  
1005 # Note that the package of defs file is fixed and don't reflect the passed package name.
1006 13 ▶ no warnings qw(once);
Subroutine BEGIN: called CC=1 SCAR=0.0
1007 0 ▶ if ($self) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self) { } 0 0 0
if ($self) { } 0 0 0
1008  
1009 # The method 'init' use the fixed module and method name. Only the package name can be specified.
1010 0 $self->gen_defs( package => $pkg );
1011 0 $self->gen_model("${pkg}::${DEFAULT_MODEL_NAME}");
1012 0 $self->gen_proc("${pkg}::${DEFAULT_PROC_NAME}");
1013 } else {
1014 0 gen_defs( package => $pkg );
1015 0 gen_model("${pkg}::Model");
1016 0 gen_proc("${pkg}::MainProc");
1017  
1018 } ## end else [ if ($self) ]
1019  
1020 } ## end sub init
1021  
1022 =item gen_model()
1023  
1024 Generate a Model file.
1025  
1026 Su::gen_model("SomePkg::SomeModelName")
1027  
1028 perl -MSu=base,./lib/ -e 'Su::gen_model("Pkg::ModelName")'
1029  
1030 =cut
1031  
1032 3 ▶sub gen_model {
Subroutine gen_model: called CC=3 SCAR=16.5
Pod documented
1033 0 ▶ my $self = shift if ( ref $_[0] eq __PACKAGE__ );
Subroutine gen_model: not called CC=3 SCAR=16.5
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if ref $_[0] eq __PACKAGE__ 3 0 3
if ref $_[0] eq __PACKAGE__ 0 0 0
1034 0 ▶ my $BASE_DIR = $self->{base} ? $self->{base} : $BASE_DIR;
Branch: 2 branches
BranchTrueFalseTotal
$self->{'base'} ? : 3 0 3
$self->{'base'} ? : 0 0 0
1035 0 my $mdl = Su::Model->new( base => $BASE_DIR );
1036 0 $mdl->generate_model(@_);
1037  
1038 } ## end sub gen_model
1039  
1040 =item gen_proc()
1041  
1042 Generate a Process file.
1043  
1044 perl -MSu=base,./lib/ -e 'Su::gen_proc("Pkg::TestProc")'
1045  
1046 =cut
1047  
1048 3 ▶sub gen_proc {
Subroutine gen_proc: called CC=3 SCAR=16.5
Pod documented
1049 0 ▶ my $self = shift if ( ref $_[0] eq __PACKAGE__ );
Subroutine gen_proc: not called CC=3 SCAR=16.5
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if ref $_[0] eq __PACKAGE__ 3 0 3
if ref $_[0] eq __PACKAGE__ 0 0 0
1050 0 ▶ my $BASE_DIR = $self->{base} ? $self->{base} : $BASE_DIR;
Branch: 2 branches
BranchTrueFalseTotal
$self->{'base'} ? : 3 0 3
$self->{'base'} ? : 0 0 0
1051 0 my $proc = Su::Process->new( base => $BASE_DIR );
1052 0 $proc->generate_proc(@_);
1053  
1054 # my $generated_file = $proc->generate_proc(@_);
1055  
1056 } ## end sub gen_proc
1057  
1058 =item generate()
1059  
1060 Generate a pair of Process and Model file.
1061  
1062 my $su = Su->new;
1063 $su->generate('pkg::SomeProc');
1064  
1065 This example generates C<pkg/SomeProc.pm> and C<pkg/SomeModl.pm>.
1066  
1067 You can use this method from the commandline.
1068  
1069 perl -MSu=base,lib -e 'Su::generate("Pkg::SomeProc", 1)'
1070 perl -MSu=base,lib -e 'Su::generate("Pkg::SomeProc", "Defs::MyDefs")'
1071  
1072 If the second parameter is specified, the Defs file will generated.
1073  
1074 =cut
1075  
1076 3 ▶sub generate {
Subroutine generate: called CC=11 SCAR=37.5
Pod documented
1077 0 ▶ my $self = shift if ( ref $_[0] eq __PACKAGE__ );
Subroutine generate: not called CC=11 SCAR=37.5
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if ref $_[0] eq __PACKAGE__ 3 0 3
if ref $_[0] eq __PACKAGE__ 0 0 0
1078 0 my $fqcn = shift;
1079 0 my $gen_def = shift;
1080  
1081 # Save original fqcn.
1082 0 my $proc_fqcn = $fqcn;
1083 0 my $model_fqcn;
1084  
1085 0 ▶ return unless $fqcn;
Branch: 2 branches
BranchTrueFalseTotal
unless $fqcn 0 3 3
unless $fqcn 0 0 0
1086  
1087 # Generate process.
1088 0 ▶ my $proc_fname = $self ? $self->gen_proc($fqcn) : gen_proc($fqcn);
Branch: 2 branches
BranchTrueFalseTotal
$self ? : 3 0 3
$self ? : 0 0 0
1089  
1090 # Check whether the parameter has the postfix 'Proc'.
1091 0 ▶ if ( $fqcn =~ /.+Proc$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fqcn =~ /.+Proc$/) { } 1 2 3
if ($fqcn =~ /.+Proc$/) { } 0 0 0
1092  
1093 # Replace 'Proc' postfix to 'Model' to generate Model file.
1094 0 $fqcn =~ s/(.+)Proc$/$1Model/;
1095 } else {
1096  
1097 # Just add the postfix 'Model' to the passed param.
1098 0 $fqcn .= "Model";
1099 }
1100 0 $model_fqcn = $fqcn;
1101  
1102 # Generate model.
1103 0 ▶ my $model_fname = $self ? $self->gen_model($fqcn) : gen_model($fqcn);
Branch: 2 branches
BranchTrueFalseTotal
$self ? : 3 0 3
$self ? : 0 0 0
1104  
1105 0 $proc_fname =~ s/\.pm$//;
1106 0 $proc_fname =~ s!/!::!g;
1107  
1108 0 $model_fname =~ s/\.pm$//;
1109 0 $model_fname =~ s!/!::!g;
1110  
1111 0 ▶ my $defs_fname = _is_string($gen_def) ? $gen_def : 'Defs::Defs';
Branch: 2 branches
BranchTrueFalseTotal
_is_string($gen_def) ? : 0 3 3
_is_string($gen_def) ? : 0 0 0
1112  
1113 # Generate defs file.
1114 0 ▶ if (
Branch: 4 branches
BranchTrueFalseTotal
if ($gen_def or $self ? $self->_is_defs_exist($defs_fname) : _is_defs_exist($defs_fname)) { } 3 0 3
$self ? : 1 0 1
if ($gen_def or $self ? $self->_is_defs_exist($defs_fname) : _is_defs_exist($defs_fname)) { } 0 0 0
$self ? : 0 0 0
Condition33%2/6
$gen_def or $self ? $self->_is_defs_exist($defs_fname) : _is_defs_exist($defs_fname)
l!l&&r!l&&!r
210
$gen_def or $self ? $self->_is_defs_exist($defs_fname) : _is_defs_exist($defs_fname)
l!l&&r!l&&!r
000
MC/DC0%0/2
$gen_def or $self ? $self->_is_defs_exist($defs_fname) : _is_defs_exist($defs_fname)
$gen_def $self ? $self->_is_defs_exist($defs_fname) : _is_defs_exist($defs_fname)
MC/DC0%0/2
$gen_def or $self ? $self->_is_defs_exist($defs_fname) : _is_defs_exist($defs_fname)
$gen_def $self ? $self->_is_defs_exist($defs_fname) : _is_defs_exist($defs_fname)
Truth table66%2/3
A or B
A= $gen_defB= $self ? $self->_is_defs_exist($defs_fname) : _is_defs_exist($defs_fname)
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $gen_defB= $self ? $self->_is_defs_exist($defs_fname) : _is_defs_exist($defs_fname)
ABresult
1X1
011
000
1115 $gen_def
1116 || (
1117 $self ? $self->_is_defs_exist($defs_fname) : _is_defs_exist($defs_fname) )
1118 )
1119 {
1120 0 my @pkg_arr = split( '::', $fqcn );
1121 0 my $pkg = @pkg_arr[ 0 .. ( scalar @pkg_arr - 2 ) ];
1122  
1123 0 ▶ if ($self) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self) { } 3 0 3
if ($self) { } 0 0 0
1124 0 $self->gen_defs(
1125 name => $defs_fname,
1126  
1127 # package => $pkg,
1128 proc => $proc_fqcn,
1129 model => $model_fqcn,
1130 just_add_entry_if_defs_already_exist => 1,
1131 use_proc_name_as_entry_id => 1,
1132 );
1133  
1134 # $self->gen_defs( package => $defs_fname );
1135 } else {
1136 0 gen_defs(
1137 name => $defs_fname,
1138  
1139 # package => $pkg,
1140 proc => $proc_fqcn,
1141 model => $model_fqcn,
1142 just_add_entry_if_defs_already_exist => 1,
1143 use_proc_name_as_entry_id => 1,
1144 );
1145  
1146 } ## end else [ if ($self) ]
1147 } ## end if ( $gen_def || ( $self...))
1148 else {
1149 0 my $entry_id = _make_entry_id($proc_fqcn);
1150 0 my $output = <<"__HERE__";
1151 An example of the entry to add to the Defs file.
1152  
1153 $entry_id => {
1154 proc => '$proc_fqcn',
1155 model => '$model_fqcn',
1156 },
1157 __HERE__
1158  
1159 0 print $output;
1160 } ## end else [ if ( $gen_def || ( $self...))]
1161 } ## end sub generate
1162  
1163 =item gen_defs()
1164  
1165 Generate a definition file.
1166  
1167 perl -MSu=base,./lib/ -e 'Su::gen_defs()'
1168  
1169 You can specify the package name of the definition file as a parameter.
1170  
1171 gen_defs('Defs::Defs');
1172  
1173 Also you can specify other parameters as a hash.
1174  
1175 gen_defs(name=>'Defs::Defs',
1176 package=>'pkg',
1177 proc=>'MyProc',
1178 model=>'MyModel',
1179 just_add_entry_if_defs_already_exist => 1,
1180 use_proc_name_as_entry_id => 1)
1181  
1182 param use_proc_name_as_entry_id:
1183 Set the proc name as entry id instead of default id 'main'.
1184  
1185 param just_add_entry_if_defs_already_exist:
1186 If the specified Defs file is already exist, then add the entry to that Defs file.
1187  
1188 return:
1189 1: If generation success.
1190 0: If the Defs file already exists.
1191  
1192 =cut
1193  
1194 6 ▶sub gen_defs {
Subroutine gen_defs: called CC=6 SCAR=25.8
Pod documented
1195 0 ▶ my $self = shift if ( ref $_[0] eq __PACKAGE__ );
Subroutine gen_defs: not called CC=6 SCAR=25.8
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if ref $_[0] eq __PACKAGE__ 6 0 6
if ref $_[0] eq __PACKAGE__ 0 0 0
1196  
1197 0 ▶ my $ret =
Branch: 2 branches
BranchTrueFalseTotal
$self ? : 6 0 6
$self ? : 0 0 0
1198 $self
1199 ? $self->_gen_defs_with_template_id( 'DefsPm', @_ )
1200 : _gen_defs_with_template_id( 'DefsPm', @_ );
1201  
1202 # Exclude the case of single parameter.
1203 0 ▶ if ( scalar @_ != 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ != 1) 6 0 6
if (scalar @_ != 1) 0 0 0
1204 0 my %defs_h = @_;
1205 0 ▶ if ( $ret == 0 && !$defs_h{just_add_entry_if_defs_already_exist} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ret == 0 and not $defs_h{"just_add_entry_if_defs_already_exist"}) 1 5 6
if ($ret == 0 and not $defs_h{"just_add_entry_if_defs_already_exist"}) 0 0 0
Condition50%3/6
$ret == 0 and not $defs_h{"just_add_entry_if_defs_already_exist"}
!ll&&!rl&&r
321
$ret == 0 and not $defs_h{"just_add_entry_if_defs_already_exist"}
!ll&&!rl&&r
000
MC/DC100%2/2
$ret == 0 and not $defs_h{"just_add_entry_if_defs_already_exist"}
$ret == 0 not $defs_h{"just_add_entry_if_defs_already_exist"}
MC/DC0%0/2
$ret == 0 and not $defs_h{"just_add_entry_if_defs_already_exist"}
$ret == 0 not $defs_h{"just_add_entry_if_defs_already_exist"}
Truth table100%3/3
A and not(B)
A= $ret == 0B= not $defs_h{"just_add_entry_if_defs_already_exist"}
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= $ret == 0B= not $defs_h{"just_add_entry_if_defs_already_exist"}
ABresult
0X0
100
111
1206 0 warn "[WARN] Defs file alredy exists.";
1207 }
1208 } ## end if ( scalar @_ != 1 )
1209 0 return $ret;
1210  
1211 } ## end sub gen_defs
1212  
1213 =begin comment
1214  
1215 param template_id: File name to load template string.
1216 return:
1217 1: If generation success.
1218 0: If the Defs file already exists.
1219  
1220 =end comment
1221  
1222 =cut
1223  
1224 6 ▶sub _gen_defs_with_template_id {
Subroutine _gen_defs_with_template_id: called CC=4 SCAR=19.0
1225 0 ▶ my $self = shift if ( ref $_[0] eq __PACKAGE__ );
Subroutine _gen_defs_with_template_id: not called CC=4 SCAR=19.0
Branch: 2 branches
BranchTrueFalseTotal
if ref $_[0] eq __PACKAGE__ 6 0 6
if ref $_[0] eq __PACKAGE__ 0 0 0
1226 0 my $template_id = shift;
1227 0 my $template_string;
1228 13 ▶ use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
1229  
1230 0 my $template_fname =
1231 dirname(__FILE__) . $SU_TMPL_DIR . $template_id . '.tmpl';
1232  
1233 0 ▶ open( my $F, '<', $template_fname ) or die $! . ":$template_fname";
Branch: 2 branches
BranchTrueFalseTotal
&Su::open(my $F, "<", $template_fname) or die $! . ":$template_fname" 6 0 6
&Su::open(my $F, "<", $template_fname) or die $! . ":$template_fname" 0 0 0
1234 0 $template_string = join '', <$F>;
1235  
1236 0 ▶ return $self
Branch: 2 branches
BranchTrueFalseTotal
$self ? : 6 0 6
$self ? : 0 0 0
1237 ? $self->_gen_defs_internal( $template_string, @_ )
1238 : _gen_defs_internal( $template_string, @_ );
1239 } ## end sub _gen_defs_with_template_id
1240  
1241 =begin comment
1242  
1243 param1: Template string to expand.
1244 return:
1245 1: If generation success.
1246 0: If the Defs file already exists.
1247  
1248 =end comment
1249  
1250 =cut
1251  
1252 6 ▶sub _gen_defs_internal {
Subroutine _gen_defs_internal: called CC=22 SCAR=47.5
1253 0 ▶ my $self = shift if ( ref $_[0] eq __PACKAGE__ );
Subroutine _gen_defs_internal: not called CC=22 SCAR=47.5
Branch: 2 branches
BranchTrueFalseTotal
if ref $_[0] eq __PACKAGE__ 6 0 6
if ref $_[0] eq __PACKAGE__ 0 0 0
1254 0 my $template_string = shift;
1255 0 my $defs_id;
1256 my %defs_h;
1257  
1258 # The single parameter is Defs file name.
1259 0 ▶ if ( scalar @_ == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 1) { } 0 6 6
if (scalar @_ == 1) { } 0 0 0
1260 0 ▶ $defs_id = shift || $DEFS_MODULE_NAME;
Condition0%0/6
shift() || $Su::DEFS_MODULE_NAME
l!l&&r!l&&!r
000
shift() || $Su::DEFS_MODULE_NAME
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $Su::DEFS_MODULE_NAME
shift() $Su::DEFS_MODULE_NAME
MC/DC0%0/2
shift() || $Su::DEFS_MODULE_NAME
shift() $Su::DEFS_MODULE_NAME
Truth table0%0/3
A || B
A= shift()B= $Su::DEFS_MODULE_NAME
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= shift()B= $Su::DEFS_MODULE_NAME
ABresult
1X1
011
000
1261 } else {
1262  
1263 # Else the hash of parameters.
1264 0 %defs_h = @_;
1265  
1266 0 ▶ $defs_id = $defs_h{name} || $defs_h{file_name} || $DEFS_MODULE_NAME;
Condition33%4/12
$defs_h{'name'} || $defs_h{'file_name'} || $Su::DEFS_MODULE_NAME
l!l&&r!l&&!r
330
$defs_h{"name"} or $defs_h{"file_name"}
l!l&&r!l&&!r
303
$defs_h{'name'} || $defs_h{'file_name'} || $Su::DEFS_MODULE_NAME
l!l&&r!l&&!r
000
$defs_h{"name"} or $defs_h{"file_name"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$defs_h{'name'} || $defs_h{'file_name'} || $Su::DEFS_MODULE_NAME
$defs_h{"name"} $defs_h{"file_name"} $Su::DEFS_MODULE_NAME
MC/DC0%0/3
$defs_h{'name'} || $defs_h{'file_name'} || $Su::DEFS_MODULE_NAME
$defs_h{"name"} $defs_h{"file_name"} $Su::DEFS_MODULE_NAME
Truth table50%2/4
A or B || C
A= $defs_h{"name"}B= $defs_h{"file_name"}C= $Su::DEFS_MODULE_NAME
ABCresult
1XX1
01X1
0011
0000
Truth table0%0/4
A or B || C
A= $defs_h{"name"}B= $defs_h{"file_name"}C= $Su::DEFS_MODULE_NAME
ABCresult
1XX1
01X1
0011
0000
1267  
1268 } ## end else [ if ( scalar @_ == 1 ) ]
1269  
1270 0 ▶ my $BASE_DIR = $self->{base} ? $self->{base} : $BASE_DIR;
Branch: 2 branches
BranchTrueFalseTotal
$self->{'base'} ? : 6 0 6
$self->{'base'} ? : 0 0 0
1271 0 ▶ my $DEFS_DIR = $self->{defs} ? $self->{defs} : $DEFS_DIR;
Branch: 2 branches
BranchTrueFalseTotal
$self->{'defs'} ? : 0 6 6
$self->{'defs'} ? : 0 0 0
1272  
1273 # Make directory path.
1274 0 my @arr = split( '/|::', $defs_id );
1275 0 my $defs_base_name = '';
1276 0 ▶ if ( scalar @arr > 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @arr > 1) 3 3 6
if (scalar @arr > 1) 0 0 0
1277 0 $defs_base_name = join( '/', @arr[ 0 .. scalar @arr - 2 ] );
1278 }
1279  
1280 0 my $dir;
1281 0 ▶ if ( $defs_id =~ /::|\// ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($defs_id =~ m[::|/]) { } 3 3 6
if ($defs_id =~ m[::|/]) { } 0 0 0
1282 0 $dir = $BASE_DIR . "/" . $defs_base_name;
1283 } else {
1284 0 $dir = $BASE_DIR . "/" . $DEFS_DIR . "/" . $defs_base_name;
1285 }
1286  
1287 # Prepare directory for generate file.
1288 0 ▶ mkpath $dir unless ( -d $dir );
Branch: 2 branches
BranchTrueFalseTotal
unless -d $dir 2 4 6
unless -d $dir 0 0 0
1289  
1290 0 ▶ if ( !-d $dir ) {
Branch: 2 branches
BranchTrueFalseTotal
unless (-d $dir) 0 6 6
unless (-d $dir) 0 0 0
1291 0 die "Can't make dir:" . $!;
1292 }
1293  
1294 0 my $defs_id_filepath = $defs_id;
1295 0 $defs_id_filepath =~ s!::!/!g;
1296  
1297 # Generate file.
1298 0 my $fpath;
1299 0 ▶ if ( $defs_id =~ /::|\// ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($defs_id =~ m[::|/]) { } 3 3 6
if ($defs_id =~ m[::|/]) { } 0 0 0
1300 0 $fpath = $BASE_DIR . "/" . $defs_id_filepath . ".pm";
1301 } else {
1302 0 $fpath = $BASE_DIR . "/" . $DEFS_DIR . "/" . $defs_id_filepath . ".pm";
1303 }
1304  
1305 0 $defs_id =~ s/\//::/g;
1306  
1307 0 ▶ if ( $defs_id !~ /::/ ) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($defs_id =~ /::/) 3 3 6
unless ($defs_id =~ /::/) 0 0 0
1308 0 my $defs_dir_for_package = $DEFS_DIR;
1309 0 $defs_dir_for_package =~ s!/!::!g;
1310  
1311 #Note: Automatically add the default package Models.
1312 0 $defs_id = $defs_dir_for_package . '::' . $defs_id;
1313 } ## end if ( $defs_id !~ /::/ )
1314  
1315 0 ▶ my $defs_proc_name = $defs_h{proc} || $DEFAULT_PROC_NAME;
Condition33%2/6
$defs_h{'proc'} || $Su::DEFAULT_PROC_NAME
l!l&&r!l&&!r
330
$defs_h{'proc'} || $Su::DEFAULT_PROC_NAME
l!l&&r!l&&!r
000
MC/DC0%0/2
$defs_h{'proc'} || $Su::DEFAULT_PROC_NAME
$defs_h{'proc'} $Su::DEFAULT_PROC_NAME
MC/DC0%0/2
$defs_h{'proc'} || $Su::DEFAULT_PROC_NAME
$defs_h{'proc'} $Su::DEFAULT_PROC_NAME
Truth table66%2/3
A || B
A= $defs_h{'proc'}B= $Su::DEFAULT_PROC_NAME
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $defs_h{'proc'}B= $Su::DEFAULT_PROC_NAME
ABresult
1X1
011
000
1316 0 ▶ my $defs_model_name = $defs_h{model} || $DEFAULT_MODEL_NAME;
Condition33%2/6
$defs_h{'model'} || $Su::DEFAULT_MODEL_NAME
l!l&&r!l&&!r
330
$defs_h{'model'} || $Su::DEFAULT_MODEL_NAME
l!l&&r!l&&!r
000
MC/DC0%0/2
$defs_h{'model'} || $Su::DEFAULT_MODEL_NAME
$defs_h{'model'} $Su::DEFAULT_MODEL_NAME
MC/DC0%0/2
$defs_h{'model'} || $Su::DEFAULT_MODEL_NAME
$defs_h{'model'} $Su::DEFAULT_MODEL_NAME
Truth table66%2/3
A || B
A= $defs_h{'model'}B= $Su::DEFAULT_MODEL_NAME
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $defs_h{'model'}B= $Su::DEFAULT_MODEL_NAME
ABresult
1X1
011
000
1317 0 ▶ my $pkg = $defs_h{package} || $defs_h{pkg} || "";
Condition20%2/10
$defs_h{'package'} || $defs_h{'pkg'} || ''
l!l
06
$defs_h{"package"} or $defs_h{"pkg"}
l!l&&r!l&&!r
006
$defs_h{'package'} || $defs_h{'pkg'} || ''
l!l
00
$defs_h{"package"} or $defs_h{"pkg"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$defs_h{'package'} || $defs_h{'pkg'} || ''
$defs_h{"package"} $defs_h{"pkg"}
MC/DC0%0/2
$defs_h{'package'} || $defs_h{'pkg'} || ''
$defs_h{"package"} $defs_h{"pkg"}
Truth table33%1/3
A or B
A= $defs_h{"package"}B= $defs_h{"pkg"}
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $defs_h{"package"}B= $defs_h{"pkg"}
ABresult
1X1
011
000
1318 0 my $main_entry_id = 'main';
1319  
1320 # If the Defs file is already exist.
1321 0 ▶ if ( -f $fpath ) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $fpath) 3 3 6
if (-f $fpath) 0 0 0
1322  
1323 # Add the entry to the Defs file which already exists.
1324 0 ▶ if ( $defs_h{just_add_entry_if_defs_already_exist} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($defs_h{'just_add_entry_if_defs_already_exist'}) { } 2 1 3
if ($defs_h{'just_add_entry_if_defs_already_exist'}) { } 0 0 0
1325  
1326 0 my $entry_id = _make_entry_id($defs_proc_name);
1327 0 open( my $I, $fpath );
1328 0 my $content = join '', <$I>;
1329 0 close $I;
1330 0 my $tmpl_str = <<"__HERE__";
1331 ,
1332 ${entry_id} =>
1333 {
1334 proc=>"${pkg}${defs_proc_name}",
1335 model=>"${pkg}${defs_model_name}",
1336 },
1337 __HERE__
1338  
1339 0 $content =~ s/(# \[The mark to add the entries\])/$tmpl_str\n$1/;
1340  
1341 0 open( my $FO, '>', $fpath );
1342 0 print $FO $content;
1343 0 close $FO;
1344 } else {
1345  
1346 # Do Nothing.
1347 }
1348 0 return 0;
1349 } ## end if ( -f $fpath )
1350  
1351 # Make entry id.
1352 0 ▶ if ( $defs_h{use_proc_name_as_entry_id} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($defs_h{"use_proc_name_as_entry_id"}) 1 2 3
if ($defs_h{"use_proc_name_as_entry_id"}) 0 0 0
1353 0 $main_entry_id = _make_entry_id($defs_proc_name);
1354 }
1355  
1356 0 open( my $file, '>', $fpath );
1357  
1358 0 my $ft = Su::Template->new;
1359  
1360 13 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
1361  
1362 # Make package name, else remain empty.
1363 0 ▶ $pkg = $pkg ? ( $pkg . '::' ) : '';
Branch: 2 branches
BranchTrueFalseTotal
$pkg ? : 0 3 3
$pkg ? : 0 0 0
1364  
1365 0 my $contents =
1366 $ft->expand( $template_string, $defs_id, $pkg, $defs_proc_name,
1367 $defs_model_name, $main_entry_id );
1368  
1369 0 print $file $contents;
1370 0 return 1;
1371  
1372 } ## end sub _gen_defs_internal
1373  
1374 =begin comment
1375  
1376 Return 1 if the type of the passed argment is a string.
1377 If the parameter type is a number or reference then this method return 0.
1378 If the string "true" is passed, this method return 0;
1379  
1380 =end comment
1381  
1382 =cut
1383  
1384 3 ▶sub _is_string {
Subroutine _is_string: called CC=5 SCAR=27.0
1385 0 ▶ my $arg = shift;
Subroutine _is_string: not called CC=5 SCAR=27.0
1386 0 ▶ if ( $arg && ( $arg ^ $arg ) ne '0' && !( ref $arg ) && $arg ne 'true' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg and ($arg ^ $arg) ne '0' and not ref $arg and $arg ne 'true') { } 0 3 3
if ($arg and ($arg ^ $arg) ne '0' and not ref $arg and $arg ne 'true') { } 0 0 0
Condition22%4/18
$arg and ($arg ^ $arg) ne 0 and not ref $arg and $arg ne "true"
!ll&&!rl&&r
300
$arg and ($arg ^ $arg) ne 0 and not ref $arg
!ll&&!rl&&r
300
$arg and ($arg ^ $arg) ne 0
!ll&&!rl&&r
120
$arg and ($arg ^ $arg) ne 0 and not ref $arg and $arg ne "true"
!ll&&!rl&&r
000
$arg and ($arg ^ $arg) ne 0 and not ref $arg
!ll&&!rl&&r
000
$arg and ($arg ^ $arg) ne 0
!ll&&!rl&&r
000
MC/DC0%0/4
$arg and ($arg ^ $arg) ne 0 and not ref $arg and $arg ne "true"
$arg ($arg ^ $arg) ne 0 not ref $arg $arg ne "true"
MC/DC0%0/4
$arg and ($arg ^ $arg) ne 0 and not ref $arg and $arg ne "true"
$arg ($arg ^ $arg) ne 0 not ref $arg $arg ne "true"
Truth table40%2/5
A and B and not(C) and D
A= $argB= ($arg ^ $arg) ne 0C= not ref $argD= $arg ne "true"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
Truth table0%0/5
A and B and not(C) and D
A= $argB= ($arg ^ $arg) ne 0C= not ref $argD= $arg ne "true"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1387 0 return 1;
1388 } else {
1389 0 return 0;
1390 }
1391  
1392 } ## end sub _is_string
1393  
1394 =begin comment
1395  
1396 Extract the class name from the passed parameter and make lower case it's firsr charactor.
1397  
1398 param: The string of fully qualified name.
1399 return: The class name converted to lower case of it's first charactor.
1400  
1401 =end comment
1402  
1403 =cut
1404  
1405 3 ▶sub _make_entry_id {
Subroutine _make_entry_id: called CC=1 SCAR=1.2
1406 0 ▶ my $arg = shift;
Subroutine _make_entry_id: not called CC=1 SCAR=1.2
1407 0 my @name_elem = split( '::', $arg );
1408 0 return lcfirst $name_elem[ scalar @name_elem - 1 ];
1409  
1410 } ## end sub _make_entry_id
1411  
1412 =begin comment
1413  
1414 Return 1 if passed argument is reference of empty hash.
1415 Note that if argument type is not reference, then return 1;
1416 Non-hash type parameter also return 1.
1417  
1418 Note: Currently not used.
1419  
1420 =end comment
1421  
1422 =cut
1423  
1424 # sub is_hash_empty {
1425 # my $self = shift if ( ref $_[0] eq __PACKAGE__ );
1426 # my $href = shift;
1427 # return 1 if ( !$href );
1428 # if ( ref $href eq 'HASH' ) {
1429 # if ( keys %{$href} ) {
1430 # return 0;
1431 # } else {
1432 # return 1;
1433 # }
1434 # } ## end if ( ref $href eq 'HASH')
1435 # return 1;
1436 # } ## end sub is_hash_empty
1437  
1438 =begin comment
1439  
1440 Unload the passed module.
1441  
1442 _unload_module('Defs::Defs.pm');
1443  
1444 =end comment
1445  
1446 =cut
1447  
1448 3 ▶sub _unload_module {
Subroutine _unload_module: called CC=2 SCAR=9.0
1449 0 ▶ my $self = shift if ( ref $_[0] eq __PACKAGE__ );
Subroutine _unload_module: not called CC=2 SCAR=9.0
Branch: 2 branches
BranchTrueFalseTotal
if ref $_[0] eq __PACKAGE__ 0 3 3
if ref $_[0] eq __PACKAGE__ 0 0 0
1450 0 my $fqmn = shift;
1451 0 my @path_elems = split '::', $fqmn;
1452  
1453 {
1454 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.1
1455 0 @{ $fqmn . '::ISA' } = ();
1456 0 %{ $fqmn . '::' } = ();
1457 0 delete ${ ( join '::', @path_elems[ 0 .. $#path_elems - 1 ] ) . '::' }
1458 0 { $path_elems[-1] . '::' };
1459 0 delete $INC{ ( join '/', @path_elems ) . '.pm' };
1460 }
1461  
1462 } ## end sub _unload_module
1463  
1464 =begin comment
1465  
1466 Return true if the Defs file is exist.
1467  
1468 param: Defs module name or file path.
1469  
1470 return:
1471 1: If the Defs file exist.
1472 undef: If the Defs file not exist.
1473  
1474 =end comment
1475  
1476 =cut
1477  
1478 1 ▶sub _is_defs_exist {
Subroutine _is_defs_exist: called CC=6 SCAR=27.9
1479 0 ▶ my $self = shift if ( ref $_[0] eq __PACKAGE__ );
Subroutine _is_defs_exist: not called CC=6 SCAR=27.9
Branch: 2 branches
BranchTrueFalseTotal
if ref $_[0] eq __PACKAGE__ 1 0 1
if ref $_[0] eq __PACKAGE__ 0 0 0
1480 0 ▶ my $defs_id = shift || $DEFS_MODULE_NAME;
Condition16%1/6
shift() || $Su::DEFS_MODULE_NAME
l!l&&r!l&&!r
100
shift() || $Su::DEFS_MODULE_NAME
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $Su::DEFS_MODULE_NAME
shift() $Su::DEFS_MODULE_NAME
MC/DC0%0/2
shift() || $Su::DEFS_MODULE_NAME
shift() $Su::DEFS_MODULE_NAME
Truth table33%1/3
A || B
A= shift()B= $Su::DEFS_MODULE_NAME
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= shift()B= $Su::DEFS_MODULE_NAME
ABresult
1X1
011
000
1481  
1482 0 ▶ my $BASE_DIR = $self->{base} ? $self->{base} : $BASE_DIR;
Branch: 2 branches
BranchTrueFalseTotal
$self->{'base'} ? : 1 0 1
$self->{'base'} ? : 0 0 0
1483 0 ▶ my $DEFS_DIR = $self->{defs} ? $self->{defs} : $DEFS_DIR;
Branch: 2 branches
BranchTrueFalseTotal
$self->{'defs'} ? : 0 1 1
$self->{'defs'} ? : 0 0 0
1484  
1485 0 my $defs_id_filepath = $defs_id;
1486 0 $defs_id_filepath =~ s/::/\//;
1487 0 my $fpath;
1488 0 ▶ if ( $defs_id =~ /::|\// ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($defs_id =~ m[::|/]) { } 1 0 1
if ($defs_id =~ m[::|/]) { } 0 0 0
1489 0 $fpath = $BASE_DIR . "/" . $defs_id_filepath . ".pm";
1490 } else {
1491 0 $fpath = $BASE_DIR . "/" . $DEFS_DIR . "/" . $defs_id_filepath . ".pm";
1492 }
1493 0 return -f $fpath;
1494 } ## end sub _is_defs_exist
1495  
1496 13 1;
1497  
1498 __END__
↑ Summary Su/Build/CustomBuild.pm »