File Coverage

blib/lib/DBIx/Schema/Annotate.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package DBIx::Schema::Annotate;
2 2     2   16816 use 5.008001;
  2         6  
  2         67  
3 2     2   7 use strict;
  2         4  
  2         67  
4 2     2   8 use warnings;
  2         5  
  2         50  
5 2     2   909 use DBIx::Inspector;
  2         17170  
  2         58  
6 2     2   1395 use Smart::Args;
  0            
  0            
7             use IO::All;
8             use Module::Load ();
9              
10             our $VERSION = "0.04";
11              
12             our $BLOCK_LINE = '## == Schema Info ==';
13              
14             sub new {
15             args(
16             my $class => 'ClassName',
17             my $dbh => 'DBI::db',
18             );
19              
20             bless {
21             dbh => $dbh,
22             driver => '',
23             tables => '',
24             }, $class;
25             }
26              
27             sub driver {
28             my $self = shift;
29             $self->{driver} ||= do {
30             my $driver_class = sprintf('%s::Driver::%s', __PACKAGE__, $self->{dbh}->{Driver}->{Name});
31             Module::Load::load($driver_class);
32             $driver_class->new(dbh => $self->{dbh});
33             };
34             }
35              
36             sub tables {
37             my $self = shift;
38             $self->{tables} ||= do {
39             my $inspector = DBIx::Inspector->new(dbh => $self->{dbh});
40             my @list;
41             for my $info ($inspector->tables) {
42             push @list, $info->name;
43             }
44             \@list;
45             };
46             }
47              
48             sub get_table_ddl {
49             args(
50             my $self,
51             my $table_name => 'Str',
52             );
53             return $self->driver->table_ddl(table_name => $table_name);
54             }
55              
56             sub clean {
57             args(
58             my $self,
59             my $dir => 'Str',
60             );
61              
62             for my $table_name (@{$self->tables}) {
63             my $f_path = io->catfile($dir, _camelize($table_name).'.pm');
64             next unless ( -e $f_path);
65              
66             my $io = io($f_path);
67             $io->print(do{
68             my $content = $io->all;
69             $content =~ s/^$BLOCK_LINE.+$BLOCK_LINE\n\n//gms;
70             $content;
71             });
72             }
73              
74             }
75              
76             sub write_files {
77             args(
78             my $self,
79             my $dir => 'Str',
80             );
81              
82             for my $table_name (@{$self->tables}) {
83             my $io = io->catfile($dir, _camelize($table_name).'.pm');
84             next unless ( -e $io->pathname);
85              
86             $io->print(do{
87             my $content = $io->all;
88             #clean
89             $content =~ s/^$BLOCK_LINE.+$BLOCK_LINE\n\n//gms;
90             my $ddl = $self->get_table_ddl(table_name => $table_name);
91              
92             my $annotate = join(
93             "\n" =>
94             $BLOCK_LINE,
95             (map { '# '.$_} split('\n', $ddl)),
96             $BLOCK_LINE
97             );
98              
99             sprintf("%s\n\n%s",$annotate, $content);
100             });
101             }
102             }
103              
104             sub _camelize {
105             my $s = shift;
106             join('', map{ ucfirst $_ } split(/(?<=[A-Za-z])_(?=[A-Za-z])|\b/, $s));
107             }
108              
109              
110             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