File Coverage

blib/lib/Class/DBI/Relationship/MightHave.pm
Criterion Covered Total %
statement 9 42 21.4
branch 0 6 0.0
condition 0 9 0.0
subroutine 3 12 25.0
pod 3 3 100.0
total 15 72 20.8


line stmt bran cond sub pod time code
1             package Class::DBI::Relationship::MightHave;
2              
3 4     4   21 use strict;
  4         7  
  4         133  
4 4     4   20 use warnings;
  4         8  
  4         147  
5              
6 4     4   18 use base 'Class::DBI::Relationship';
  4         7  
  4         2375  
7              
8             sub remap_arguments {
9 0     0 1   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 1   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 1   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              
39             sub _object_accessor {
40 0     0     my $rel = shift;
41 0           my ($class, $method) = ($rel->class, $rel->accessor);
42             return sub {
43 0     0     my $self = shift;
44 0           my $meta = $class->meta_info($rel->name => $method);
45 0           my ($f_class, @extra) =
46 0           ($meta->foreign_class, @{ $meta->args->{import} });
47             return
48 0 0 0       if defined($self->{"_${method}_object"})
49             && $self->{"_${method}_object"}
50             ->isa('Class::DBI::Object::Has::Been::Deleted');
51 0   0       $self->{"_${method}_object"} ||= $f_class->retrieve($self->id);
52 0           };
53             }
54              
55             sub _imported_accessor {
56 0     0     my ($rel, $name) = @_;
57 0           my ($class, $method) = ($rel->class, $rel->accessor);
58             return sub {
59 0     0     my $self = shift;
60 0           my $meta = $class->meta_info($rel->name => $method);
61 0           my ($f_class, @extra) =
62 0           ($meta->foreign_class, @{ $meta->args->{import} });
63 0   0       my $for_obj = $self->$method() || do {
64             return unless @_; # just fetching
65             my $val = shift;
66             $f_class->insert(
67             { $f_class->primary_column => $self->id, $name => $val });
68             $self->$method();
69             };
70 0           $for_obj->$name(@_);
71 0           };
72             }
73              
74             1;