File Coverage

blib/lib/DBIx/Schema/Annotate.pm
Criterion Covered Total %
statement 27 73 36.9
branch 0 8 0.0
condition 0 6 0.0
subroutine 9 16 56.2
pod 2 6 33.3
total 38 109 34.8


line stmt bran cond sub pod time code
1             package DBIx::Schema::Annotate;
2 2     2   28410 use 5.008001;
  2         9  
  2         104  
3 2     2   14 use strict;
  2         3  
  2         100  
4 2     2   12 use warnings;
  2         8  
  2         85  
5 2     2   1697 use utf8;
  2         26  
  2         14  
6 2     2   1547 use Encode;
  2         25808  
  2         247  
7 2     2   1361 use DBIx::Inspector;
  2         36017  
  2         81  
8 2     2   1236 use Smart::Args;
  2         48104  
  2         213  
9 2     2   1524 use IO::All;
  2         29720  
  2         23  
10 2     2   170 use Module::Load ();
  2         5  
  2         1949  
11              
12             our $VERSION = "0.06";
13              
14             our $BLOCK_LINE = '## == Schema Info ==';
15              
16             sub new {
17 0     0 1   args(
18             my $class => 'ClassName',
19             my $dbh => 'DBI::db',
20             );
21              
22 0           bless {
23             dbh => $dbh,
24             driver => '',
25             tables => '',
26             }, $class;
27             }
28              
29             sub driver {
30 0     0 0   my $self = shift;
31 0   0       $self->{driver} ||= do {
32 0           my $driver_class = sprintf('%s::Driver::%s', __PACKAGE__, $self->{dbh}->{Driver}->{Name});
33 0           Module::Load::load($driver_class);
34 0           $driver_class->new(dbh => $self->{dbh});
35             };
36             }
37              
38             sub tables {
39 0     0 0   my $self = shift;
40 0   0       $self->{tables} ||= do {
41 0           my $inspector = DBIx::Inspector->new(dbh => $self->{dbh});
42 0           my @list;
43 0           for my $info ($inspector->tables) {
44 0           push @list, $info->name;
45             }
46 0           \@list;
47             };
48             }
49              
50             sub get_table_ddl {
51 0     0 0   args(
52             my $self,
53             my $table_name => 'Str',
54             );
55 0           return $self->driver->table_ddl(table_name => $table_name);
56             }
57              
58             sub clean {
59 0     0 0   args(
60             my $self,
61             my $dir => 'Str',
62             );
63              
64 0           for my $table_name (@{$self->tables}) {
  0            
65 0           my $f_path = io->catfile($dir, _camelize($table_name).'.pm');
66 0 0         next unless ( -e $f_path);
67              
68 0           my $io = io($f_path);
69 0           $io->print(do{
70 0           my $content = $io->all;
71 0           $content =~ s/^$BLOCK_LINE\n.+$BLOCK_LINE\n\n//gms;
72 0           $content;
73             });
74             }
75              
76             }
77              
78             sub write_files {
79 0     0 1   args(
80             my $self,
81             my $dir => 'Str',
82             );
83              
84 0           TABLE:
85 0           for my $table_name (@{$self->tables}) {
86 0           my $io = io->catfile($dir, _camelize($table_name).'.pm');
87 0 0         next TABLE unless ( -e $io->pathname);
88              
89 0           $io->print(do{
90 0           my $content = $io->all;
91 0           my $ddl = $self->get_table_ddl(table_name => $table_name);
92 0           $ddl = encode_utf8($ddl);
93              
94 0 0         if ($content =~ m/^$BLOCK_LINE\n(.+)\n$BLOCK_LINE\n\n/ms) {
95 0           my $ddl_in_file = $1;
96 0           $ddl_in_file =~ s/^# //gms;
97 0 0         next TABLE if $ddl_in_file eq $ddl;
98             }
99              
100             #clean
101 0           $content =~ s/^$BLOCK_LINE\n.+$BLOCK_LINE\n\n//gms;
102 0           my $annotate = join(
103             "\n" =>
104             $BLOCK_LINE,
105 0           (map { '# '.$_} split('\n', $ddl)),
106             $BLOCK_LINE
107             );
108              
109 0           sprintf("%s\n\n%s",$annotate, $content);
110             });
111             }
112             }
113              
114             sub _camelize {
115 0     0     my $s = shift;
116 0           join('', map{ ucfirst $_ } split(/(?<=[A-Za-z])_(?=[A-Za-z])|\b/, $s));
  0            
117             }
118              
119              
120             1;
121              
122             __END__
123              
124             =encoding utf-8
125              
126             =head1 NAME
127              
128             DBIx::Schema::Annotate - Add table schema as comment to your ORM file. This module is inspired by annotate_models.
129              
130              
131             =head1 SYNOPSIS
132              
133             use DBIx::Schema::Annotate;
134              
135             my $dbh = DBI->connect('....') or die $DBI::errstr;
136             my $annotate = DBIx::Schema::Annotate->new( dbh => $dbh );
137             $annotate->write_files(
138             dir => '...',
139             exception_rule => {
140             # todo
141             }
142             );
143              
144             # Amon2 + Teng
145             $ 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/!)'
146              
147             =head1 DESCRIPTION
148              
149             Schema is added to pm file of specified path follower of the same camelize name as table.
150              
151             For example 'post' table and 'post_comment' table exist, and we assume that $self->write_files(dir => $dir) was carried out.
152             The targets to which DBIx::Schema::Annotate adds a annotate are $dir/Post.pm and $dir/PostComment.pm.
153              
154             This module is supporting MySQL and SQLite.
155              
156             =head1 METHODS
157              
158             =head2 new( dbh => $dbh )
159              
160             Constructor.
161              
162             =head2 write_files( dir => 'path/to/...' )
163              
164             Schema is added to pm file of 'path/to/...' follower of the same camelize name as table.
165              
166             =head1 LICENSE
167              
168             Copyright (C) tokubass.
169              
170             This library is free software; you can redistribute it and/or modify
171             it under the same terms as Perl itself.
172              
173             =head1 AUTHOR
174              
175             tokubass E<lt>tokubass@cpan.orgE<gt>
176              
177             =cut
178