File Coverage

blib/lib/Class/DBI/Relationship/HasA.pm
Criterion Covered Total %
statement 28 51 54.9
branch 3 30 10.0
condition 0 12 0.0
subroutine 8 10 80.0
pod 2 2 100.0
total 41 105 39.0


line stmt bran cond sub pod time code
1             package Class::DBI::Relationship::HasA;
2              
3 4     4   21 use strict;
  4         7  
  4         123  
4 4     4   19 use warnings;
  4         7  
  4         102  
5              
6 4     4   20 use base 'Class::DBI::Relationship';
  4         9  
  4         3331  
7              
8             sub remap_arguments {
9 3     3 1 9 my ($proto, $class, $want_col, $a_class, %meths) = @_;
10 3 50       11 $class->_invalid_object_method("has_a") if ref $class;
11 3 50       24 my $column = $class->find_column($want_col)
12             or return $class->_croak("Column $want_col does not exist in $class");
13 3 50       20 $class->_croak("$class $column needs an associated class") unless $a_class;
14 3         14 return ($class, $column, $a_class, \%meths);
15             }
16              
17             sub triggers {
18 3     3 1 5 my $self = shift;
19 3         10 $self->class->_require_class($self->foreign_class);
20 3         12149 my $column = $self->accessor;
21             return (
22 3         23 select => $self->_inflator,
23              
24             # after_create => $self->_inflator, # see t/6
25             "after_set_$column" => $self->_inflator,
26             deflate_for_create => $self->_deflator(1),
27             deflate_for_update => $self->_deflator,
28             );
29             }
30              
31             sub _inflator {
32 6     6   85 my $rel = shift;
33 6         18 my $col = $rel->accessor;
34             return sub {
35 0     0   0 my $self = shift;
36 0 0       0 defined(my $value = $self->_attrs($col)) or return;
37 0         0 my $meta = $self->meta_info($rel->name => $col);
38 0         0 my ($a_class, %meths) = ($meta->foreign_class, %{ $meta->args });
  0         0  
39              
40 0 0 0     0 return if ref $value and $value->isa($a_class);
41 0         0 my $inflator;
42              
43             my $get_new_value = sub {
44 0         0 my ($inflator, $value, $want_class, $obj) = @_;
45 0 0       0 my $new_value =
46             (ref $inflator eq 'CODE')
47             ? $inflator->($value, $obj)
48             : $want_class->$inflator($value);
49 0         0 return $new_value;
50 0         0 };
51              
52             # If we have a custom inflate ...
53 0 0       0 if (exists $meths{'inflate'}) {
54 0         0 $value = $get_new_value->($meths{'inflate'}, $value, $a_class, $self);
55 0 0 0     0 return $self->_attribute_store($col, $value)
56             if ref $value
57             and $value->isa($a_class);
58 0 0       0 $self->_croak("Inflate method didn't inflate right") if ref $value;
59             }
60              
61 0 0       0 return $self->_croak("Can't inflate $col to $a_class using '$value': "
62             . ref($value)
63             . " is not a $a_class")
64             if ref $value;
65              
66 0 0       0 $inflator = $a_class->isa('Class::DBI') ? "_simple_bless" : "new";
67 0         0 $value = $get_new_value->($inflator, $value, $a_class);
68              
69 0 0 0     0 return $self->_attribute_store($col, $value)
70             if ref $value
71             and $value->isa($a_class);
72              
73             # use ref as $obj may be overloaded and appear 'false'
74 0 0       0 return $self->_croak(
75             "Can't inflate $col to $a_class " . "via $inflator using '$value'")
76             unless ref $value;
77 6         68 };
78             }
79              
80             sub _deflator {
81 6     6   13 my ($self, $always) = @_;
82 6         16 my $col = $self->accessor;
83             return sub {
84 0     0   0 my $self = shift;
85 0 0       0 return unless $self->_attribute_exists($col);
86 0 0 0     0 $self->_attribute_store($col => $self->_deflated_column($col))
87             if ($always or $self->{__Changed}->{$col});
88 6         79 };
89             }
90              
91             sub _set_up_class_data {
92 3     3   7 my $self = shift;
93 3         68 $self->class->_extend_class_data(__hasa_rels => $self->accessor =>
94 3         17 [ $self->foreign_class, %{ $self->args } ]);
95 3         99 $self->SUPER::_set_up_class_data;
96             }
97              
98             1;