Su/Model.pm

filter: Statementstmt 54.5% 127 / 233 Branchbran 40.0% 48 / 120 Conditioncond 27.7% 5 / 18 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 72.7% 16 / 22 Podpod 100% 8 / 8 totaltotal 49.8% 206 / 413 CC 67 SCAR 33.2
CC
67
Cov
49%
load_model
43.5
load_model
43.5
generate_model
40.9
CRAP 27.7
« Su/Build/CustomBuild.pm ↑ Summary Su/Template.pm »
1 package Su::Model;
2  
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 15 ▶use Su::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use Su::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 15 our @ISA = qw(Exporter);
16  
17 15 our @EXPORT = qw(generate_model load_model );
18  
19 15 our $MODEL_DIR = "Models";
20  
21 15 our $MODEL_BASE_DIR = "./";
22  
23 15 our $MODEL_CACHE_HREF = {};
24  
25 =pod
26  
27 =head1 NAME
28  
29 Su::Model - A module to treat user data.
30  
31 =head1 SYNOPSYS
32  
33 Su::Model::load_model('Pkg::SomeModel', {share => 1} )->{field_A} = $value;
34  
35 my $value = Su::Model::load_model('Pkg::SomeModel')->{field_A};
36  
37 =head1 DESCRIPTION
38  
39 Su::Model holds the data used in your application.
40 For convenience, Su provides method to generate Model class.
41  
42 use Su::Model;
43 generate_model('NewModel');
44  
45 =head1 ATTRIBUTES
46  
47 =cut
48  
49 15 our $_attr = {};
50  
51 =head1 FUNCTIONS
52  
53 =over
54  
55 =cut
56  
57 19 ▶sub import {
Subroutine import: called CC=5 SCAR=25.2
58 0 ▶ my $self = shift;
Subroutine import: not called CC=5 SCAR=25.2
59 0 my %tmp_h = @_;
60 0 my $imports_aref = $tmp_h{import};
61 0 delete $tmp_h{import};
62 0 my $base = $tmp_h{base};
63 0 my $dir = $tmp_h{dir};
64  
65 # print "base:" . Dumper($base) . "\n";
66 # print "dir:" . Dumper($dir) . "\n";
67  
68 0 ▶ $MODEL_BASE_DIR = $base if $base;
Branch: 2 branches
BranchTrueFalseTotal
if $base 0 19 19
if $base 0 0 0
69 0 ▶ $MODEL_DIR = $dir if $dir;
Branch: 2 branches
BranchTrueFalseTotal
if $dir 0 19 19
if $dir 0 0 0
70  
71 0 ▶ if ( $base || $dir ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($base or $dir) { } 0 19 19
if ($base or $dir) { } 0 0 0
Condition16%1/6
$base or $dir
l!l&&r!l&&!r
0019
$base or $dir
l!l&&r!l&&!r
000
MC/DC0%0/2
$base or $dir
$base $dir
MC/DC0%0/2
$base or $dir
$base $dir
Truth table33%1/3
A or B
A= $baseB= $dir
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $baseB= $dir
ABresult
1X1
011
000
72 0 $self->export_to_level( 1, $self, @{$imports_aref} );
73 } else {
74  
75 # If '' or '' is not passed, then all of the parameters are required method names.
76 0 $self->export_to_level( 1, $self, @_ );
77 }
78  
79 } ## end sub import
80  
81 =item attr()
82  
83 Save the passed data in application scope.
84  
85 Su::Model->attr( 'key1', 'value1' );
86 my $value = Su::Model->attr('key1');
87  
88 Su::Model->attr->{key4} = 'value4';
89 my $value = Su::Model->attr->{key4};
90  
91 =cut
92  
93 10 ▶sub attr {
Subroutine attr: called CC=5 SCAR=23.3
Pod documented
94 0 ▶ my $self = shift if ( ref $_[0] eq __PACKAGE__ or $_[0] eq __PACKAGE__ );
Subroutine attr: not called CC=5 SCAR=23.3
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if ref $_[0] eq __PACKAGE__ or $_[0] eq __PACKAGE__ 10 0 10
if ref $_[0] eq __PACKAGE__ or $_[0] eq __PACKAGE__ 0 0 0
Condition16%1/6
ref $_[0] eq __PACKAGE__ or $_[0] eq __PACKAGE__
l!l&&r!l&&!r
0100
ref $_[0] eq __PACKAGE__ or $_[0] eq __PACKAGE__
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $_[0] eq __PACKAGE__ or $_[0] eq __PACKAGE__
ref $_[0] eq __PACKAGE__ $_[0] eq __PACKAGE__
MC/DC0%0/2
ref $_[0] eq __PACKAGE__ or $_[0] eq __PACKAGE__
ref $_[0] eq __PACKAGE__ $_[0] eq __PACKAGE__
Truth table33%1/3
A or B
A= ref $_[0] eq __PACKAGE__B= $_[0] eq __PACKAGE__
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= ref $_[0] eq __PACKAGE__B= $_[0] eq __PACKAGE__
ABresult
1X1
011
000
95 0 my $key = shift;
96 0 my $value = shift;
97 0 ▶ if ( !defined $key ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $key) { } 2 8 10
elsif (defined $value) { } 4 4 8
if (not defined $key) { } 0 0 0
elsif (defined $value) { } 0 0 0
98  
99 # If no argment is passed, return hash ref itself.
100 0 return $_attr;
101 } elsif ( defined $value ) {
102 0 $_attr->{$key} = $value;
103 } else {
104 0 return $_attr->{$key};
105 }
106 } ## end sub attr
107  
108 =item new()
109  
110 A Constructor.
111  
112 =cut
113  
114 42 ▶sub new {
Subroutine new: called CC=1 SCAR=1.2
Pod documented
115 0 ▶ my $self = shift;
Subroutine new: not called CC=1 SCAR=1.2
Pod documented
116 0 my %h = @_;
117 0 my $log = Su::Log->new;
118 0 $h{logger} = $log;
119 0 $h{models} = {};
120 0 return bless \%h, $self;
121 } ## end sub new
122  
123 =item generate_model()
124  
125 generate_model('SomeModel', qw(field1 string field2 number field3 date));
126 $mdl->generate_model('Nest/Mdl2');
127 $mdl->generate_model('Nest::Mdl3');
128 $mdl->generate_model('Nest::Mdl4',"field1",{"key1"=>"value1","key2"=>"value2"},"field2","value3");
129  
130 generate_model(NAME, &rest @(FIELD, VALUE));
131  
132 Generate the model class using the passed model name.
133 If the optional parameters are passed, then generate the model class
134 using the passed parameter as the value of the model field of the
135 Model.
136 VALUE can be specified as scalar or hash reference.
137  
138 The model field of the generated Model is like the following.
139  
140 my $model=
141 {
142 field1 => "value1",
143 field2 => {somekey => "hashvalue"},
144 };
145  
146  
147 You can generate Model class from command line using the following command.
148  
149 perl -I../lib -MSu::Model -e '{generate_model("ModelClass",field1,"value1",field2,"value2")}'
150 perl -I../lib -MSu::Model -e '{generate_model("Pkg::ModelClass",field1,"value1",field2,"value2")}'
151  
152 If you want to specify the directory to generate the Model class, then
153 pass the C<base> parameter like the following sample.
154  
155 perl -MSu::Model=base,lib -e '{generate_model("ModelClass",field1,value1,field2,value2)}'
156  
157 You can specify the package name using the C<dir> parameter.
158  
159 perl -MSu::Model=dir,PkgName -e '{generate_model("ModelClass",field1,value1,field2,value2)}'
160  
161 Note that if the model name is specified with qualified package name,
162 then this C<dir> parameter not effect.
163  
164 If generation is success, this subroutine return the generated file
165 name, else should die or return undef.
166  
167 =cut
168  
169 11 ▶sub generate_model {
Subroutine generate_model: called CC=13 SCAR=37.1
Pod documented
170 0 ▶ my $self = shift if ( ref $_[0] eq __PACKAGE__ );
Subroutine generate_model: not called CC=13 SCAR=37.1
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if ref $_[0] eq __PACKAGE__ 8 3 11
if ref $_[0] eq __PACKAGE__ 0 0 0
171 0 ▶ my $logger = $self->{logger} ? $self->{logger} : Su::Log->new;
Branch: 2 branches
BranchTrueFalseTotal
$self->{'logger'} ? : 8 3 11
$self->{'logger'} ? : 0 0 0
172  
173 # NOTE: No need this safe guard.
174 # $self = {} unless $self;
175 0 ▶ my $MODEL_BASE_DIR = $self->{base} ? $self->{base} : $MODEL_BASE_DIR;
Branch: 2 branches
BranchTrueFalseTotal
$self->{'base'} ? : 8 3 11
$self->{'base'} ? : 0 0 0
176 0 ▶ my $MODEL_DIR = $self->{dir} ? $self->{dir} : $MODEL_DIR;
Branch: 2 branches
BranchTrueFalseTotal
$self->{'dir'} ? : 5 6 11
$self->{'dir'} ? : 0 0 0
177  
178 #diag('$MODEL_BASE_DIR:' . $MODEL_BASE_DIR);
179 #diag('$MODEL_DIR:' . $MODEL_DIR);
180 0 $logger->trace( '$MODEL_BASE_DIR:' . $MODEL_BASE_DIR );
181 0 $logger->trace( '$MODEL_DIR:' . $MODEL_DIR );
182 0 my $comp_id = shift;
183 0 my @rest = @_;
184  
185 # Make directory path.
186 0 my @arr = split( '/|::', $comp_id );
187 0 my $comp_base_name = '';
188 0 ▶ if ( scalar @arr > 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @arr > 1) 6 5 11
if (scalar @arr > 1) 0 0 0
189 0 $comp_base_name = join( '/', @arr[ 0 .. scalar @arr - 2 ] );
190 }
191  
192 0 my $dir;
193 0 ▶ if ( $comp_id =~ /::|\// ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($comp_id =~ m[::|/]) { } 6 5 11
if ($comp_id =~ m[::|/]) { } 0 0 0
194 0 $dir = $MODEL_BASE_DIR . "/" . $comp_base_name;
195 } else {
196 0 $dir = $MODEL_BASE_DIR . "/" . $MODEL_DIR . "/" . $comp_base_name;
197 }
198  
199 # Prepare directory for generate file.
200 0 ▶ mkpath $dir unless ( -d $dir );
Branch: 2 branches
BranchTrueFalseTotal
unless -d $dir 2 9 11
unless -d $dir 0 0 0
201  
202 # '$!' can't judge error correctly.
203 # $! and die "$!:" . $dir;
204 0 ▶ if ( !-d $dir ) {
Branch: 2 branches
BranchTrueFalseTotal
unless (-d $dir) 0 11 11
unless (-d $dir) 0 0 0
205 0 die "Can't make dir:" . $!;
206 }
207  
208 0 my $comp_id_filepath = $comp_id;
209 0 $comp_id_filepath =~ s!::!/!g;
210  
211 # Generate file.
212 0 my $fpath;
213 0 ▶ if ( $comp_id =~ /::|\// ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($comp_id =~ m[::|/]) { } 6 5 11
if ($comp_id =~ m[::|/]) { } 0 0 0
214 0 $fpath = $MODEL_BASE_DIR . "/" . $comp_id_filepath . ".pm";
215 } else {
216 0 $fpath =
217 $MODEL_BASE_DIR . "/" . $MODEL_DIR . "/" . $comp_id_filepath . ".pm";
218 }
219  
220 0 ▶ open( my $file, '>', $fpath ) or carp "Can't open file:$fpath:" . $!;
Branch: 2 branches
BranchTrueFalseTotal
open my $file, ">", $fpath or carp("Can't open file:${fpath}:" . $!) 11 0 11
open my $file, ">", $fpath or carp("Can't open file:${fpath}:" . $!) 0 0 0
221  
222 0 $comp_id =~ s/\//::/g;
223  
224 0 ▶ if ( $comp_id !~ /::/ ) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($comp_id =~ /::/) 5 6 11
unless ($comp_id =~ /::/) 0 0 0
225 0 my $model_dir_for_package = $MODEL_DIR;
226 0 $model_dir_for_package =~ s!/!::!g;
227  
228 #Note: Automatically add the default package Models.
229 0 $comp_id = $model_dir_for_package . '::' . $comp_id;
230 } ## end if ( $comp_id !~ /::/ )
231  
232 0 my $contents = _gen_contents( $comp_id, @_ );
233  
234 0 my $ret = print $file $contents;
235  
236 0 ▶ if ( $ret == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ret == 1) { } 11 0 11
if ($ret == 1) { } 0 0 0
237 0 print "generated:$fpath\n";
238 0 return $fpath;
239 } else {
240 0 print "output fail:$fpath\n";
241 0 return undef;
242 }
243  
244 } ## end sub generate_model
245  
246 =item load_model()
247  
248 Loat the Model object from the passed model name and return it's model field.
249 Note that this mothod do not return the instance of the loaded model object itself.
250  
251 Functional style usage is like the following.
252  
253 my $model_href = Su::Model::load_model('SomeModel');
254  
255 OO Style usage is like the following.
256  
257 my $mdl = Su::Model->new;
258 $model_href = $mdl->load_model('Pkg/Mdl2');
259 $model_href = $mdl->load_model('Pkg::Mdl3');
260  
261 If you want to set some data to the model and access the data from the
262 model, then the sample code becomes as follwings:
263  
264 Su::Model::load_model('Pkg::SomeModel')->{value} = $value;
265  
266 my $value = Su::Model::load_model('Pkg::SomeModel')->{value};
267  
268 If you want to suppress dying because of module require error, then pass
269 the second parameter like the following.
270  
271 my $model = $mdl->load_model( 'Pkg::SomeModel', {suppress_error => 1} );
272  
273 When the second parameter is passed and load error occured, then this
274 method return undef.
275  
276 If you want to share and reuse model data, then pass the share parameter as
277 the second parameter.
278  
279 my $model = $mdl->load_model( 'Pkg::SomeModel', {share => 1} );
280  
281 =cut
282  
283 54 ▶sub load_model {
Subroutine load_model: called CC=13 SCAR=37.7
Pod documented
284 0 ▶ my $self = shift if ( ref $_[0] eq __PACKAGE__ );
Subroutine load_model: not called CC=13 SCAR=37.7
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if ref $_[0] eq __PACKAGE__ 48 6 54
if ref $_[0] eq __PACKAGE__ 0 0 0
285  
286 0 my $model_id = shift;
287 0 my $opt_href = shift;
288 0 ▶ my $MODEL_CACHE_HREF = $self ? $self->{models} : $MODEL_CACHE_HREF;
Branch: 2 branches
BranchTrueFalseTotal
$self ? : 52 2 54
$self ? : 0 0 0
289  
290 # Return the cacned data if cache exists.
291 return $MODEL_CACHE_HREF->{$model_id}
292 0 ▶ if ( $MODEL_CACHE_HREF->{$model_id} ) && $opt_href->{share};
Branch: 2 branches
BranchTrueFalseTotal
if $MODEL_CACHE_HREF->{$model_id} and $opt_href->{"share"} 2 52 54
if $MODEL_CACHE_HREF->{$model_id} and $opt_href->{"share"} 0 0 0
Condition50%3/6
$MODEL_CACHE_HREF->{$model_id} and $opt_href->{"share"}
!ll&&!rl&&r
5022
$MODEL_CACHE_HREF->{$model_id} and $opt_href->{"share"}
!ll&&!rl&&r
000
MC/DC100%2/2
$MODEL_CACHE_HREF->{$model_id} and $opt_href->{"share"}
$MODEL_CACHE_HREF->{$model_id} $opt_href->{"share"}
MC/DC0%0/2
$MODEL_CACHE_HREF->{$model_id} and $opt_href->{"share"}
$MODEL_CACHE_HREF->{$model_id} $opt_href->{"share"}
Truth table100%3/3
A and B
A= $MODEL_CACHE_HREF->{$model_id}B= $opt_href->{"share"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $MODEL_CACHE_HREF->{$model_id}B= $opt_href->{"share"}
ABresult
0X0
100
111
293  
294 # NOTE: No need this safe guard.
295 # $self = {} unless $self;
296 0 ▶ my $MODEL_BASE_DIR = $self->{base} ? $self->{base} : $MODEL_BASE_DIR;
Branch: 2 branches
BranchTrueFalseTotal
$self->{'base'} ? : 15 37 52
$self->{'base'} ? : 0 0 0
297 0 ▶ my $MODEL_DIR = $self->{dir} ? $self->{dir} : $MODEL_DIR;
Branch: 2 branches
BranchTrueFalseTotal
$self->{'dir'} ? : 47 5 52
$self->{'dir'} ? : 0 0 0
298  
299 0 my $model_path = $model_id;
300  
301 # Convert package separator to file path separator.
302 0 $model_path =~ s!::!/!g;
303 0 $model_path .= ".pm";
304  
305 # Trim the head of dot slash(./) of the file path.
306 0 $model_path =~ s!^\./(.+)!$1!;
307  
308 0 eval { require($model_path); };
309 0 ▶ if ($@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) 5 47 52
if ($@) 0 0 0
310 0 ▶ if ( $opt_href->{suppress_error} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opt_href->{'suppress_error'}) { } 4 1 5
if ($opt_href->{'suppress_error'}) { } 0 0 0
311 0 return undef;
312 } else {
313 0 die $@;
314 }
315 } ## end if ($@)
316  
317 # Recover separator to use as model package separator.
318 0 $model_id =~ s!/!::!g;
319 0 my $model_href;
320 0 ▶ if ( exists &{ ( $model_id . "::new" ) } ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists &{$model_id . '::new';}) { } 0 47 47
if (exists &{$model_id . '::new';}) { } 0 0 0
321 0 my $model_inst = $model_id->new;
322 0 $model_href = $model_inst->{model};
323 } else {
324 0 ▶ $model_href = $model_id->can('model') ? $model_id->model : undef;
Branch: 2 branches
BranchTrueFalseTotal
$model_id->can('model') ? : 47 0 47
$model_id->can('model') ? : 0 0 0
325 }
326  
327 0 ▶ die "Model has no model field:" . $model_path unless $model_href;
Branch: 2 branches
BranchTrueFalseTotal
unless $model_href 0 47 47
unless $model_href 0 0 0
328  
329 # Cache the model data.
330 0 $self->{models}->{$model_id} = $model_href;
331  
332 0 ▶ if ( $opt_href->{share} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opt_href->{'share'}) { } 0 47 47
if ($opt_href->{'share'}) { } 0 0 0
333 0 return $model_href;
334 } else {
335  
336 # To prevent destructive effect to this model data, we need to replicate the instance.
337 0 return dclone($model_href);
338 }
339 } ## end sub load_model
340  
341 =begin comment
342  
343 Return the contents of the new Model.
344  
345 =end comment
346  
347 =cut
348  
349 11 ▶sub _gen_contents {
Subroutine _gen_contents: called CC=2 SCAR=10.0
350 0 ▶ shift if ( ref $_[0] eq __PACKAGE__ );
Subroutine _gen_contents: not called CC=2 SCAR=10.0
Branch: 2 branches
BranchTrueFalseTotal
if ref $_[0] eq __PACKAGE__ 0 11 11
if ref $_[0] eq __PACKAGE__ 0 0 0
351  
352 0 my $comp_id = shift;
353 0 my %h = @_;
354  
355 0 my $ft = Su::Template->new;
356 0 my $ret = $ft->expand( <<'__TMPL__', $comp_id, \%h );
357 % my $comp_id = shift;
358 % my $href = shift;
359 package <%=$comp_id%>;
360 use strict;
361 use warnings;
362  
363 my $model=
364 {
365 % while(my ($k,$v) = each(%{$href})){
366 % if(ref $v eq 'HASH'){
367 <%=$k%> =>
368 {
369 % while(my ($kk,$vv) = each(%{$v})){
370 <%=$kk%> => "<%=$vv%>",
371 % }
372 },
373 % }else{
374 <%=$k%> => "<%=$v%>",
375 % }
376 % }
377 };
378  
379 sub model{
380 if($_[0] eq __PACKAGE__){
381 shift;
382 }
383 my $arg = shift;
384 if($arg){
385 $model = $arg;
386 }else{
387 return $model;
388 }
389  
390 }
391  
392 1;
393  
394 __TMPL__
395  
396 0 return $ret;
397  
398 } ## end sub _gen_contents
399  
400 =pod
401  
402 =back
403  
404 =cut
405  
406 #sub tmpl_test{
407 #
408 # my $ret = tmpl(<<'__HERE__');
409 #% my $tmp = "tmpval";
410 #one
411 #hoge<% foreach my $v ("a","b","c"){%>fuga
412 #looping
413 #<%= $v%>
414 #<%} %>
415 #<%= $tmp%>
416 #two
417 #three
418 #__HERE__
419 #
420 # return $ret;
421 #
422 #}
423  
424 15 1;
« Su/Build/CustomBuild.pm ↑ Summary Su/Template.pm »