File Coverage

blib/lib/DBIx/Lite/Schema.pm
Criterion Covered Total %
statement 39 42 92.8
branch 8 14 57.1
condition 4 9 44.4
subroutine 7 7 100.0
pod 3 3 100.0
total 61 75 81.3


line stmt bran cond sub pod time code
1             package DBIx::Lite::Schema;
2             $DBIx::Lite::Schema::VERSION = '0.36';
3 6     6   219866 use strict;
  6         13  
  6         236  
4 6     6   30 use warnings;
  6         13  
  6         401  
5              
6 6     6   40 use Carp qw(croak);
  6         15  
  6         422  
7 6     6   4504 use DBIx::Lite::Schema::Table;
  6         41  
  6         3461  
8             $Carp::Internal{$_}++ for __PACKAGE__;
9              
10             sub new {
11 4     4 1 11 my $class = shift;
12 4         12 my (%params) = @_;
13            
14 4         17 my $self = {
15             tables => {},
16             };
17            
18 4 50       22 if (my $tables = delete $params{tables}) {
19 0         0 foreach my $table_name (keys %$tables) {
20 0         0 $tables->{$table_name}{name} = $table_name;
21 0         0 $self->{tables}{$table_name} = DBIx::Lite::Schema::Table->new($tables->{$table_name});
22             }
23             }
24            
25 4 50       13 !%params
26             or croak "Unknown options: " . join(', ', keys %params);
27            
28 4         10 bless $self, $class;
29 4         34 $self;
30             }
31              
32             sub table {
33 38     38 1 71 my $self = shift;
34 38         109 my $table_name = shift;
35 38   66     185 $self->{tables}{$table_name} ||= DBIx::Lite::Schema::Table->new(name => $table_name);
36 38         119 return $self->{tables}{$table_name};
37             }
38              
39             sub one_to_many {
40 2     2 1 5 my $self = shift;
41 2         8 my ($from, $to, $accessors) = @_;
42            
43 2 50 33     25 $from && $from =~ /^(.+)\.(.+)$/
44             or croak "Relationship keys must be defined in table.column format";
45 2         8 my $from_table = $self->table($1);
46 2         5 my $from_key = $2;
47            
48 2 50 33     16 $to && $to =~ /^(.+)\.(.+)$/
49             or croak "Relationship keys must be defined in table.column format";
50 2         7 my $to_table = $self->table($1);
51 2         6 my $to_key = $2;
52            
53 2         4 my $from_table_accessor = $to_table->{name};
54 2         4 my $to_table_accessor;
55 2 50       7 if ($accessors) {
56 2 100       8 if (ref $accessors eq 'ARRAY') {
57 1         4 ($from_table_accessor, $to_table_accessor) = @$accessors;
58             } else {
59 1         3 $to_table_accessor = $accessors;
60             }
61             }
62            
63 2         11 $from_table->{has_many}{ $from_table_accessor } = [ $to_table->{name}, { $from_key => $to_key } ];
64 2 50       17 $to_table->{has_one}{ $to_table_accessor } = [ $from_table->{name}, { $to_key => $from_key } ]
65             if $to_table_accessor;
66             }
67              
68             1;
69              
70             __END__