File Coverage

blib/lib/Class/DBI/Frozen/301/Relationship/MightHave.pm
Criterion Covered Total %
statement 9 41 21.9
branch 0 4 0.0
condition 0 6 0.0
subroutine 3 12 25.0
pod 0 3 0.0
total 12 66 18.1


line stmt bran cond sub pod time code
1             package Class::DBI::Relationship::MightHave;
2              
3 24     24   137 use strict;
  24         45  
  24         898  
4 24     24   125 use warnings;
  24         44  
  24         699  
5              
6 24     24   120 use base 'Class::DBI::Relationship';
  24         41  
  24         27194  
7              
8             sub remap_arguments {
9 0     0 0   my ($proto, $class, $method, $f_class, @methods) = @_;
10 0           $class->_require_class($f_class);
11 0           return ($class, $method, $f_class, { import => \@methods });
12             }
13              
14             sub triggers {
15 0     0 0   my $self = shift;
16              
17 0           my $method = $self->accessor;
18              
19             return (
20             before_update => sub {
21 0 0   0     if (my $for_obj = shift->$method()) { $for_obj->update }
  0            
22             },
23              
24             before_delete => sub {
25 0 0   0     if (my $for_obj = shift->$method()) { $for_obj->delete }
  0            
26             },
27 0           );
28             }
29              
30             sub methods {
31 0     0 0   my $self = shift;
32 0           my ($class, $method) = ($self->class, $self->accessor);
33             return (
34 0           $method => $self->_object_accessor,
35 0           map { $_ => $self->_imported_accessor($_) } @{ $self->args->{import} });
  0            
36             }
37              
38             sub _object_accessor {
39 0     0     my $self = shift;
40 0           my ($class, $method) = ($self->class, $self->accessor);
41             return sub {
42 0     0     my $self = shift;
43 0           my $meta = $class->meta_info(might_have => $method);
44 0           my ($f_class, @extra) =
45 0           ($meta->foreign_class, @{ $meta->args->{import} });
46 0   0       $self->{"_${method}_object"} ||= $f_class->retrieve($self->id);
47 0           };
48             }
49              
50             sub _imported_accessor {
51 0     0     my ($self, $name) = @_;
52 0           my ($class, $method) = ($self->class, $self->accessor);
53             return sub {
54 0     0     my $self = shift;
55 0           my $meta = $class->meta_info(might_have => $method);
56 0           my ($f_class, @extra) =
57 0           ($meta->foreign_class, @{ $meta->args->{import} });
58 0   0       my $for_obj = $self->$method() || do {
59             my $val = shift or return; # just fetching
60             $f_class->create({ $f_class->primary_column => $self->id, $name => $val });
61             $self->$method();
62             };
63 0           $for_obj->$name(@_);
64 0           };
65             }
66              
67             1;