blib/lib/DBIx/Schema/Annotate.pm

filter: Statementstmt 33.8% 23 / 68 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 50.0% 7 / 14 Podpod 33.3% 2 / 6 totaltotal 30.1% 32 / 106 SCAR 38.6
CC
10
Cov
28%
write_files
30.0
tables
12.0
clean
12.0
CRAP 47.2
↑ Summary blib/lib/DBIx/Schema/Annotate/Driver/SQLite.pm »
1 package DBIx::Schema::Annotate;
2 2 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use DBIx::Inspector;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Smart::Args;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use IO::All;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Module::Load ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $VERSION = "0.05";
11  
12 2 our $BLOCK_LINE = '## == Schema Info ==';
13  
14 sub new {
15 0 ▶ args(
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
16 my $class => 'ClassName',
17 my $dbh => 'DBI::db',
18 );
19  
20 0 bless {
21 dbh => $dbh,
22 driver => '',
23 tables => '',
24 }, $class;
25 }
26  
27 sub driver {
28 0 ▶ my $self = shift;
Subroutine driver: not called CC=2 SCAR=17.9
Pod undocumented
29 0 ▶ $self->{driver} ||= do {
Condition0%0/3
$self->{"driver"} ||= do {
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"driver"} ||= do { my $driver_class = sprintf('%s::Driver::%s', __PACKAGE__, $self->{"dbh"}{"Driver"}{"Name"}); &Module::Load::load($driver_class); $driver_class->new("dbh", $self->{"dbh"}) }
$self->{"driver"} do { my $driver_class = sprintf('%s::Driver::%s', __PACKAGE__, $self->{"dbh"}{"Driver"}{"Name"}); &Module::Load::load($driver_class); $driver_class->new("dbh", $self->{"dbh"}) }
Truth table0%0/3
A ||= B
A= $self->{"driver"}B= do { my $driver_class = sprintf('%s::Driver::%s', __PACKAGE__, $self->{"dbh"}{"Driver"}{"Name"}); &Module::Load::load($driver_class); $driver_class->new("dbh", $self->{"dbh"}) }
ABresult
1X1
011
000
30 0 my $driver_class = sprintf('%s::Driver::%s', __PACKAGE__, $self->{dbh}->{Driver}->{Name});
31 0 Module::Load::load($driver_class);
32 0 $driver_class->new(dbh => $self->{dbh});
33 };
34 }
35  
36 sub tables {
37 0 ▶ my $self = shift;
Subroutine tables: not called CC=3 SCAR=24.8
Pod undocumented
38 0 ▶ $self->{tables} ||= do {
Condition0%0/3
$self->{"tables"} ||= do {
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"tables"} ||= do { my $inspector = "DBIx::Inspector"->new("dbh", $self->{"dbh"}); my @list; foreach my $info ($inspector->tables) { push @list, $info->name; } ; \@list }
$self->{"tables"} do { my $inspector = "DBIx::Inspector"->new("dbh", $self->{"dbh"}); my @list; foreach my $info ($inspector->tables) { push @list, $info->name; } ; \@list }
Truth table0%0/3
A ||= B
A= $self->{"tables"}B= do { my $inspector = "DBIx::Inspector"->new("dbh", $self->{"dbh"}); my @list; foreach my $info ($inspector->tables) { push @list, $info->name; } ; \@list }
ABresult
1X1
011
000
39 0 my $inspector = DBIx::Inspector->new(dbh => $self->{dbh});
40 0 my @list;
41 0 for my $info ($inspector->tables) {
42 0 push @list, $info->name;
43 }
44 0 \@list;
45 };
46 }
47  
48 sub get_table_ddl {
49 0 ▶ args(
Subroutine get_table_ddl: not called CC=1 SCAR=6.9
Pod undocumented
50 my $self,
51 my $table_name => 'Str',
52 );
53 0 return $self->driver->table_ddl(table_name => $table_name);
54 }
55  
56 sub clean {
57 0 ▶ args(
Subroutine clean: not called CC=3 SCAR=24.8
Pod undocumented
58 my $self,
59 my $dir => 'Str',
60 );
61  
62 0 for my $table_name (@{$self->tables}) {
63 0 my $f_path = io->catfile($dir, _camelize($table_name).'.pm');
64 0 ▶ next unless ( -e $f_path);
Branch: 1 branch
BranchTrueFalseTotal
unless -e $f_path 0 0 0
65  
66 0 my $io = io($f_path);
67 0 $io->print(do{
68 0 my $content = $io->all;
69 0 $content =~ s/^$BLOCK_LINE\n.+$BLOCK_LINE\n\n//gms;
70 0 $content;
71 });
72 }
73  
74 }
75  
76 sub write_files {
77 0 ▶ args(
Subroutine write_files: not called CC=5 SCAR=34.0
Pod documented
78 my $self,
79 my $dir => 'Str',
80 );
81  
82 TABLE:
83 0 for my $table_name (@{$self->tables}) {
84 0 my $io = io->catfile($dir, _camelize($table_name).'.pm');
85 0 ▶ next TABLE unless ( -e $io->pathname);
Branch: 1 branch
BranchTrueFalseTotal
unless -e $io->pathname 0 0 0
86  
87 0 $io->print(do{
88 0 my $content = $io->all;
89 0 my $ddl = $self->get_table_ddl(table_name => $table_name);
90  
91 0 ▶ if ($content =~ m/^$BLOCK_LINE\n(.+)\n$BLOCK_LINE\n\n/ms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /^$BLOCK_LINE\n(.+)\n$BLOCK_LINE\n\n/ms) 0 0 0
92 0 my $ddl_in_file = $1;
93 0 $ddl_in_file =~ s/^# //gms;
94 0 ▶ next TABLE if $ddl_in_file eq $ddl;
Branch: 1 branch
BranchTrueFalseTotal
if $ddl_in_file eq $ddl 0 0 0
95 }
96  
97 #clean
98 0 $content =~ s/^$BLOCK_LINE\n.+$BLOCK_LINE\n\n//gms;
99 my $annotate = join(
100 "\n" =>
101 $BLOCK_LINE,
102 0 (map { '# '.$_} split('\n', $ddl)),
103 $BLOCK_LINE
104 );
105  
106 0 sprintf("%s\n\n%s",$annotate, $content);
107 });
108 }
109 }
110  
111 sub _camelize {
112 0 ▶ my $s = shift;
Subroutine _camelize: not called CC=1 SCAR=6.9
113 0 join('', map{ ucfirst $_ } split(/(?<=[A-Za-z])_(?=[A-Za-z])|\b/, $s));
114 }
115  
116  
117 2 1;
118  
119 __END__
120  
121 =encoding utf-8
122  
123 =head1 NAME
124  
125 DBIx::Schema::Annotate - Add table schema as comment to your ORM file. This module is inspired by annotate_models.
126  
127  
128 =head1 SYNOPSIS
129  
130 use DBIx::Schema::Annotate;
131  
132 my $dbh = DBI->connect('....') or die $DBI::errstr;
133 my $annotate = DBIx::Schema::Annotate->new( dbh => $dbh );
134 $annotate->write_files(
135 dir => '...',
136 exception_rule => {
137 # todo
138 }
139 );
140  
141 # Amon2 + Teng
142 $ carton exec -- perl -Ilib -MMyApp -MDBIx::Schema::Annotate -e 'my $c = MyApp->bootstrap; DBIx::Schema::Annotate->new( dbh => $c->db->{dbh})->write_files(dir => q!lib/MyApp/DB/Row/!)'
143  
144 =head1 DESCRIPTION
145  
146 Schema is added to pm file of specified path follower of the same camelize name as table.
147  
148 For example 'post' table and 'post_comment' table exist, and we assume that $self->write_files(dir => $dir) was carried out.
149 The targets to which DBIx::Schema::Annotate adds a annotate are $dir/Post.pm and $dir/PostComment.pm.
150  
151 This module is supporting MySQL and SQLite.
152  
153 =head1 METHODS
154  
155 =head2 new( dbh => $dbh )
156  
157 Constructor.
158  
159 =head2 write_files( dir => 'path/to/...' )
160  
161 Schema is added to pm file of 'path/to/...' follower of the same camelize name as table.
162  
163 =head1 LICENSE
164  
165 Copyright (C) tokubass.
166  
167 This library is free software; you can redistribute it and/or modify
168 it under the same terms as Perl itself.
169  
170 =head1 AUTHOR
171  
172 tokubass E<lt>tokubass@cpan.orgE<gt>
173  
174 =cut
175  
↑ Summary blib/lib/DBIx/Schema/Annotate/Driver/SQLite.pm »