sub has_a {
my ($class, $col, @rest) = @_;
$class->NEXT::ACTUAL::has_a(lc($col), @rest);
- $class->mk_group_accessors('has_a' => $col);
+ $class->mk_group_accessors('inflated_column' => $col);
return 1;
}
lc($f_key) ), @rest);
}
-sub get_has_a {
+sub get_inflated_column {
my ($class, $get, @rest) = @_;
- return $class->NEXT::ACTUAL::get_has_a(lc($get), @rest);
+ return $class->NEXT::ACTUAL::get_inflated_column(lc($get), @rest);
}
-sub store_has_a {
+sub store_inflated_column {
my ($class, $set, @rest) = @_;
- return $class->NEXT::ACTUAL::store_has_a(lc($set), @rest);
+ return $class->NEXT::ACTUAL::store_inflated_column(lc($set), @rest);
}
-sub set_has_a {
+sub set_inflated_column {
my ($class, $set, @rest) = @_;
- return $class->NEXT::ACTUAL::set_has_a(lc($set), @rest);
+ return $class->NEXT::ACTUAL::set_inflated_column(lc($set), @rest);
}
sub get_column {
$self->add_relationship($col, $f_class,
{ "foreign.${pri}" => "self.${col}" },
{ _type => 'has_a' } );
- $self->mk_group_accessors('has_a' => $col);
+ $self->inflate_column($col,
+ { inflate => sub {
+ my ($val, $self) = @_;
+ return ($self->search_related($col, {}, {}))[0]
+ || $f_class->new({ $pri => $val }); },
+ deflate => sub {
+ my ($val, $self) = @_;
+ $self->throw("$val isn't a $f_class") unless $val->isa($f_class);
+ return ($val->_ident_values)[0] } } );
return 1;
}
-sub get_has_a {
- my ($self, $rel) = @_;
- #warn $rel;
- #warn join(', ', %{$self->{_column_data}});
- return $self->{_relationship_data}{$rel}
- if $self->{_relationship_data}{$rel};
- return undef unless $self->get_column($rel);
- #my ($pri) = (keys %{$self->_relationships->{$rel}{class}->_primaries})[0];
- return $self->{_relationship_data}{$rel} =
- ($self->search_related($rel, {}, {}))[0]
- || do {
- my $f_class = $self->_relationships->{$rel}{class};
- my ($pri) = keys %{$f_class->_primaries};
- $f_class->new({ $pri => $self->get_column($rel) }); };
-}
-
-sub set_has_a {
- my ($self, $rel, @rest) = @_;
- my $ret = $self->store_has_a($rel, @rest);
- $self->{_dirty_columns}{$rel} = 1;
- return $ret;
-}
-
-sub store_has_a {
- my ($self, $rel, $obj) = @_;
- unless (ref $obj) {
- delete $self->{_relationship_data}{$rel};
- return $self->store_column($rel, $obj);
- }
- my $rel_obj = $self->_relationships->{$rel};
- $self->throw( "Can't set $rel: object $obj is not of class ".$rel_obj->{class} )
- unless $obj->isa($rel_obj->{class});
- $self->{_relationship_data}{$rel} = $obj;
- #warn "Storing $obj: ".($obj->_ident_values)[0];
- $self->store_column($rel, ($obj->_ident_values)[0]);
- return $obj;
-}
-
-sub new {
- my ($class, $attrs, @rest) = @_;
- my %hasa;
- foreach my $key (keys %$attrs) {
- my $rt = $class->_relationships->{$key}{attrs}{_type};
- next unless $rt && $rt eq 'has_a' && ref $attrs->{$key};
- $hasa{$key} = delete $attrs->{$key};
- }
- my $new = $class->NEXT::ACTUAL::new($attrs, @rest);
- foreach my $key (keys %hasa) {
- $new->store_has_a($key, $hasa{$key});
- }
- return $new;
-}
-
-sub _cond_value {
- my ($self, $attrs, $key, $value) = @_;
- if ( my $rel_obj = $self->_relationships->{$key} ) {
- my $rel_type = $rel_obj->{attrs}{_type} || '';
- if ($rel_type eq 'has_a' && ref $value) {
- $self->throw( "Object $value is not of class ".$rel_obj->{class} )
- unless $value->isa($rel_obj->{class});
- $value = ($value->_ident_values)[0];
- #warn $value;
- }
- }
- return $self->NEXT::ACTUAL::_cond_value($attrs, $key, $value);
-}
-
1;
sub _inflate_column_value {
my ($self, $col, $value) = @_;
+ return $value unless defined $value; # NULL is NULL is NULL
return $value unless exists $self->_columns->{$col}{_inflate_info}{inflate};
my $inflate = $self->_columns->{$col}{_inflate_info}{inflate};
return $inflate->($value, $self);
delete $self->{_inflated_column}{$col};
return $self->store_column($col, $obj);
}
+ my $deflated = $self->_deflate_column_value($col, $obj);
+ # Do this now so we don't store if it's invalid
$self->{_inflated_column}{$col} = $obj;
#warn "Storing $obj: ".($obj->_ident_values)[0];
- $self->store_column($col, $self->_deflate_column_value($col, $obj));
+ $self->store_column($col, $deflated);
return $obj;
}