1 package DBIx::Class::InflateColumn;
7 my ($self, $col, $attrs) = @_;
8 die "No such column $col to inflate" unless exists $self->_columns->{$col};
9 die "inflate_column needs attr hashref" unless ref $attrs eq 'HASH';
10 $self->_columns->{$col}{_inflate_info} = $attrs;
11 $self->mk_group_accessors('inflated_column' => $col);
15 sub _inflated_column {
16 my ($self, $col, $value) = @_;
17 return $value unless defined $value; # NULL is NULL is NULL
18 return $value unless exists $self->_columns->{$col}{_inflate_info};
19 return $value unless exists $self->_columns->{$col}{_inflate_info}{inflate};
20 my $inflate = $self->_columns->{$col}{_inflate_info}{inflate};
21 return $inflate->($value, $self);
24 sub _deflated_column {
25 my ($self, $col, $value) = @_;
26 return $value unless ref $value; # If it's not an object, don't touch it
27 return $value unless exists $self->_columns->{$col}{_inflate_info};
28 return $value unless exists $self->_columns->{$col}{_inflate_info}{deflate};
29 my $deflate = $self->_columns->{$col}{_inflate_info}{deflate};
30 return $deflate->($value, $self);
33 sub get_inflated_column {
34 my ($self, $col) = @_;
35 $self->throw("$col is not an inflated column") unless
36 exists $self->_columns->{$col}{_inflate_info};
38 return $self->{_inflated_column}{$col}
39 if exists $self->{_inflated_column}{$col};
40 return $self->{_inflated_column}{$col} =
41 $self->_inflated_column($col, $self->get_column($col));
44 sub set_inflated_column {
45 my ($self, $col, @rest) = @_;
46 my $ret = $self->store_inflated_column($col, @rest);
47 $self->{_dirty_columns}{$col} = 1;
51 sub store_inflated_column {
52 my ($self, $col, $obj) = @_;
54 delete $self->{_inflated_column}{$col};
55 return $self->store_column($col, $obj);
58 my $deflated = $self->_deflated_column($col, $obj);
59 # Do this now so we don't store if it's invalid
61 $self->{_inflated_column}{$col} = $obj;
62 #warn "Storing $obj: ".($obj->_ident_values)[0];
63 $self->store_column($col, $deflated);
68 my ($class, $attrs, @rest) = @_;
71 foreach my $key (keys %$attrs) {
72 if (exists $class->_columns->{$key}{_inflate_info}) {
73 $deflated{$key} = $class->_deflated_column($key,
74 delete $attrs->{$key});
77 return $class->NEXT::ACTUAL::new({ %$attrs, %deflated }, @rest);
81 my ($self, $attrs, $key, $value) = @_;
82 if (exists $self->_columns->{$key}) {
83 $value = $self->_deflated_column($key, $value);
85 return $self->NEXT::ACTUAL::_cond_value($attrs, $key, $value);