ParameterizedJoinHack.pm

filter: Statementstmt 100% 39 / 39 Branchbran 100% 20 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 100% 65 / 65 CC 11 SCAR 19.6
CC
11
Cov
100%
parameterized_has_many
10.0
__ANON__
2.0
BEGIN
1.0
CRAP 7.1
↑ Summary ResultSet/ParameterizedJoinHack.pm »
1 package DBIx::Class::ParameterizedJoinHack;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use base qw(DBIx::Class);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.001_003'; # 0.1.3
8 1 $VERSION = eval $VERSION;
9  
10 1 our $STORE = '_parameterized_join_hack_meta_info';
11  
12 1 __PACKAGE__->mk_group_accessors(inherited => $STORE);
13  
14 sub parameterized_has_many {
15 11 ▶ my ($class, $rel, $f_source, $cond, $attrs) = @_;
Subroutine parameterized_has_many: called CC=10 SCAR=23.0
Pod documented
16  
17 11 ▶ die "Missing relation name for parameterized_has_many"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rel 1 10 11
18 unless defined $rel;
19 10 ▶ die "Missing foreign source"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $f_source 1 9 10
20 unless defined $f_source;
21  
22 {
23 9 my $cond_ref = ref($cond);
24 9 ▶ $cond_ref = 'non-reference value'
Branch: 1 branch
BranchTrueFalseTotal
unless $cond_ref 1 8 9
25 unless $cond_ref;
26 9 ▶ die "Condition needs to be [ \\\@args, \&code ], not ${cond_ref}"
Branch: 1 branch
BranchTrueFalseTotal
unless $cond_ref eq "ARRAY" 2 7 9
27 unless $cond_ref eq 'ARRAY';
28 }
29 7 my ($args, $code) = @$cond;
30  
31 {
32 7 my $arg_ref = ref($cond->[0]);
33 7 ▶ $arg_ref = 'non-reference value'
Branch: 1 branch
BranchTrueFalseTotal
unless $arg_ref 1 6 7
34 unless $arg_ref;
35 7 ▶ die "Arguments must be declared as array ref of names, not ${arg_ref}"
Branch: 1 branch
BranchTrueFalseTotal
unless $arg_ref eq "ARRAY" 2 5 7
36 unless $arg_ref eq 'ARRAY';
37 5 my $code_ref = ref($cond->[1]);
38 5 ▶ $code_ref = 'non-reference value'
Branch: 1 branch
BranchTrueFalseTotal
unless $code_ref 1 4 5
39 unless $code_ref;
40 5 ▶ die "Condition builder must be declared as code ref, not ${code_ref}"
Branch: 1 branch
BranchTrueFalseTotal
unless $code_ref eq "CODE" 2 3 5
41 unless $code_ref eq 'CODE';
42 }
43  
44 my $store = $class->$STORE({
45 3 ▶ %{$class->$STORE||{}},
Branch: 1 branch
BranchTrueFalseTotal
$class->$DBIx::Class::ParameterizedJoinHack::STORE or {} 1 2 3
46 $rel => { params => {}, args => $args },
47 3 })->{$rel};
48  
49 my $wrapped_code = sub {
50 10 ▶ my $params = $store->{params};
Subroutine __ANON__: called CC=2 SCAR=6.9
51 10 my @missing = grep !exists $params->{$_}, @$args;
52 10 ▶ die "Attempted to use parameterized rel ${rel} for ${class} without"
Branch: 1 branch
BranchTrueFalseTotal
if @missing 1 9 10
53 ." passing parameters ".join(', ', @missing) if @missing;
54 9 local *_ = $params;
55 9 &$code;
56 3 };
57  
58 3 $class->has_many($rel, $f_source, $wrapped_code, $attrs);
59 3 return; # no, you are not going to accidentally rely on a return value
60 }
61  
62 1 1;
63  
64 =head1 NAME
65  
66 DBIx::Class::ParameterizedJoinHack - Parameterized Relationship Joins
67  
68 =head1 SYNOPSIS
69  
70 #
71 # The Result class we want to allow to join with a dynamic
72 # condition.
73 #
74 package MySchema::Result::Person;
75 use base qw(DBIx::Class::Core);
76  
77 __PACKAGE__->load_components(qw(ParameterizedJoinHack));
78 __PACKAGE__->table('person');
79 __PACKAGE__->add_columns(
80 id => {
81 data_type => 'integer',
82 is_nullable => 0,
83 is_auto_increment => 1,
84 },
85 name => {
86 data_type => 'text',
87 is_nullable => 0,
88 }
89 );
90  
91 ...
92  
93 __PACKAGE__->parameterized_has_many(
94 priority_tasks => 'MySchema::Result::Task',
95 [['min_priority'] => sub {
96 my $args = shift;
97 return +{
98 "$args->{foreign_alias}.owner_id" => {
99 -ident => "$args->{self_alias}.id",
100 },
101 "$args->{foreign_alias}.priority" => {
102 '>=' => $_{min_priority},
103 },
104 };
105 }],
106 );
107  
108 1;
109  
110 #
111 # The ResultSet class belonging to your Result
112 #
113 package MySchema::ResultSet::Person;
114 use base qw(DBIx::Class::ResultSet);
115  
116 __PACKAGE__->load_components(qw(ResultSet::ParameterizedJoinHack));
117  
118 1;
119  
120 #
121 # A Result class to join against.
122 #
123 package MySchema::Result::Task;
124 use base qw(DBIx::Class::Core);
125
126 __PACKAGE__->table('task');
127 __PACKAGE__->add_columns(
128 id => {
129 data_type => 'integer',
130 is_nullable => 0,
131 is_auto_increment => 1,
132 },
133 owner_id => {
134 data_type => 'integer',
135 is_nullable => 0,
136 },
137 priority => {
138 data_type => 'integer',
139 is_nullable => 0,
140 },
141 );
142  
143 ...
144  
145 1;
146  
147 #
148 # Using the parameterized join.
149 #
150 my @urgent = MySchema
151 ->connect(...)
152 ->resultset('Person')
153 ->with_parameterized_join(
154 priority_tasks => {
155 min_priority => 300,
156 },
157 )
158 ->all;
159  
160 =head1 WARNING
161  
162 This module uses L<DBIx::Class> internals and may break at any time.
163  
164 =head1 DESCRIPTION
165  
166 This L<DBIx::Class> component allows to declare dynamically parameterized
167 has-many relationships.
168  
169 Add the component to your Result class as usual:
170  
171 __PACKAGE__->load_components(qw( ParameterizedJoinHack ));
172  
173 See L</parameterized_has_many> for details on declaring relations.
174  
175 See L<DBIx::Class::ResultSet::ParameterizedJoinHack> for ResultSet usage.
176  
177 B<Note:> Currently only L</parameterized_has_many> is implemented, since
178 it is the most requested use-case. However, adding support for other
179 relationship types is possible if a use-case is found.
180  
181 =head1 METHODS
182  
183 =head2 parameterized_has_many
184  
185 __PACKAGE__->parameterized_has_many(
186 $relation_name,
187 $foreign_source,
188 [\@join_arg_names, \&join_builder],
189 $attrs,
190 );
191  
192 The C<$relation_name>, C<$foreign_source>, and C<$attrs> are passed
193 through to C<has_many> as usual. The third argument is an array reference
194 containing an (array reference) list of argument names and a code
195 reference used to build the join conditions.
196  
197 The code reference will be called with the same arguments as if it had
198 been passed to C<has_many> directly, but the global C<%_> hash will
199 contain the named arguments for the join.
200  
201 See the L</SYNOPSIS> for an example of a definition.
202  
203 =head1 SPONSORS
204  
205 Development of this module was sponsored by
206  
207 =over
208  
209 =item * Ctrl O L<http://ctrlo.com>
210  
211 =back
212  
213 =head1 AUTHOR
214  
215 Matt S. Trout <mst@shadowcat.co.uk>
216  
217 =head1 CONTRIBUTORS
218  
219 Robert Sedlacek <r.sedlacek@shadowcat.co.uk>
220  
221 =head1 COPYRIGHT
222  
223 Copyright (c) 2015 the DBIx::Class::ParameterizedJoinHack L</AUTHOR> and L</CONTRIBUTORS>
224 as listed above.
225  
226 =head1 LICENSE
227  
228 This library is free software and may be distributed under the same terms
229 as perl itself.
↑ Summary ResultSet/ParameterizedJoinHack.pm »