File Coverage

blib/lib/DBIx/Class/Candy.pm
Criterion Covered Total %
statement 62 161 38.5
branch 9 26 34.6
condition 1 2 50.0
subroutine 13 31 41.9
pod 0 14 0.0
total 85 234 36.3


line stmt bran cond sub pod time code
1             package DBIx::Class::Candy;
2              
3 2     2   1443 use strict;
  2         4  
  2         144  
4 2     2   12 use warnings;
  2         3  
  2         109  
5              
6             our $VERSION = '0.002107'; # VERSION
7              
8 2     2   896 use namespace::clean;
  2         22786  
  2         11  
9             require DBIx::Class::Candy::Exports;
10 2     2   1294 use MRO::Compat;
  2         4358  
  2         55  
11 2     2   1016 use Sub::Exporter 'build_exporter';
  2         16082  
  2         14  
12 2     2   492 use Carp 'croak';
  2         3  
  2         2900  
13              
14             # ABSTRACT: Sugar for your favorite ORM, DBIx::Class
15              
16             my %aliases = (
17             column => 'add_columns',
18             primary_key => 'set_primary_key',
19             unique_constraint => 'add_unique_constraint',
20             relationship => 'add_relationship',
21             );
22              
23             my @methods = qw(
24             resultset_class
25             resultset_attributes
26             remove_columns
27             remove_column
28             table
29             source_name
30              
31             inflate_column
32              
33             belongs_to
34             has_many
35             might_have
36             has_one
37             many_to_many
38              
39             sequence
40             );
41              
42 1   50 1 0 5 sub base { return $_[1] || 'DBIx::Class::Core' }
43              
44 1     1 0 2 sub perl_version { return $_[1] }
45              
46 0     0 0 0 sub autotable { $_[1] }
47              
48             sub gen_table {
49 4     4 0 5046 my ( $self, $class, $version ) = @_;
50 4 50       17 if ($version == 1) {
51 4 100       33 if (my ( $part ) = $class =~ /(?:::Schema)?::Result::(.+)$/) {
52 3         757 require Lingua::EN::Inflect;
53 3         15478 require String::CamelCase;
54 3         403 $part =~ s/:://g;
55 3         9 $part = String::CamelCase::decamelize($part);
56 3         81 return join q{_}, split /\s+/,
57             Lingua::EN::Inflect::PL(join q{ }, split /_/, $part);
58             } else {
59 1         168 croak 'unrecognized naming scheme!'
60             }
61             }
62             }
63              
64             sub import {
65 1     1   9 my $self = shift;
66              
67 1         3 my $inheritor = caller(0);
68 1         3 my $args = $self->parse_arguments(\@_);
69 1         3 my $perl_version = $self->perl_version($args->{perl_version});
70 1         0 my @rest = @{$args->{rest}};
  1         2  
71              
72 1         2 $self->set_base($inheritor, $args->{base});
73 0         0 $inheritor->load_components(@{$args->{components}});
  0         0  
74 0         0 my @custom_methods;
75             my %custom_aliases;
76             {
77 0         0 my @custom = $self->gen_custom_imports($inheritor);
  0         0  
78 0         0 @custom_methods = @{$custom[0]};
  0         0  
79 0         0 %custom_aliases = %{$custom[1]};
  0         0  
80             }
81              
82 0     0   0 my $set_table = sub {};
  0         0  
83 0 0       0 if (my $v = $self->autotable($args->{autotable})) {
84 0         0 my $table_name = $self->gen_table($inheritor, $v);
85 0         0 my $ran = 0;
86 0 0   0   0 $set_table = sub { $inheritor->table($table_name) unless $ran++ }
87 0         0 }
88 0         0 @_ = ($self, @rest);
89 0         0 my $import = build_exporter({
90             exports => [
91             has_column => $self->gen_has_column($inheritor, $set_table),
92             primary_column => $self->gen_primary_column($inheritor, $set_table),
93             unique_column => $self->gen_unique_column($inheritor, $set_table),
94 0         0 (map { $_ => $self->gen_proxy($inheritor, $set_table) } @methods, @custom_methods),
95 0         0 (map { $_ => $self->gen_rename_proxy($inheritor, $set_table, %aliases, %custom_aliases) }
96             keys %aliases, keys %custom_aliases),
97             ],
98             groups => {
99             default => [
100             qw(has_column primary_column unique_column), @methods, @custom_methods, keys %aliases, keys %custom_aliases
101             ],
102             },
103             installer => $self->installer,
104             collectors => [
105             INIT => $self->gen_INIT($perl_version, \%custom_aliases, \@custom_methods, $inheritor),
106             ],
107             });
108              
109 0         0 goto $import
110             }
111              
112             sub gen_custom_imports {
113 0     0 0 0 my ($self, $inheritor) = @_;
114 0         0 my @methods;
115             my %aliases;
116 0         0 for (@{mro::get_linear_isa($inheritor)}) {
  0         0  
117 0 0       0 if (my $a = $DBIx::Class::Candy::Exports::aliases{$_}) {
118 0         0 %aliases = (%aliases, %$a)
119             }
120 0 0       0 if (my $m = $DBIx::Class::Candy::Exports::methods{$_}) {
121 0         0 @methods = (@methods, @$m)
122             }
123             }
124 0         0 return(\@methods, \%aliases)
125             }
126              
127             sub parse_arguments {
128 1     1 0 2 my $self = shift;
129 1         1 my @args = @{shift @_};
  1         3  
130              
131 1         1 my $skipnext;
132             my $base;
133 0         0 my @rest;
134 1         1 my $perl_version = undef;
135 1         2 my $components = [];
136 1         1 my $autotable = 0;
137 1         3 for my $idx ( 0 .. $#args ) {
138 4         5 my $val = $args[$idx];
139              
140 4 50       7 next unless defined $val;
141 4 100       6 if ($skipnext) {
142 2         1 $skipnext--;
143 2         4 next;
144             }
145              
146 2 100       5 if ( $val eq '-base' ) {
    50          
    0          
    0          
147 1         15 $base = $args[$idx + 1];
148 1         1 $skipnext = 1;
149             } elsif ( $val eq '-autotable' ) {
150 1         2 $autotable = ord $args[$idx + 1];
151 1         1 $skipnext = 1;
152             } elsif ( $val eq '-perl5' ) {
153 0         0 $perl_version = ord $args[$idx + 1];
154 0         0 $skipnext = 1;
155             } elsif ( $val eq '-components' ) {
156 0         0 $components = $args[$idx + 1];
157 0         0 $skipnext = 1;
158             } else {
159 0         0 push @rest, $val;
160             }
161             }
162              
163             return {
164 1         5 autotable => $autotable,
165             base => $base,
166             perl_version => $perl_version,
167             components => $components,
168             rest => \@rest,
169             };
170             }
171              
172             sub gen_primary_column {
173 0     0 0 0 my ($self, $inheritor, $set_table) = @_;
174             sub {
175 0     0   0 my $i = $inheritor;
176             sub {
177 0         0 my $column = shift;
178 0         0 my $info = shift;
179 0         0 $set_table->();
180 0         0 $i->add_columns($column => $info);
181 0         0 $i->set_primary_key($i->primary_columns, $column);
182             }
183 0         0 }
184 0         0 }
185              
186             sub gen_unique_column {
187 0     0 0 0 my ($self, $inheritor, $set_table) = @_;
188             sub {
189 0     0   0 my $i = $inheritor;
190             sub {
191 0         0 my $column = shift;
192 0         0 my $info = shift;
193 0         0 $set_table->();
194 0         0 $i->add_columns($column => $info);
195 0         0 $i->add_unique_constraint([ $column ]);
196             }
197 0         0 }
198 0         0 }
199              
200             sub gen_has_column {
201 0     0 0 0 my ($self, $inheritor, $set_table) = @_;
202             sub {
203 0     0   0 my $i = $inheritor;
204             sub {
205 0         0 my $column = shift;
206 0         0 $set_table->();
207 0         0 $i->add_columns($column => { @_ })
208             }
209 0         0 }
210 0         0 }
211              
212             sub gen_rename_proxy {
213 0     0 0 0 my ($self, $inheritor, $set_table, %aliases) = @_;
214             sub {
215 0     0   0 my ($class, $name) = @_;
216 0         0 my $meth = $aliases{$name};
217 0         0 my $i = $inheritor;
218 0         0 sub { $set_table->(); $i->$meth(@_) }
  0         0  
219 0         0 }
220 0         0 }
221              
222             sub gen_proxy {
223 0     0 0 0 my ($self, $inheritor, $set_table) = @_;
224             sub {
225 0     0   0 my ($class, $name) = @_;
226 0         0 my $i = $inheritor;
227 0         0 sub { $set_table->(); $i->$name(@_) }
  0         0  
228 0         0 }
229 0         0 }
230              
231             sub installer {
232 0     0 0 0 my ($self) = @_;
233             sub {
234 0     0   0 Sub::Exporter::default_installer @_;
235 0         0 my %subs = @{ $_[1] };
  0         0  
236 0         0 namespace::clean->import( -cleanee => $_[0]{into}, keys %subs )
237             }
238 0         0 }
239              
240             sub set_base {
241 1     1 0 2 my ($self, $inheritor, $base) = @_;
242              
243             # inlined from parent.pm
244 1         2 for ( my @useless = $self->base($base) ) {
245 1         10 s{::|'}{/}g;
246 1         241 require "$_.pm"; # dies if the file is not found
247             }
248              
249             {
250 2     2   13 no strict 'refs';
  2         3  
  2         511  
  0            
251             # This is more efficient than push for the new MRO
252             # at least until the new MRO is fixed
253 0           @{"$inheritor\::ISA"} = (@{"$inheritor\::ISA"} , $self->base($base));
  0            
  0            
254             }
255             }
256              
257             sub gen_INIT {
258 0     0 0   my ($self, $perl_version, $custom_aliases, $custom_methods, $inheritor) = @_;
259             sub {
260 0     0     my $orig = $_[1]->{import_args};
261 0           $_[1]->{import_args} = [];
262 0           %$custom_aliases = ();
263 0           @$custom_methods = ();
264              
265 0 0         if ($perl_version) {
266 0           require feature;
267 0           feature->import(":5.$perl_version")
268             }
269              
270 0           strict->import;
271 0           warnings->import;
272              
273 0           1;
274             }
275 0           }
276              
277             1;
278              
279             __END__