Helper/Row/ProxyResultSetMethod.pm

filter: Statementstmt 95.2% 40 / 42 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 88.7% 63 / 71 CC 8 SCAR 8.0
CC
8
Cov
91%
proxy_resultset_method
4.1
__ANON__
2.5
id_plus_one
2.5
CRAP 2.2
« Helper/Schema/Verifier.pm ↑ Summary Helper/Row/OnColumnMissing.pm »
1 package DBIx::Class::Helper::Row::ProxyResultSetMethod;
2 54 $DBIx::Class::Helper::Row::ProxyResultSetMethod::VERSION = '2.032002';
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.0
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 54 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 # boo. The accessor checks that there's an actual column defined, so we
33 # skip it so we can cache results.
34 0 ▶ $_[0]->{_column_data}{$slot} = $_[0]->self_rs
Subroutine id_plus_two: not called CC=2 SCAR=9.2
Subroutine id_plus_one: called CC=2 SCAR=9.2
Branch: 2 branches
BranchTrueFalseTotal
unless $_[0]->has_column_loaded($slot) 0 0 0
unless $_[0]->has_column_loaded($slot) 3 2 5
35 ->search(undef, { columns => [] })
36 ->$rs_method
37 ->get_column($slot)
38 ->next unless $_[0]->has_column_loaded($slot);
39 0 return $_[0]->get_column($slot)
40 }
41 108 }
42  
43 sub copy {
44 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
45  
46 2 shift->next::method(@_);
47 }
48  
49 54 1;
50  
51 __END__
52  
53 =pod
54  
55 =head1 NAME
56  
57 DBIx::Class::Helper::Row::ProxyResultSetMethod - Efficiently reuse ResultSet methods from results with fallback
58  
59 =head1 SYNOPSIS
60  
61 ResultSet:
62  
63 package MyApp::Schema::ResultSet::Foo;
64  
65 use parent 'DBIx::Class::ResultSet';
66  
67 __PACKAGE__->load_components(qw(
68 Helper::ResultSet::CorrelateRelationship
69 ));
70  
71 ...;
72  
73 sub with_friend_count {
74 shift->search(undef, {
75 '+columns' => {
76 'friend_count' => $self->correlate('friends')->count_rs->as_query,
77 },
78 })
79 }
80  
81 Result:
82  
83 package MyApp::Schema::Result::Foo;
84  
85 use parent 'DBIx::Class::Core';
86  
87 __PACKAGE__->load_components(qw( Helper::Row::ProxyResultSetMethod ));
88  
89 __PACKAGE__->proxy_resultset_method('friend_count');
90  
91 or with L<DBIx::Class::Candy>:
92  
93 package MyApp::Schema::Result::Foo;
94  
95 use DBIx::Class::Candy -components => ['Helper::Row::ProxyResultSetMethod'];
96  
97 proxy_resultset_method 'friend_count';
98  
99 Elsewhere:
100  
101 my $row = $foo_rs->first;
102 say $row->friend_count . ' friends';
103  
104 =head1 DESCRIPTION
105  
106 This module makes reusing resultset methods from a result trivially easy. You
107 should be using it.
108  
109 =head1 METHODS
110  
111 =head2 proxy_resultset_method
112  
113 __PACKAGE__->proxy_resultset_method( $name => {
114 slot => $slot,
115 resultset_method => $rs_method
116 });
117  
118 C<proxy_resultset_method>'s first argument is the name of the method to
119 generate and is required. The other two arguments, C<$slot>, and
120 C<$resultset_method> are optional. If unspecified C<$slot> will default to
121 C<$name> and C<$resultset_method> will default to C<"with_$name">. C<$slot>
122 is the column that the data being retrieved is stored as in the ResultSet
123 method being proxied to. C<$resultset_method> is (duh) the ResultSet method
124 being proxied to.
125  
126 If you did not call the C<with_*> method on your ResultSet, and call the
127 proxy method, it will transparently B<fallback> and do the call and fetch
128 the needed data. E.g.:
129  
130 my $foo = $schema->resultset('Foo')->first; ## did not call with_friend_count
131 print $foo->friend_count; ## will produce desired result magically
132  
133 =head1 CANDY EXPORTS
134  
135 If used in conjunction with L<DBIx::Class::Candy> this component will export:
136  
137 =over
138  
139 =item proxy_resultset_method
140  
141 =back
142  
143 =head1 DEDICATION
144  
145 This module is dedicated to Ray Bradbury, who wrote Something Wicked This Way
146 Comes, Dandelion Wine, and numerous short stories, plays, etc etc. Read this
147 author's books.
148  
149 =head1 AUTHOR
150  
151 Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
152  
153 =head1 COPYRIGHT AND LICENSE
154  
155 This software is copyright (c) 2016 by Arthur Axel "fREW" Schmidt.
156  
157 This is free software; you can redistribute it and/or modify it under
158 the same terms as the Perl 5 programming language system itself.
159  
160 =cut
« Helper/Schema/Verifier.pm ↑ Summary Helper/Row/OnColumnMissing.pm »