File Coverage

blib/lib/Karas/Dumper.pm
Criterion Covered Total %
statement 40 43 93.0
branch 1 2 50.0
condition 4 11 36.3
subroutine 7 8 87.5
pod 0 2 0.0
total 52 66 78.7


line stmt bran cond sub pod time code
1             package Karas::Dumper;
2 1     1   720 use strict;
  1         2  
  1         32  
3 1     1   6 use warnings;
  1         2  
  1         24  
4 1     1   5 use utf8;
  1         3  
  1         5  
5              
6 1     1   801 use DBIx::Inspector;
  1         8677  
  1         31  
7 1     1   2558 use String::CamelCase ();
  1         510  
  1         19  
8 1     1   1017 use Data::Dumper ();
  1         9199  
  1         600  
9              
10             sub new {
11 0     0 0 0 my $class = shift;
12 0         0 bless {}, $class;
13             }
14              
15             sub dump {
16 1     1 0 30 my $class = shift;
17 1 50       8 my %args = @_ == 1 ? %{$_[0]} : @_;
  0         0  
18 1   33     7 my $dbh = $args{dbh} // Carp::croak "Missing mandatory parameter: dbh";
19 1   33     6 my $namespace = $args{namespace} // Carp::croak "Missing mandatory parameter: namespace";
20 1   50     8 my $name_map = $args{name_map} || +{};
21 1         12 my $inspector = DBIx::Inspector->new(dbh => $dbh);
22 1         3838 my @lines = (
23             'use warnings;',
24             'use strict;',
25             '',
26             "package ${namespace}::Schema;",
27             '# This file is automatically generated by ' . __PACKAGE__ . '. Do not edit directly.',
28             ''
29             );
30 1         2 my %class_map;
31 1         9 for my $table ($inspector->tables) {
32 3   33     852 my $klass = sprintf("%s::Row::%s", $namespace, $name_map->{$table->name} || String::CamelCase::camelize($table->name));
33 3         98 $class_map{$table->name} = $klass;
34 7         5899 push @lines, (
35             sprintf("package $klass;"),
36             sprintf('# This file is automatically generated by ' . __PACKAGE__ . '. Do not edit directly.'),
37             sprintf("use parent qw(Karas::Row);"),
38 4         3054 sprintf("__PACKAGE__->mk_column_accessors(qw(%s));", join(' ', map { $_->name } $table->columns)),
39             sprintf("sub table_name { '%s' }", $table->name),
40 7         2943 sprintf("sub primary_key { qw(%s) }", join(' ', map { $_->name } $table->primary_key())),
41 3         28 sprintf("sub column_names { qw(%s) }", join(' ', map { $_->name } $table->columns())),
42             '',
43             );
44             }
45             push @lines, (
46 1         34 sprintf("sub ${namespace}::Schema::table_name2class { %s }", do {
47 1         3 local $Data::Dumper::Terse = 1;
48 1         3 local $Data::Dumper::Indent = 1;
49 1         3 local $Data::Dumper::SortKeys = 1;
50 1         6 Data::Dumper::Dumper(\%class_map)
51             })
52             );
53 1         117 push @lines, "1;";
54 1         39 return join("\n", @lines);
55             }
56              
57             1;
58