File Coverage

blib/lib/Class/DBI/Frozen/301/Relationship/HasA.pm
Criterion Covered Total %
statement 30 53 56.6
branch 3 30 10.0
condition 0 12 0.0
subroutine 8 10 80.0
pod 0 2 0.0
total 41 107 38.3


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