Candy.pm

filter: Statementstmt 95.0% 174 / 183 Branchbran 63.8% 23 / 36 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 38 / 38 Podpod 0.0% 0 / 15 totaltotal 84.6% 237 / 280 CC 24 SCAR 14.0
CC
24
Cov
89%
parse_arguments
14.5
__ANON__
5.0
gen_custom_imports
4.5
CRAP 4.0
« Candy/ResultSet.pm ↑ Summary Candy/Exports.pm »
1 package DBIx::Class::Candy;
2 5 $DBIx::Class::Candy::VERSION = '0.005002';
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 require DBIx::Class::Candy::Exports;
8 5 ▶use MRO::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Sub::Exporter 'build_exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # ABSTRACT: Sugar for your favorite ORM, DBIx::Class
13  
14 5 my %aliases = (
15 column => 'add_columns',
16 primary_key => 'set_primary_key',
17 unique_constraint => 'add_unique_constraint',
18 relationship => 'add_relationship',
19 );
20  
21 5 my @methods = qw(
22 resultset_class
23 resultset_attributes
24 remove_columns
25 remove_column
26 table
27 source_name
28  
29 inflate_column
30  
31 belongs_to
32 has_many
33 might_have
34 has_one
35 many_to_many
36  
37 sequence
38 );
39  
40 12 ▶sub base { return $_[1] || 'DBIx::Class::Core' }
Subroutine base: called CC=2 SCAR=7.6
Pod undocumented
Condition50%1/2
$_[1] || 'DBIx::Class::Core'
l!l
120
MC/DC0%0/1
$_[1] || 'DBIx::Class::Core'
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
41  
42 7 ▶sub perl_version { return $_[1] }
Subroutine perl_version: called CC=1 SCAR=0.0
Pod undocumented
43  
44 7 ▶sub autotable { $_[1] }
Subroutine autotable: called CC=1 SCAR=0.0
Pod undocumented
45  
46 10 ▶sub experimental { $_[1] }
Subroutine experimental: called CC=1 SCAR=0.0
Pod undocumented
47  
48 sub _extract_part {
49 21 ▶ my ($self, $class) = @_;
Subroutine _extract_part: called CC=2 SCAR=6.9
50 21 ▶ if (my ( $part ) = $class =~ /(?:::Schema)?::Result::(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($part) = $class =~ /(?:::Schema)?::Result::(.+)$/) { } 20 1 21
51 20 return $part
52 } else {
53 1 croak 'unrecognized naming scheme!'
54 }
55 }
56  
57 sub gen_table {
58 21 ▶ my ( $self, $class, $version ) = @_;
Subroutine gen_table: called CC=3 SCAR=11.0
Pod undocumented
59 21 ▶ if ($version eq 'singular') {
Branch: 2 branches
BranchTrueFalseTotal
if ($version eq 'singular') { } 2 19 21
elsif ($version == 1) { } 19 0 19
60 2 my $part = $self->_extract_part($class);
61 2 require String::CamelCase;
62 2 $part =~ s/:://g;
63 2 return String::CamelCase::decamelize($part);
64 } elsif ($version == 1) {
65 19 my $part = $self->_extract_part($class);
66 18 require Lingua::EN::Inflect;
67 18 require String::CamelCase;
68 18 $part =~ s/:://g;
69 18 $part = String::CamelCase::decamelize($part);
70 18 return join q{_}, split /\s+/, Lingua::EN::Inflect::PL(join q{ }, split /_/, $part);
71 }
72 }
73  
74 sub import {
75 20 ▶ my $self = shift;
Subroutine import: called CC=2 SCAR=6.9
76  
77 20 my $inheritor = caller(0);
78 20 my $args = $self->parse_arguments(\@_);
79 20 my $perl_version = $self->perl_version($args->{perl_version});
80 20 my $experimental = $self->experimental($args->{experimental});
81 20 my @rest = @{$args->{rest}};
82  
83 20 $self->set_base($inheritor, $args->{base});
84 19 $inheritor->load_components(@{$args->{components}});
85 19 my @custom_methods;
86 my %custom_aliases;
87 {
88 19 my @custom = $self->gen_custom_imports($inheritor);
89 19 @custom_methods = @{$custom[0]};
90 19 %custom_aliases = %{$custom[1]};
91 }
92  
93 19 ▶ my $set_table = sub {};
Subroutine __ANON__: called CC=1 SCAR=0.0
94 19 ▶ if (my $v = $self->autotable($args->{autotable})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $v = $self->autotable($args->{"autotable"})) 15 4 19
95 15 my $table_name = $self->gen_table($inheritor, $v);
96 15 my $ran = 0;
97 75 ▶ $set_table = sub { $inheritor->table($table_name) unless $ran++ }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless $ran++ 15 60 75
98 15 }
99 19 @_ = ($self, @rest);
100 my $import = build_exporter({
101 exports => [
102 has_column => $self->gen_has_column($inheritor, $set_table),
103 primary_column => $self->gen_primary_column($inheritor, $set_table),
104 unique_column => $self->gen_unique_column($inheritor, $set_table),
105 247 (map { $_ => $self->gen_proxy($inheritor, $set_table) } @methods, @custom_methods),
106 19 (map { $_ => $self->gen_rename_proxy($inheritor, $set_table, %aliases, %custom_aliases) }
107 keys %aliases, keys %custom_aliases),
108 ],
109 groups => {
110 default => [
111 qw(has_column primary_column unique_column), @methods, @custom_methods, keys %aliases, keys %custom_aliases
112 ],
113 },
114 installer => $self->installer,
115 collectors => [
116 INIT => $self->gen_INIT($perl_version, \%custom_aliases, \@custom_methods, $inheritor, $experimental),
117 ],
118 });
119  
120 19 goto $import
121 }
122  
123 sub gen_custom_imports {
124 19 ▶ my ($self, $inheritor) = @_;
Subroutine gen_custom_imports: called CC=4 SCAR=15.0
Pod undocumented
125 19 my @methods;
126 my %aliases;
127 19 for (@{mro::get_linear_isa($inheritor)}) {
128 465 ▶ if (my $a = $DBIx::Class::Candy::Exports::aliases{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $a = $DBIx::Class::Candy::Exports::aliases{$_}) 0 465 465
129 0 %aliases = (%aliases, %$a)
130 }
131 465 ▶ if (my $m = $DBIx::Class::Candy::Exports::methods{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $m = $DBIx::Class::Candy::Exports::methods{$_}) 0 465 465
132 0 @methods = (@methods, @$m)
133 }
134 }
135 19 return(\@methods, \%aliases)
136 }
137  
138 sub parse_arguments {
139 20 ▶ my $self = shift;
Subroutine parse_arguments: called CC=10 SCAR=26.7
Pod undocumented
140 20 my @args = @{shift @_};
141  
142 20 my $skipnext;
143 my $base;
144 20 my @rest;
145 20 my $perl_version = undef;
146 20 my $components = [];
147 20 my $autotable = 0;
148 20 my $experimental;
149  
150 20 for my $idx ( 0 .. $#args ) {
151 26 my $val = $args[$idx];
152  
153 26 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 26 26
154 26 ▶ if ($skipnext) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skipnext) 13 13 26
155 13 $skipnext--;
156 13 next;
157 }
158  
159 13 ▶ if ( $val eq '-base' ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($val eq '-base') { } 10 3 13
elsif ($val eq '-autotable') { } 3 0 3
elsif ($val eq '-perl5') { } 0 0 0
elsif ($val eq '-experimental') { } 0 0 0
elsif ($val eq '-components') { } 0 0 0
160 10 $base = $args[$idx + 1];
161 10 $skipnext = 1;
162 } elsif ( $val eq '-autotable' ) {
163 3 $autotable = $args[$idx + 1];
164 3 ▶ $autotable = ord $autotable if length $autotable == 1;
Branch: 1 branch
BranchTrueFalseTotal
if length $autotable == 1 3 0 3
165 3 $skipnext = 1;
166 } elsif ( $val eq '-perl5' ) {
167 0 $perl_version = ord $args[$idx + 1];
168 0 $skipnext = 1;
169 } elsif ( $val eq '-experimental' ) {
170 0 $experimental = $args[$idx + 1];
171 0 $skipnext = 1;
172 } elsif ( $val eq '-components' ) {
173 0 $components = $args[$idx + 1];
174 0 $skipnext = 1;
175 } else {
176 0 push @rest, $val;
177 }
178 }
179  
180 return {
181 20 autotable => $autotable,
182 base => $base,
183 perl_version => $perl_version,
184 components => $components,
185 rest => \@rest,
186 experimental => $experimental,
187 };
188 }
189  
190 sub gen_primary_column {
191 19 ▶ my ($self, $inheritor, $set_table) = @_;
Subroutine gen_primary_column: called CC=1 SCAR=0.0
Pod undocumented
192 sub {
193 19 ▶ my $i = $inheritor;
Subroutine __ANON__: called CC=1 SCAR=0.0
194 sub {
195 16 ▶ my $column = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
196 16 my $info = shift;
197 16 $set_table->();
198 16 $i->add_columns($column => $info);
199 16 $i->set_primary_key($i->primary_columns, $column);
200 }
201 19 }
202 19 }
203  
204 sub gen_unique_column {
205 19 ▶ my ($self, $inheritor, $set_table) = @_;
Subroutine gen_unique_column: called CC=1 SCAR=0.0
Pod undocumented
206 sub {
207 19 ▶ my $i = $inheritor;
Subroutine __ANON__: called CC=1 SCAR=0.0
208 sub {
209 9 ▶ my $column = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
210 9 my $info = shift;
211 9 $set_table->();
212 9 $i->add_columns($column => $info);
213 9 $i->add_unique_constraint([ $column ]);
214 }
215 19 }
216 19 }
217  
218 sub gen_has_column {
219 19 ▶ my ($self, $inheritor, $set_table) = @_;
Subroutine gen_has_column: called CC=1 SCAR=0.0
Pod undocumented
220 sub {
221 19 ▶ my $i = $inheritor;
Subroutine __ANON__: called CC=1 SCAR=0.0
222 sub {
223 2 ▶ my $column = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
224 2 $set_table->();
225 2 $i->add_columns($column => { @_ })
226 }
227 19 }
228 19 }
229  
230 sub gen_rename_proxy {
231 76 ▶ my ($self, $inheritor, $set_table, %aliases) = @_;
Subroutine gen_rename_proxy: called CC=1 SCAR=0.0
Pod undocumented
232 sub {
233 76 ▶ my ($class, $name) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
234 76 my $meth = $aliases{$name};
235 76 my $i = $inheritor;
236 41 ▶ sub { $set_table->(); $i->$meth(@_) }
Subroutine __ANON__: called CC=1 SCAR=0.0
237 76 }
238 76 }
239  
240 sub gen_proxy {
241 247 ▶ my ($self, $inheritor, $set_table) = @_;
Subroutine gen_proxy: called CC=1 SCAR=0.0
Pod undocumented
242 sub {
243 247 ▶ my ($class, $name) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
244 247 my $i = $inheritor;
245 26 ▶ sub { $set_table->(); $i->$name(@_) }
Subroutine __ANON__: called CC=1 SCAR=0.0
246 247 }
247 247 }
248  
249 sub installer {
250 19 ▶ my ($self) = @_;
Subroutine installer: called CC=1 SCAR=0.0
Pod undocumented
251 sub {
252 19 ▶ Sub::Exporter::default_installer @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
253 19 my %subs = @{ $_[1] };
254 19 namespace::clean->import( -cleanee => $_[0]{into}, keys %subs )
255 }
256 19 }
257  
258 sub set_base {
259 20 ▶ my ($self, $inheritor, $base) = @_;
Subroutine set_base: called CC=2 SCAR=6.9
Pod undocumented
260  
261 # inlined from parent.pm
262 20 for ( my @useless = $self->base($base) ) {
263 20 s{::|'}{/}g;
264 20 require "$_.pm"; # dies if the file is not found
265 }
266  
267 {
268 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
269 # This is more efficient than push for the new MRO
270 # at least until the new MRO is fixed
271 20 @{"$inheritor\::ISA"} = (@{"$inheritor\::ISA"} , $self->base($base));
272 }
273 }
274  
275 sub gen_INIT {
276 19 ▶ my ($self, $perl_version, $custom_aliases, $custom_methods, $inheritor, $experimental) = @_;
Subroutine gen_INIT: called CC=1 SCAR=0.0
Pod undocumented
277 sub {
278 19 ▶ my $orig = $_[1]->{import_args};
Subroutine __ANON__: called CC=5 SCAR=16.2
279 19 $_[1]->{import_args} = [];
280 19 %$custom_aliases = ();
281 19 @$custom_methods = ();
282  
283 19 strict->import;
284 19 warnings->import;
285  
286 19 ▶ if ($perl_version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($perl_version) 12 7 19
287 12 require feature;
288 12 feature->import(":5.$perl_version")
289 }
290  
291 19 ▶ if ($experimental) {
Branch: 1 branch
BranchTrueFalseTotal
if ($experimental) 9 10 19
292 9 require experimental;
293 9 ▶ die 'experimental arg must be an arrayref!'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $experimental and ref $experimental eq "ARRAY" 0 9 9
Condition33%1/3
ref $experimental and ref $experimental eq "ARRAY"
!ll&&!rl&&r
009
MC/DC0%0/2
ref $experimental and ref $experimental eq "ARRAY"
ref $experimental ref $experimental eq "ARRAY"
Truth table33%1/3
A and B
A= ref $experimentalB= ref $experimental eq "ARRAY"
ABresult
0X0
100
111
294 unless ref $experimental && ref $experimental eq 'ARRAY';
295 # to avoid experimental referring to the method
296 9 experimental::->import(@$experimental)
297 }
298  
299 19 mro::set_mro($inheritor, 'c3');
300  
301 19 1;
302 }
303 19 }
304  
305 5 1;
306  
307 __END__
« Candy/ResultSet.pm ↑ Summary Candy/Exports.pm »