File Coverage

blib/lib/DBIx/Class/Relationship/ProxyMethods.pm
Criterion Covered Total %
statement 31 31 100.0
branch 10 10 100.0
condition n/a
subroutine 8 8 100.0
pod 0 2 0.0
total 49 51 96.0


line stmt bran cond sub pod time code
1             package # hide from PAUSE
2             DBIx::Class::Relationship::ProxyMethods;
3              
4 312     312   99292 use strict;
  312         766  
  312         8604  
5 312     312   1674 use warnings;
  312         675  
  312         8102  
6 312     312   1687 use base 'DBIx::Class';
  312         660  
  312         36704  
7 312     312   2055 use DBIx::Class::_Util 'quote_sub';
  312         664  
  312         13675  
8 312     312   1858 use namespace::clean;
  312         843  
  312         1767  
9              
10             our %_pod_inherit_config =
11             (
12             class_map => { 'DBIx::Class::Relationship::ProxyMethods' => 'DBIx::Class::Relationship' }
13             );
14              
15             sub register_relationship {
16 23705     23705 0 374747 my ($class, $rel, $info) = @_;
17 23705 100       89615 if (my $proxy_args = $info->{attrs}{proxy}) {
18 1576         17322 $class->proxy_to_related($rel, $proxy_args);
19             }
20 23703         767624 $class->next::method($rel, $info);
21             }
22              
23             sub proxy_to_related {
24 1576     1576 0 5085 my ($class, $rel, $proxy_args) = @_;
25 1576         12659 my %proxy_map = $class->_build_proxy_map_from($proxy_args);
26              
27 1574         7903 my @qsub_args = ( {}, {
28             attributes => [qw(
29             DBIC_method_is_proxy_to_relationship
30             DBIC_method_is_generated_from_resultsource_metadata
31             )],
32             } );
33              
34             quote_sub "${class}::$_", sprintf( <<'EOC', $rel, $proxy_map{$_} ), @qsub_args
35             my $self = shift;
36             my $relobj = $self->%1$s;
37             if (@_ && !defined $relobj) {
38             $relobj = $self->create_related( q{%1$s} => { %2$s => $_[0] } );
39             @_ = ();
40             }
41             $relobj ? $relobj->%2$s(@_) : undef;
42             EOC
43 1574         19196 for keys %proxy_map
44             }
45              
46             sub _build_proxy_map_from {
47 1576     1576   4710 my ( $class, $proxy_arg ) = @_;
48 1576         4291 my $ref = ref $proxy_arg;
49              
50 1576 100       7678 if ($ref eq 'HASH') {
    100          
    100          
51 787         4680 return %$proxy_arg;
52             }
53             elsif ($ref eq 'ARRAY') {
54             return map {
55 527 100       1815 (ref $_ eq 'HASH')
  785         5051  
56             ? (%$_)
57             : ($_ => $_)
58             } @$proxy_arg;
59             }
60             elsif ($ref) {
61 2         18 $class->throw_exception("Unable to process the 'proxy' argument $proxy_arg");
62             }
63             else {
64 260         1268 return ( $proxy_arg => $proxy_arg );
65             }
66             }
67              
68             1;