Helpers/Util.pm

filter: Statementstmt 90.5% 48 / 53 Branchbran 82.3% 28 / 34 Conditioncond 83.3% 15 / 18 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 100% 10 / 10 Podpod 100% 6 / 6 totaltotal 86.2% 113 / 131 CC 25 SCAR 18.6
CC
25
Cov
87%
normalize_connect_info
11.7
order_by_visitor
6.1
assert_similar_namespaces
5.0
CRAP 6.4
↑ Summary Helper/Row/OnColumnChange.pm »
1 package DBIx::Class::Helpers::Util;
2 55 $DBIx::Class::Helpers::Util::VERSION = '2.032002';
3 55 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 55 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: Helper utilities for DBIx::Class components
7  
8 55 use Sub::Exporter::Progressive -setup => {
9 exports => [
10 qw(
11 get_namespace_parts is_load_namespaces is_not_load_namespaces
12 assert_similar_namespaces order_by_visitor
13 normalize_connect_info
14 ),
15 ],
16 55 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 sub get_namespace_parts {
19 434 ▶ my $package = shift;
Subroutine get_namespace_parts: called CC=2 SCAR=7.6
Pod documented
20  
21 434 ▶ if ($package =~ m/(^[\w:]+::Result)::([\w:]+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package =~ /(^[\w:]+::Result)::([\w:]+)$/) { } 434 0 434
22 434 return ($1, $2);
23 } else {
24 0 die "$package doesn't look like ".'$namespace::Result::$resultclass';
25 }
26 }
27  
28 sub is_load_namespaces {
29 443 ▶ my $namespace = shift;
Subroutine is_load_namespaces: called CC=1 SCAR=0.0
Pod documented
30 443 $namespace =~ /^[\w:]+::Result::[\w:]+$/;
31 }
32  
33 sub is_not_load_namespaces {
34 7 ▶ my $namespace = shift;
Subroutine is_not_load_namespaces: called CC=2 SCAR=7.2
Pod documented
35 7 ▶ $namespace =~ /^([\w:]+)::[\w]+$/ and
Branch: 1 branch
BranchTrueFalseTotal
$namespace =~ /^([\w:]+)::[\w]+$/ and not $1 =~ /::Result/ 7 0 7
36 $1 !~ /::Result/;
37 }
38  
39 sub assert_similar_namespaces {
40 221 ▶ my $ns1 = shift;
Subroutine assert_similar_namespaces: called CC=5 SCAR=16.1
Pod documented
41 221 my $ns2 = shift;
42  
43 221 ▶ die "Namespaces $ns1 and $ns2 are dissimilar"
Branch: 1 branch
BranchTrueFalseTotal
unless is_load_namespaces($ns1) and is_load_namespaces($ns2) or is_not_load_namespaces($ns1) and is_not_load_namespaces($ns2) 2 219 221
Condition100%9/9
is_load_namespaces($ns1) and is_load_namespaces($ns2) or is_not_load_namespaces($ns1) and is_not_load_namespaces($ns2)
l!l&&r!l&&!r
21812
is_load_namespaces($ns1) and is_load_namespaces($ns2)
!ll&&!rl&&r
21218
is_not_load_namespaces($ns1) and is_not_load_namespaces($ns2)
!ll&&!rl&&r
111
MC/DC75%3/4
is_load_namespaces($ns1) and is_load_namespaces($ns2) or is_not_load_namespaces($ns1) and is_not_load_namespaces($ns2)
is_load_namespaces($ns1) is_load_namespaces($ns2) is_not_load_namespaces($ns1) is_not_load_namespaces($ns2)
Truth table57%4/7
A and B or C and D
A= is_load_namespaces($ns1)B= is_load_namespaces($ns2)C= is_not_load_namespaces($ns1)D= is_not_load_namespaces($ns2)
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
44 unless is_load_namespaces($ns1) and is_load_namespaces($ns2) or
45 is_not_load_namespaces($ns1) and is_not_load_namespaces($ns2);
46 }
47  
48 sub _order_by_visitor_HASHREF {
49 4 ▶ my ($hash, $callback) = @_;
Subroutine _order_by_visitor_HASHREF: called CC=4 SCAR=14.0
50  
51 4 my %ret;
52 # there should only be one k/v pair, but DBIC checks for that and I'm not
53 # going to reimplement said check here.
54 4 for my $k (keys %$hash) {
55 4 my $v = $hash->{$k};
56  
57 4 ▶ if (my $v_ref = ref $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $v_ref = ref $v) { } 1 3 4
58 1 ▶ if ($v_ref eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v_ref eq 'ARRAY') { } 1 0 1
59 1 $ret{$k} = [ map $callback->($_), @$v ]
60 } else {
61 0 die 'this should never happen'
62 }
63 } else {
64 3 $ret{$k} = ($callback->($v));
65 }
66 }
67  
68 4 \%ret
69 }
70  
71 sub order_by_visitor {
72 4 ▶ my ($order, $callback) = @_;
Subroutine order_by_visitor: called CC=6 SCAR=18.1
Pod documented
73  
74 4 ▶ if (my $top_ref = ref $order) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $top_ref = ref $order) { } 3 1 4
75 3 ▶ if ($top_ref eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($top_ref eq 'HASH') { } 1 2 3
elsif ($top_ref eq 'ARRAY') { } 2 0 2
76 1 return _order_by_visitor_HASHREF($order, $callback)
77 } elsif ($top_ref eq 'ARRAY') {
78 return [
79 map {
80 2 ▶ if (my $ref = ref $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $_) { } 3 2 5
81 3 ▶ if ($ref eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 3 0 3
82 3 _order_by_visitor_HASHREF($_, $callback)
83 } else {
84 0 die 'this should never happen'
85 }
86 } else {
87 2 $callback->($_)
88 }
89 } @$order
90 ];
91 }
92 } else {
93 1 return $callback->($order)
94 }
95 }
96  
97 sub normalize_connect_info {
98 63 ▶ my %all;
Subroutine normalize_connect_info: called CC=11 SCAR=24.6
Pod documented
99  
100 63 ▶ if (!ref $_[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $_[0]) { } 61 2 63
elsif (ref $_[0] eq 'CODE') { } 2 0 2
101 %all = (
102 dsn => $_[0],
103 ( exists $_[1] ?
104 (user => $_[1],
105  
106 exists $_[2] ?
107 ( password => $_[2],
108  
109 ( exists $_[3] && ref $_[3] ?
110 2 ▶ %{$_[3]}
Branch: 1 branch
BranchTrueFalseTotal
exists $_[4] && ref $_[4] ? : 1 2 3
Condition66%2/3
exists $_[4] and ref $_[4]
!ll&&!rl&&r
201
MC/DC50%1/2
exists $_[4] and ref $_[4]
exists $_[4] ref $_[4]
Truth table66%2/3
A and B
A= exists $_[4]B= ref $_[4]
ABresult
0X0
100
111
111 : ()
112 ),
113  
114 ( exists $_[4] && ref $_[4] ?
115 61 ▶ %{$_[4]}
Branch: 3 branches
BranchTrueFalseTotal
exists $_[1] ? : 4 57 61
exists $_[2] ? : 3 1 4
exists $_[3] && ref $_[3] ? : 2 1 3
Condition66%2/3
exists $_[3] and ref $_[3]
!ll&&!rl&&r
102
MC/DC50%1/2
exists $_[3] and ref $_[3]
exists $_[3] ref $_[3]
Truth table66%2/3
A and B
A= exists $_[3]B= ref $_[3]
ABresult
0X0
100
111
116 : ()
117 ),
118  
119 )
120 : ()
121  
122 )
123  
124 : ()
125 ),
126 )
127 } elsif (ref $_[0] eq 'CODE') {
128 %all = (
129 dbh_maker => $_[0],
130 ( exists $_[1] && ref $_[1] ?
131 2 ▶ %{$_[1]}
Branch: 1 branch
BranchTrueFalseTotal
exists $_[1] && ref $_[1] ? : 1 1 2
Condition66%2/3
exists $_[1] and ref $_[1]
!ll&&!rl&&r
101
MC/DC50%1/2
exists $_[1] and ref $_[1]
exists $_[1] ref $_[1]
Truth table66%2/3
A and B
A= exists $_[1]B= ref $_[1]
ABresult
0X0
100
111
132 : ()
133 ),
134 )
135 } else {
136 0 %all = %{$_[0]}
137 }
138  
139 63 return \%all;
140 }
141  
142 55 1;
143  
144 __END__
145  
146 =pod
147  
148 =head1 NAME
149  
150 DBIx::Class::Helpers::Util - Helper utilities for DBIx::Class components
151  
152 =head1 SYNOPSIS
153  
154 use DBIx::Class::Helpers::Util ':all';
155  
156 my ($namespace, $class) = get_namespace_parts('MyApp:Schema::Person');
157 is $namespace, 'MyApp::Schema';
158 is $class, 'Person';
159  
160 if (is_load_namespaces('MyApp::Schema::Result::Person')) {
161 print 'correctly structured project';
162 }
163  
164 if (is_not_load_namespaces('MyApp::Schema::Person')) {
165 print 'incorrectly structured project';
166 }
167  
168 if (assert_similar_namespaces('MyApp::Schema::Person', 'FooApp::Schema::People')) {
169 print 'both projects are structured similarly';
170 }
171  
172 if (assert_similar_namespaces('MyApp::Schema::Result::Person', 'FooApp::Schema::Result::People')) {
173 print 'both projects are structured similarly';
174 }
175  
176 # in a resultset
177  
178 sub search {
179 my ($self, $search, $attrs) = @_;
180  
181 $attrs->{order_by} = order_by_visitor($attrs->{order_by}, sub {
182 my $field = shift;
183  
184 return 'foo_bar' if $field eq 'foo.bar';
185 return $field;
186 }) if $attrs && $attrs->{order_by};
187  
188 $self->next::method($search, $attrs);
189 }
190  
191 # in schema
192  
193 sub connection {
194 my $self = shift;
195  
196 my $args = normalize_connect_info(@_);
197 $args->{quote_names} = 1;
198  
199 $self->next::method($args)
200 }
201  
202 =head1 DESCRIPTION
203  
204 A collection of various helper utilities for L<DBIx::Class> stuff. Probably
205 only useful for components.
206  
207 =head1 EXPORTS
208  
209 =head2 order_by_visitor
210  
211 This function allows you to easily transform C<order_by> clauses. See
212 L</SYNOPSIS> for example.
213  
214 =head2 get_namespace_parts
215  
216 Returns the namespace and class name of a package. See L</SYNOPSIS> for example.
217  
218 =head2 is_load_namespaces
219  
220 Returns true if a package is structured in a way that would work for
221 load_namespaces. See L</SYNOPSIS> for example.
222  
223 =head2 is_not_load_namespaces
224  
225 Returns true if a package is structured in a way that would not work for
226 load_namespaces. See L</SYNOPSIS> for example.
227  
228 =head2 assert_similar_namespaces
229  
230 Dies if both packages are structured in the same way. The same means both are
231 load_namespaces or both are not. See L</SYNOPSIS> for example.
232  
233 =head2 normalize_connect_info
234  
235 Takes L<all of the various and interesting
236 forms|DBIx::Class::Storage::DBI/connect_info> that can be passed to connect and
237 normalizes them into the final and simplest form, a single hashref.
238  
239 =head1 AUTHOR
240  
241 Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
242  
243 =head1 COPYRIGHT AND LICENSE
244  
245 This software is copyright (c) 2016 by Arthur Axel "fREW" Schmidt.
246  
247 This is free software; you can redistribute it and/or modify it under
248 the same terms as the Perl 5 programming language system itself.
249  
250 =cut
↑ Summary Helper/Row/OnColumnChange.pm »