blib/lib/DBIx/Schema/Annotate.pm

filter: Statementstmt 35.9% 23 / 64 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 50.0% 7 / 14 Podpod 33.3% 2 / 6 totaltotal 32.6% 32 / 98 SCAR 33.7
CC
8
Cov
31%
tables
12.0
clean
12.0
write_files
12.0
CRAP 29.0
↑ 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.04";
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.+$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=3 SCAR=24.8
Pod documented
78 my $self,
79 my $dir => 'Str',
80 );
81  
82 0 for my $table_name (@{$self->tables}) {
83 0 my $io = io->catfile($dir, _camelize($table_name).'.pm');
84 0 ▶ next unless ( -e $io->pathname);
Branch: 1 branch
BranchTrueFalseTotal
unless -e $io->pathname 0 0 0
85  
86 0 $io->print(do{
87 0 my $content = $io->all;
88 #clean
89 0 $content =~ s/^$BLOCK_LINE.+$BLOCK_LINE\n\n//gms;
90 0 my $ddl = $self->get_table_ddl(table_name => $table_name);
91  
92 my $annotate = join(
93 "\n" =>
94 $BLOCK_LINE,
95 0 (map { '# '.$_} split('\n', $ddl)),
96 $BLOCK_LINE
97 );
98  
99 0 sprintf("%s\n\n%s",$annotate, $content);
100 });
101 }
102 }
103  
104 sub _camelize {
105 0 ▶ my $s = shift;
Subroutine _camelize: not called CC=1 SCAR=6.9
106 0 join('', map{ ucfirst $_ } split(/(?<=[A-Za-z])_(?=[A-Za-z])|\b/, $s));
107 }
108  
109  
110 2 1;
111  
112 __END__
113  
114 =encoding utf-8
115  
116 =head1 NAME
117  
118 DBIx::Schema::Annotate - Add table schema as comment to your ORM file. This module is inspired by annotate_models.
119  
120  
121 =head1 SYNOPSIS
122  
123 use DBIx::Schema::Annotate;
124  
125 my $dbh = DBI->connect('....') or die $DBI::errstr;
126 my $annotate = DBIx::Schema::Annotate->new( dbh => $dbh );
127 $annotate->write_files(
128 dir => '...',
129 exception_rule => {
130 # todo
131 }
132 );
133  
134 # Amon2 + Teng
135 $ 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/!)'
136  
137 =head1 DESCRIPTION
138  
139 Schema is added to pm file of specified path follower of the same camelize name as table.
140  
141 For example 'post' table and 'post_comment' table exist, and we assume that $self->write_files(dir => $dir) was carried out.
142 The targets to which DBIx::Schema::Annotate adds a annotate are $dir/Post.pm and $dir/PostComment.pm.
143  
144 This module is supporting MySQL and SQLite.
145  
146 =head1 METHODS
147  
148 =head2 new( dbh => $dbh )
149  
150 Constructor.
151  
152 =head2 write_files( dir => 'path/to/...' )
153  
154 Schema is added to pm file of 'path/to/...' follower of the same camelize name as table.
155  
156 =head1 LICENSE
157  
158 Copyright (C) tokubass.
159  
160 This library is free software; you can redistribute it and/or modify
161 it under the same terms as Perl itself.
162  
163 =head1 AUTHOR
164  
165 tokubass E<lt>tokubass@cpan.orgE<gt>
166  
167 =cut
168  
↑ Summary blib/lib/DBIx/Schema/Annotate/Driver/SQLite.pm »