Helper/Row/ProxyResultSetMethod.pm

filter: Statementstmt 89.5% 43 / 48 Branchbran 75.0% 6 / 8 Conditioncond 80.0% 4 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 90.9% 10 / 11 Podpod 100% 2 / 2 totaltotal 85.7% 66 / 77 CC 8 SCAR 7.8
CC
8
Cov
87%
proxy_resultset_method
4.2
__ANON__
2.3
id_plus_one
2.3
CRAP 2.2
« Helper/Row/OnColumnMissing.pm ↑ Summary Helper/ResultSet/Random.pm »
1 package DBIx::Class::Helper::Row::ProxyResultSetMethod;
2 54 $DBIx::Class::Helper::Row::ProxyResultSetMethod::VERSION = '2.033001';
3 # ABSTRACT: Efficiently reuse ResultSet methods from results with fallback
4  
5 54 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 54 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 54 ▶use parent 'DBIx::Class::Helper::Row::SelfResultSet', 'DBIx::Class::Row';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 54 ▶use Sub::Name ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 54 ▶use DBIx::Class::Candy::Exports;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 54 export_methods [qw( proxy_resultset_method )];
15  
16 54 __PACKAGE__->mk_group_accessors(inherited => '_proxy_slots');
17  
18 sub proxy_resultset_method {
19 108 ▶ my ($self, $name, $attr) = @_;
Subroutine proxy_resultset_method: called CC=4 SCAR=14.3
Pod documented
20  
21 108 ▶ my $rs_method = $attr->{resultset_method} || "with_$name";
Condition100%2/2
$attr->{'resultset_method'} || "with_$name"
l!l
5454
MC/DC100%1/1
$attr->{'resultset_method'} || "with_$name"
$attr->{'resultset_method'}
Truth table100%2/2
A
A= $attr->{'resultset_method'}
Aresult
11
00
22 108 ▶ my $slot = $attr->{slot} || $name;
Condition66%2/3
$attr->{'slot'} || $name
l!l&&r!l&&!r
54540
MC/DC0%0/2
$attr->{'slot'} || $name
$attr->{'slot'} $name
Truth table66%2/3
A || B
A= $attr->{'slot'}B= $name
ABresult
1X1
011
000
23  
24 108 ▶ $self->_proxy_slots([]) unless $self->_proxy_slots;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_proxy_slots 54 54 108
25 108 push @{$self->_proxy_slots}, $slot;
26  
27 54 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 108 my $method = $self . '::' . $name;
29 108 *{$method} = Sub::Name::subname $method, sub {
30 0 ▶ my ($self) = @_;
Subroutine id_plus_one: not called CC=2 SCAR=8.2
Subroutine id_plus_two: called CC=2 SCAR=8.2
31 54 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 0 ▶ unless ($self->has_column_loaded($slot)) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($self->has_column_loaded($slot)) 0 0 0
unless ($self->has_column_loaded($slot)) 3 2 5
34 # boo. The accessor checks that there's an actual column defined, so we
35 # skip it so we can cache results.
36 0 $self->{_column_data}{$slot} = undef;
37 0 $self->set_column(
38 $slot,
39 $self->self_rs
40 ->search(undef, { columns => [] })
41 ->$rs_method
42 ->get_column($slot)
43 ->next,
44 );
45 }
46  
47 0 return $self->get_column($slot)
48 }
49 108 }
50  
51 sub copy {
52 2 ▶ delete local @{$_[0]->{_column_data}}{@{$_[0]->_proxy_slots||[]}};
Subroutine copy: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_proxy_slots or [] 1 1 2
53  
54 2 shift->next::method(@_);
55 }
56  
57 54 1;
58  
59 __END__
60  
61 =pod
62  
63 =head1 NAME
64  
65 DBIx::Class::Helper::Row::ProxyResultSetMethod - Efficiently reuse ResultSet methods from results with fallback
66  
67 =head1 SYNOPSIS
68  
69 ResultSet:
70  
71 package MyApp::Schema::ResultSet::Foo;
72  
73 use parent 'DBIx::Class::ResultSet';
74  
75 __PACKAGE__->load_components(qw(
76 Helper::ResultSet::CorrelateRelationship
77 ));
78  
79 ...;
80  
81 sub with_friend_count {
82 shift->search(undef, {
83 '+columns' => {
84 'friend_count' => $self->correlate('friends')->count_rs->as_query,
85 },
86 })
87 }
88  
89 Result:
90  
91 package MyApp::Schema::Result::Foo;
92  
93 use parent 'DBIx::Class::Core';
94  
95 __PACKAGE__->load_components(qw( Helper::Row::ProxyResultSetMethod ));
96  
97 __PACKAGE__->proxy_resultset_method('friend_count');
98  
99 or with L<DBIx::Class::Candy>:
100  
101 package MyApp::Schema::Result::Foo;
102  
103 use DBIx::Class::Candy -components => ['Helper::Row::ProxyResultSetMethod'];
104  
105 proxy_resultset_method 'friend_count';
106  
107 Elsewhere:
108  
109 my $row = $foo_rs->first;
110 say $row->friend_count . ' friends';
111  
112 =head1 DESCRIPTION
113  
114 This module makes reusing resultset methods from a result trivially easy. You
115 should be using it.
116  
117 =head1 METHODS
118  
119 =head2 proxy_resultset_method
120  
121 __PACKAGE__->proxy_resultset_method( $name => {
122 slot => $slot,
123 resultset_method => $rs_method
124 });
125  
126 C<proxy_resultset_method>'s first argument is the name of the method to
127 generate and is required. The other two arguments, C<$slot>, and
128 C<$resultset_method> are optional. If unspecified C<$slot> will default to
129 C<$name> and C<$resultset_method> will default to C<"with_$name">. C<$slot>
130 is the column that the data being retrieved is stored as in the ResultSet
131 method being proxied to. C<$resultset_method> is (duh) the ResultSet method
132 being proxied to.
133  
134 If you did not call the C<with_*> method on your ResultSet, and call the
135 proxy method, it will transparently B<fallback> and do the call and fetch
136 the needed data. E.g.:
137  
138 my $foo = $schema->resultset('Foo')->first; ## did not call with_friend_count
139 print $foo->friend_count; ## will produce desired result magically
140  
141 =head1 CANDY EXPORTS
142  
143 If used in conjunction with L<DBIx::Class::Candy> this component will export:
144  
145 =over
146  
147 =item proxy_resultset_method
148  
149 =back
150  
151 =head1 DEDICATION
152  
153 This module is dedicated to Ray Bradbury, who wrote Something Wicked This Way
154 Comes, Dandelion Wine, and numerous short stories, plays, etc etc. Read this
155 author's books.
156  
157 =head1 AUTHOR
158  
159 Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
160  
161 =head1 COPYRIGHT AND LICENSE
162  
163 This software is copyright (c) 2016 by Arthur Axel "fREW" Schmidt.
164  
165 This is free software; you can redistribute it and/or modify it under
166 the same terms as the Perl 5 programming language system itself.
167  
168 =cut
« Helper/Row/OnColumnMissing.pm ↑ Summary Helper/ResultSet/Random.pm »