my $val = $self->get_column($col);
- return $self->{_filtered_column}{$col} = $self->_column_from_storage($col, $val);
+ return $self->{_filtered_column}{$col} = $self->_column_from_storage(
+ $col, $val
+ );
}
sub get_column {
my ($self, $col) = @_;
if (exists $self->{_filtered_column}{$col}) {
- return $self->{_column_data}{$col} ||= $self->_column_to_storage ($col, $self->{_filtered_column}{$col});
+ return $self->{_column_data}{$col} ||= $self->_column_to_storage (
+ $col, $self->{_filtered_column}{$col}
+ );
}
return $self->next::method ($col);
my $self = shift;
foreach my $col (keys %{$self->{_filtered_column}||{}}) {
- $self->{_column_data}{$col} ||= $self->_column_to_storage ($col, $self->{_filtered_column}{$col})
- if exists $self->{_filtered_column}{$col};
+ $self->{_column_data}{$col} ||= $self->_column_to_storage (
+ $col, $self->{_filtered_column}{$col}
+ ) if exists $self->{_filtered_column}{$col};
}
$self->next::method (@_);
}
sub update {
- my ($self, $attrs, @rest) = @_;
+ my ($self, $data, @rest) = @_;
- foreach my $key (keys %{$attrs||{}}) {
+ foreach my $col (keys %{$data||{}}) {
if (
- $self->has_column($key)
+ $self->has_column($col)
&&
- exists $self->column_info($key)->{_filter_info}
+ exists $self->column_info($col)->{_filter_info}
) {
- $self->set_filtered_column($key, delete $attrs->{$key});
+ $self->set_filtered_column($col, delete $data->{$col});
# FIXME update() reaches directly into the object-hash
# and we may *not* have a filtered value there - thus
# the void-ctx filter-trigger
- $self->get_column($key) unless exists $self->{_column_data}{$key};
+ $self->get_column($col) unless exists $self->{_column_data}{$col};
}
}
- return $self->next::method($attrs, @rest);
+ return $self->next::method($data, @rest);
}
sub new {
- my ($class, $attrs, @rest) = @_;
- my $source = $attrs->{-result_source}
+ my ($class, $data, @rest) = @_;
+ my $source = $data->{-result_source}
or $class->throw_exception('Sourceless rows are not supported with DBIx::Class::FilterColumn');
- my $obj = $class->next::method($attrs, @rest);
- foreach my $key (keys %{$attrs||{}}) {
- if ($obj->has_column($key) &&
- exists $obj->column_info($key)->{_filter_info} ) {
- $obj->set_filtered_column($key, $attrs->{$key});
+ my $obj = $class->next::method($data, @rest);
+
+ foreach my $col (keys %{$data||{}}) {
+ if ($obj->has_column($col) &&
+ exists $obj->column_info($col)->{_filter_info} ) {
+ $obj->set_filtered_column($col, $data->{$col});
}
}
return $value unless exists $info->{_inflate_info};
- my $inflate = $info->{_inflate_info}{inflate};
- $self->throw_exception("No inflator for $col") unless defined $inflate;
-
- return $inflate->($value, $self);
+ return (
+ $info->{_inflate_info}{inflate}
+ ||
+ $self->throw_exception("No inflator found for '$col'")
+ )->($value, $self);
}
sub _deflated_column {
return $value unless exists $info->{_inflate_info};
- my $deflate = $info->{_inflate_info}{deflate};
- $self->throw_exception("No deflator for $col") unless defined $deflate;
-
- return $deflate->($value, $self);
+ return (
+ $info->{_inflate_info}{deflate}
+ ||
+ $self->throw_exception("No deflator found for '$col'")
+ )->($value, $self);
}
=head2 get_inflated_column
sub get_inflated_column {
my ($self, $col) = @_;
+
$self->throw_exception("$col is not an inflated column")
unless exists $self->column_info($col)->{_inflate_info};
+
+ # we take care of keeping things in sync
return $self->{_inflated_column}{$col}
if exists $self->{_inflated_column}{$col};
=cut
sub set_inflated_column {
- my ($self, $col, $inflated) = @_;
- $self->set_column($col, $self->_deflated_column($col, $inflated));
+ my ($self, $col, $value) = @_;
+
+ $self->set_column($col, $self->_deflated_column($col, $value));
- if (length ref $inflated and ! is_literal_value($inflated) ) {
- $self->{_inflated_column}{$col} = $inflated;
+ if (length ref $value and ! is_literal_value($value) ) {
+ $self->{_inflated_column}{$col} = $value;
} else {
delete $self->{_inflated_column}{$col};
}
- return $inflated;
+ return $value;
}
=head2 store_inflated_column
=cut
sub store_inflated_column {
- my ($self, $col, $inflated) = @_;
+ my ($self, $col, $value) = @_;
- if (is_literal_value($inflated)) {
+ if (is_literal_value($value)) {
delete $self->{_inflated_column}{$col};
- $self->store_column($col => $inflated);
+ $self->store_column($col => $value);
}
else {
delete $self->{_column_data}{$col};
- $self->{_inflated_column}{$col} = $inflated;
+ $self->{_inflated_column}{$col} = $value;
}
- return $inflated;
+ return $value;
}
=head1 SEE ALSO
sub get_column {
my ($self, $column) = @_;
$self->throw_exception( "Can't fetch data as class method" ) unless ref $self;
- return $self->{_column_data}{$column} if exists $self->{_column_data}{$column};
+
+ return $self->{_column_data}{$column}
+ if exists $self->{_column_data}{$column};
+
if (exists $self->{_inflated_column}{$column}) {
- return $self->store_column($column,
- $self->_deflated_column($column, $self->{_inflated_column}{$column}));
+ # deflate+return cycle
+ return $self->store_column($column, $self->_deflated_column(
+ $column, $self->{_inflated_column}{$column}
+ ));
}
- $self->throw_exception( "No such column '${column}'" ) unless $self->has_column($column);
+
+ $self->throw_exception( "No such column '${column}'" )
+ unless $self->has_column($column);
+
return undef;
}
sub has_column_loaded {
my ($self, $column) = @_;
$self->throw_exception( "Can't call has_column data as class method" ) unless ref $self;
- return 1 if exists $self->{_inflated_column}{$column};
- return exists $self->{_column_data}{$column};
+
+ return (
+ exists $self->{_inflated_column}{$column}
+ or
+ exists $self->{_column_data}{$column}
+ ) ? 1 : 0;
}
=head2 get_columns
sub get_columns {
my $self = shift;
if (exists $self->{_inflated_column}) {
+ # deflate cycle for each inflation, including filter rels
foreach my $col (keys %{$self->{_inflated_column}}) {
unless (exists $self->{_column_data}{$col}) {
my ($self, $column, $new_value) = @_;
my $had_value = $self->has_column_loaded($column);
- my ($old_value, $in_storage) = ($self->get_column($column), $self->in_storage)
- if $had_value;
+ my $old_value = $self->get_column($column);
$new_value = $self->store_column($column, $new_value);
my $dirty =
$self->{_dirty_columns}{$column}
||
- $in_storage # no point tracking dirtyness on uninserted data
+ $self->in_storage # no point tracking dirtyness on uninserted data
? ! $self->_eq_column_values ($column, $old_value, $new_value)
: 1
;
$had_value
and
# no storage - no storage-value
- $in_storage
+ $self->in_storage
and
# no value already stored (multiple changes before commit to storage)
! exists $self->{_column_data_in_storage}{$column}
if (ref $upd->{$key}) {
my $info = $self->relationship_info($key);
my $acc_type = $info->{attrs}{accessor} || '';
+
if ($acc_type eq 'single') {
my $rel_obj = delete $upd->{$key};
$self->set_from_related($key => $rel_obj);
my $undef_artist_cd = $schema->resultset("CD")->new_result({ 'title' => 'badgers', 'year' => 2007 });
-is($undef_artist_cd->has_column_loaded('artist'), '', 'FK not loaded');
+ok(! $undef_artist_cd->has_column_loaded('artist'), 'FK not loaded');
is($undef_artist_cd->search_related('artist')->count, 0, '0=1 search when FK does not exist and object not yet in db');
lives_ok {
$undef_artist_cd->related_resultset('artist')->new({name => 'foo'});