Generator/Model.pm

filter: Statementstmt 29.7% 25 / 84 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 54.5% 6 / 11 Podpod 25.0% 1 / 4 totaltotal 22.7% 32 / 141 CC 16 SCAR 43.3
CC
16
Cov
22%
run
156.0
singular
20.0
write_file
6.0
CRAP 75.9
↑ Summary Generator.pm »
1 package Jorge::Generator::Model;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Jorge::Generator::Model - Jorge based Models generator. Runs with jorge-generate
9  
10 =head1 VERSION
11  
12 Version 0.02
13  
14 =cut
15  
16 1 our $VERSION = '0.03';
17  
18 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Carp qw( croak cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 =head1 FUNCTIONS
24  
25 =head2 run
26  
27 =cut
28  
29 1 my @types = qw(
30 bit
31 char
32 datetime
33 enum
34 int
35 text
36 timestamp
37 varchar
38 pk
39 );
40  
41 1 our $char_lenght = 255;
42 1 our $varchar_lenght = 255;
43 1 our $int_lenght = 11;
44  
45  
46 1 my %sql_types = (
47 bit => 'TINYINT(1) NOT NULL',
48 char => "CHAR($char_lenght) NOT NULL",
49 date => 'DATETIME NOT NULL',
50 enum => 'ENUM("") NOT NULL',
51 int => "INT($int_lenght) NOT NULL",
52 pk => "INT($int_lenght) NOT NULL AUTO_INCREMENT",
53 text => 'TEXT NOT NULL',
54 timestamp => 'TIMESTAMP NOT NULL default CURRENT_TIMESTAMP',
55 varchar => "VARCHAR($char_lenght) NOT NULL",
56 );
57  
58 0 ▶sub run {
Subroutine run: not called CC=12 SCAR=50.5
Pod documented
59 0 ▶ pod2usage() unless @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV 0 0 0
60  
61 0 my %config;
62 GetOptions(
63 'model=s' => \$config{_model},
64 'plural=s' => \$config{_plural},
65 'fields=s' => \$config{_fields},
66 0 ▶ help => sub { pod2usage(); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 0 ▶ ) or pod2usage();
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("model=s", \$config{"_model"}, "plural=s", \$config{"_plural"}, "fields=s", \$config{"_fields"}, "help", sub { pod2usage(); } ) or pod2usage() 0 0 0
68  
69 0 ▶ pod2usage() unless ( $config{_model} && $config{_fields} );
Branch: 1 branch
BranchTrueFalseTotal
unless $config{"_model"} and $config{"_fields"} 0 0 0
Condition0%0/3
$config{"_model"} and $config{"_fields"}
!ll&&!rl&&r
000
MC/DC0%0/2
$config{"_model"} and $config{"_fields"}
$config{"_model"} $config{"_fields"}
Truth table0%0/3
A and B
A= $config{"_model"}B= $config{"_fields"}
ABresult
0X0
100
111
70  
71 0 $config{_model} = ucfirst( $config{_model} );
72 0 ▶ $config{_plural} = ucfirst( $config{_plural} || $config{_model} . 's');
Condition0%0/2
$config{'_plural'} || $config{'_model'} . 's'
l!l
00
MC/DC0%0/1
$config{'_plural'} || $config{'_model'} . 's'
$config{'_plural'}
Truth table0%0/2
A
A= $config{'_plural'}
Aresult
11
00
73  
74 #now, parse the fields we received.
75 0 my @fields = split( /,/, $config{_fields} );
76 0 foreach my $f (@fields) {
77 0 my ( $field, $value ) = split( ':', $f );
78 0 ▶ croak "missing param or value" unless $field && $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $field and $value 0 0 0
Condition0%0/3
$field and $value
!ll&&!rl&&r
000
MC/DC0%0/2
$field and $value
$field $value
Truth table0%0/3
A and B
A= $fieldB= $value
ABresult
0X0
100
111
79
80 0 my $its_a_class = $value =~ m/[A-Z](.*)/;
81 croak "invalid data type \'$value\' on $field"
82 0 ▶ if !( grep { $_ eq $value } @types or $its_a_class );
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $value;} @types or $its_a_class 0 0 0
Condition0%0/3
grep {$_ eq $value;} @types or $its_a_class
l!l&&r!l&&!r
000
MC/DC0%0/2
grep {$_ eq $value;} @types or $its_a_class
grep {$_ eq $value;} @types $its_a_class
Truth table0%0/3
A or B
A= grep {$_ eq $value;} @typesB= $its_a_class
ABresult
1X1
011
000
83  
84 0 $value = ucfirst $value;
85 0 $field = ucfirst $field;
86  
87 0 ▶ if ($its_a_class){
Branch: 1 branch
BranchTrueFalseTotal
if ($its_a_class) 0 0 0
88 0 push( @{ $config{_classes} }, $value );
89 }
90
91 0 $config{$field} = $value;
92 0 push( @{ $config{_order} }, $field );
93 }
94 0 write_file($config{_model}, singular(%config));
95 0 write_file($config{_plural}, plural(%config));
96  
97 }
98  
99  
100 0 ▶sub singular {
Subroutine singular: not called CC=4 SCAR=30.0
Pod undocumented
101 0 my %config = @_;
102 0 ▶ my @use = @{$config{_classes}} if defined $config{_classes};
Branch: 1 branch
BranchTrueFalseTotal
if defined $config{"_classes"} 0 0 0
103 0 my @fields_raw = @{$config{_order}};
104 0 my $use_line = join( "", map { "use $_;\n" } @use );
105 0 my @fields = grep { $config{$_} =~ m/[a-z]+/ } @fields_raw;
106
107 0 my $fields_list = join( "\n ", map { $_ } @fields );
108 #note the spacing. must match the number of spaces in the $tmpl var in
109 # order to properly allign the fields
110 0 my @pks = grep { $config{$_} eq 'Pk' } @fields;
111 0 my $pks_line = join("\n ", map { "$_ => { pk => 1, read_only => 1 }," } @pks);
112 0 ▶ my @datetimes = grep { $config{$_} eq 'Timestamp' || $config{$_} eq 'Datetime' } @fields;
Condition0%0/3
$config{$_} eq 'Timestamp' || $config{$_} eq 'Datetime'
l!l&&r!l&&!r
000
MC/DC0%0/2
$config{$_} eq 'Timestamp' || $config{$_} eq 'Datetime'
$config{$_} eq 'Timestamp' $config{$_} eq 'Datetime'
Truth table0%0/3
A || B
A= $config{$_} eq 'Timestamp'B= $config{$_} eq 'Datetime'
ABresult
1X1
011
000
113 0 my $datetime_line = join("\n ", map { "$_ => { datetime => 1 }," } @datetimes);
114 0 my $classes_line = join("\n ", map { "$_ => { class => new $_() }," } @use);
115
116 #sql code
117 0 my $sql_pks = join(",",@pks);
118 0 ▶ my $sql_fields = join("\n ", map { "`$_` ". ( $sql_types{lc($config{$_})} || 'INT('.$int_lenght.') NOT NULL ') ." ," } @fields);
Condition0%0/2
$sql_types{lc $config{$_}} || 'INT(' . $Jorge::Generator::Model::int_lenght . ') NOT NULL '
l!l
00
MC/DC0%0/1
$sql_types{lc $config{$_}} || 'INT(' . $Jorge::Generator::Model::int_lenght . ') NOT NULL '
$sql_types{lc $config{$_}}
Truth table0%0/2
A
A= $sql_types{lc $config{$_}}
Aresult
11
00
119
120
121 0 my $tmpl = <<END;
122 package $config{_model};
123 use base 'Jorge::DBEntity';
124 use Jorge::Plugin::Md5;
125  
126 #Insert any dependencies here.
127 #Example:
128 $use_line
129  
130 use strict;
131  
132 sub _fields {
133
134 my \$table_name = '$config{_model}';
135
136 my \@fields = qw(
137 $fields_list
138 );
139  
140 my \%fields = (
141 $pks_line
142 $datetime_line
143 $classes_line
144 );
145  
146 return [ \\\@fields, \\\%fields, \$table_name ];
147 }
148  
149 1;
150 __END__
↑ Summary Generator.pm »