File Coverage

blib/lib/Ado/Model.pm
Criterion Covered Total %
statement 52 53 96.2
branch 3 6 50.0
condition 4 10 40.0
subroutine 16 17 94.1
pod 4 4 100.0
total 79 90 86.6


line stmt bran cond sub pod time code
1             package Ado::Model; #The schema/base class
2 24     24   791690 use 5.010001;
  24         100  
3 24     24   135 use strict;
  24         54  
  24         546  
4 24     24   122 use warnings;
  24         51  
  24         669  
5 24     24   121 use utf8;
  24         48  
  24         272  
6 24     24   697 use parent qw(DBIx::Simple::Class);
  24         50  
  24         174  
7 24     24   48405 use Carp;
  24         50  
  24         1486  
8 24     24   8062 use DBIx::Simple::Class::Schema;
  24         51144  
  24         6864  
9              
10             our $VERSION = '0.01';
11 0     0 1 0 sub is_base_class { return 1 }
12              
13             sub dbix {
14              
15             # Singleton DBIx::Simple instance
16 437     437 1 93523 state $DBIx;
17              
18             # uncoverable statement
19 437   33     2382 return ($_[1] ? ($DBIx = $_[1]) : $DBIx)
20             || Carp::croak('DBIx::Simple is not instantiated. Please first do '
21             . $_[0]
22             . '->dbix(DBIx::Simple->connect($DSN,$u,$p,{...})');
23             }
24              
25              
26             #The methods below are not generated but written additionally
27              
28             sub select_range {
29 4     4 1 10 my $class = shift;
30 4         11 state $dbix = $class->dbix;
31 4         19 my $SQL = $class->SQL('SELECT') . $class->SQL_LIMIT(@_);
32 4         91 return $dbix->query($SQL)->objects($class);
33             }
34              
35             # Generates classes from tables on the fly and returns the classname.
36             sub table_to_class {
37 2     2 1 24 my ($class, $args) = shift->_get_obj_args(@_);
38 2         21 state $tables = {};
39 2         4 my $table = $args->{table};
40              
41             # already generated?
42 2 50       10 return $tables->{$table} if (exists $tables->{$table});
43              
44             # uncoverable branch false
45 2   33     6 $args->{namespace} //= $class;
46 2         10 my $class_name = $args->{namespace} . '::' . Mojo::Util::camelize($table);
47              
48             # loaded from file?
49             return $tables->{$table} = $class_name
50 2 50       33 if $INC{Mojo::Util::class_to_path($class_name)};
51 2   50     28 state $connected = DBIx::Simple::Class::Schema->dbix($class->dbix) && 1;
52             my $perl_code = DBIx::Simple::Class::Schema->load_schema(
53             namespace => $args->{namespace},
54             table => $table,
55 2   50     28 type => $args->{type} || "'TABLE','VIEW'",
56             );
57 2 50   34   13720 Carp::croak($@) unless (eval "{$perl_code}"); ## no critic (ProhibitStringyEval)
  34     10   1335  
  10     12   361  
  12     9   21309  
  9     13   136  
  13     2   358  
  4         78  
  2         3  
  2         44  
  2         8  
  2         4  
  2         24  
  2         82  
  2         3  
  2         14  
58              
59 2         7 $tables->{$table} = $class_name;
60 2         8 return $tables->{$table};
61             }
62              
63              
64             1;
65              
66             __END__