File Coverage

blib/lib/Aniki/Schema/Relationship/Declare.pm
Criterion Covered Total %
statement 62 62 100.0
branch n/a
condition n/a
subroutine 16 16 100.0
pod n/a
total 78 78 100.0


line stmt bran cond sub pod time code
1             package Aniki::Schema::Relationship::Declare;
2 28     28   8983600 use 5.014002;
  28         181  
3              
4 28     28   172 use strict;
  28         112  
  28         611  
5 28     28   165 use warnings;
  28         132  
  28         844  
6              
7 28     28   205 use B::Hooks::EndOfScope;
  28         80  
  28         375  
8 28     28   2916 use DBIx::Schema::DSL ();
  28         305963  
  28         544  
9 28     28   168 use Aniki::Schema::Relationship;
  28         79  
  28         1623  
10              
11             my %RULES;
12              
13             sub import {
14 28     28   358 my $caller = caller;
15             {
16 28     28   183 no strict qw/refs/;
  28         85  
  28         3393  
  28         64  
17 28     86   125 *{"${caller}::relationship_rules"} = sub { $RULES{$caller} };
  28         222  
  86         5035  
18 28         84 *{"${caller}::relation"} = \&_relation;
  28         143  
19 28         70 *{"${caller}::relay_to"} = \&_relay_to;
  28         119  
20 28         70 *{"${caller}::relay_by"} = \&_relay_by;
  28         125  
21             }
22             on_scope_end {
23 28     28   205 no strict qw/refs/;
  28         76  
  28         784  
24 28     28   236 no warnings qw/redefine/;
  28         66  
  28         8705  
25 28     28   6774 *{"${caller}::create_table"} = \&_create_table;
  28         241  
26 28         254 };
27             }
28              
29             my %TABLE_NAME;
30              
31             sub _create_table ($$) {## no critic
32 80     80   696335 my ($table_name, $code) = @_;
33 80         284 my $caller = caller;
34 80         385 $TABLE_NAME{$caller} = $table_name;
35 80         437 goto \&DBIx::Schema::DSL::create_table;
36             }
37              
38             sub _relation {
39 104     104   439 my ($src_columns, $dest_table_name, $dest_columns, %opt) = @_;
40 104         305 my $caller = caller;
41 104         264 my $src_table_name = $TABLE_NAME{$caller};
42 104         270 push @{ $RULES{$caller} } => {
  104         836  
43             src_table_name => $src_table_name,
44             src_columns => $src_columns,
45             dest_table_name => $dest_table_name,
46             dest_columns => $dest_columns,
47             %opt,
48             };
49             }
50              
51             sub _relay_to {
52 52     52   28629 my $dest_table_name = shift;
53 52         169 my $caller = caller;
54 52         215 my $src_columns = ["${dest_table_name}_id"];
55 52         239 my $dest_columns = ['id'];
56 52         208 @_ = ($src_columns, $dest_table_name, $dest_columns, @_);
57 52         247 goto \&_relation;
58             }
59              
60             sub _relay_by {
61 52     52   76411 my $dest_table_name = shift;
62 52         145 my $caller = caller;
63 52         146 my $src_table_name = $TABLE_NAME{$caller};
64 52         244 my $src_columns = ['id'];
65 52         218 my $dest_columns = ["${src_table_name}_id"];
66 52         210 @_ = ($src_columns, $dest_table_name, $dest_columns, @_);
67 52         230 goto \&_relation;
68             }
69              
70             1;
71             __END__
72              
73             =pod
74              
75             =encoding utf-8
76              
77             =head1 NAME
78              
79             Aniki::Schema::Relationship::Declare - DSL for declaring relationship rules
80              
81             =head1 SYNOPSIS
82              
83             use 5.014002;
84             package MyProj::DB::Schema {
85             use DBIx::Schema::DSL;
86             use Aniki::Schema::Relationship::Declare;
87              
88             create_table 'module' => columns {
89             integer 'id', primary_key, auto_increment;
90             varchar 'name';
91             integer 'author_id';
92              
93             add_index 'author_id_idx' => ['author_id'];
94              
95             relay_to 'author', name => '';
96             };
97              
98             create_table 'author' => columns {
99             integer 'id', primary_key, auto_increment;
100             varchar 'name', unique;
101              
102             relay_by 'module';
103             };
104             };
105              
106             1;
107              
108             =head1 FUNCTIONS
109              
110             =over 4
111              
112             =item C<relay_to>
113              
114             =item C<relay_by>
115              
116             =item C<relation>
117              
118             =back
119              
120             =head1 SEE ALSO
121              
122             L<perl>
123              
124             =head1 LICENSE
125              
126             Copyright (C) karupanerura.
127              
128             This library is free software; you can redistribute it and/or modify
129             it under the same terms as Perl itself.
130              
131             =head1 AUTHOR
132              
133             karupanerura E<lt>karupa@cpan.orgE<gt>
134              
135             =cut