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.31';
3 3     3   13 use strict;
  3         3  
  3         72  
4 3     3   9 use warnings;
  3         3  
  3         62  
5              
6 3     3   10 use Carp qw(croak);
  3         3  
  3         111  
7 3     3   1082 use DBIx::Lite::Schema::Table;
  3         6  
  3         895  
8             $Carp::Internal{$_}++ for __PACKAGE__;
9              
10             sub new {
11 3     3 1 6 my $class = shift;
12 3         4 my (%params) = @_;
13            
14 3         10 my $self = {
15             tables => {},
16             };
17            
18 3 50       10 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 3 50       13 !%params
26             or croak "Unknown options: " . join(', ', keys %params);
27            
28 3         6 bless $self, $class;
29 3         24 $self;
30             }
31              
32             sub table {
33 33     33 1 41 my $self = shift;
34 33         46 my $table_name = shift;
35 33   66     191 $self->{tables}{$table_name} ||= DBIx::Lite::Schema::Table->new(name => $table_name);
36 33         242 return $self->{tables}{$table_name};
37             }
38              
39             sub one_to_many {
40 2     2 1 4 my $self = shift;
41 2         6 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         11 my $from_table = $self->table($1);
46 2         6 my $from_key = $2;
47            
48 2 50 33     19 $to && $to =~ /^(.+)\.(.+)$/
49             or croak "Relationship keys must be defined in table.column format";
50 2         8 my $to_table = $self->table($1);
51 2         6 my $to_key = $2;
52            
53 2         5 my $from_table_accessor = $to_table->{name};
54 2         3 my $to_table_accessor;
55 2 50       9 if ($accessors) {
56 2 100       7 if (ref $accessors eq 'ARRAY') {
57 1         5 ($from_table_accessor, $to_table_accessor) = @$accessors;
58             } else {
59 1         3 $to_table_accessor = $accessors;
60             }
61             }
62            
63 2         12 $from_table->{has_many}{ $from_table_accessor } = [ $to_table->{name}, { $from_key => $to_key } ];
64 2 50       16 $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__