File Coverage

blib/lib/DBIx/Schema/Annotate.pm
Criterion Covered Total %
statement 21 66 31.8
branch 0 8 0.0
condition 0 6 0.0
subroutine 7 14 50.0
pod 2 6 33.3
total 30 100 30.0


line stmt bran cond sub pod time code
1             package DBIx::Schema::Annotate;
2 2     2   22415 use 5.008001;
  2         5  
  2         59  
3 2     2   8 use strict;
  2         3  
  2         78  
4 2     2   10 use warnings;
  2         6  
  2         66  
5 2     2   933 use DBIx::Inspector;
  2         18040  
  2         55  
6 2     2   866 use Smart::Args;
  2         34452  
  2         276  
7 2     2   1577 use IO::All;
  2         22378  
  2         17  
8 2     2   132 use Module::Load ();
  2         3  
  2         1084  
9              
10             our $VERSION = "0.05";
11              
12             our $BLOCK_LINE = '## == Schema Info ==';
13              
14             sub new {
15 0     0 1   args(
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     0 0   my $self = shift;
29 0   0       $self->{driver} ||= do {
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     0 0   my $self = shift;
38 0   0       $self->{tables} ||= do {
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     0 0   args(
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     0 0   args(
58             my $self,
59             my $dir => 'Str',
60             );
61              
62 0           for my $table_name (@{$self->tables}) {
  0            
63 0           my $f_path = io->catfile($dir, _camelize($table_name).'.pm');
64 0 0         next unless ( -e $f_path);
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     0 1   args(
78             my $self,
79             my $dir => 'Str',
80             );
81              
82 0           TABLE:
83 0           for my $table_name (@{$self->tables}) {
84 0           my $io = io->catfile($dir, _camelize($table_name).'.pm');
85 0 0         next TABLE unless ( -e $io->pathname);
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 0         if ($content =~ m/^$BLOCK_LINE\n(.+)\n$BLOCK_LINE\n\n/ms) {
92 0           my $ddl_in_file = $1;
93 0           $ddl_in_file =~ s/^# //gms;
94 0 0         next TABLE if $ddl_in_file eq $ddl;
95             }
96              
97             #clean
98 0           $content =~ s/^$BLOCK_LINE\n.+$BLOCK_LINE\n\n//gms;
99 0           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     0     my $s = shift;
113 0           join('', map{ ucfirst $_ } split(/(?<=[A-Za-z])_(?=[A-Za-z])|\b/, $s));
  0            
114             }
115              
116              
117             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