DBIx-Class

 view release on metacpan or  search on metacpan

lib/DBIx/Class/FilterColumn.pm  view on Meta::CPAN

  ) for grep
    { ! exists $self->{_column_data}{$_} }
    keys %{$self->{_filtered_column}||{}}
  ;

  $self->next::method (@_);
}

# and *another* separate codepath, argh!
sub get_dirty_columns {
  my $self = shift;

  $self->{_dirty_columns}{$_}
    and
  ! exists $self->{_column_data}{$_}
    and
  $self->{_column_data}{$_} = $self->_column_to_storage (
    $_, $self->{_filtered_column}{$_}
  )
    for keys %{$self->{_filtered_column}||{}};

  $self->next::method(@_);
}

sub store_column {
  my ($self, $col) = (shift, @_);

  # blow cache
  delete $self->{_filtered_column}{$col};

  $self->next::method(@_);
}

sub has_column_loaded {
  my ($self, $col) = @_;
  return 1 if exists $self->{_filtered_column}{$col};
  return $self->next::method($col);
}

sub set_filtered_column {
  my ($self, $col, $filtered) = @_;

  # unlike IC, FC does not need to deal with the 'filter' abomination
  # thus we can short-curcuit filtering entirely and never call set_column
  # in case this is already a dirty change OR the row never touched storage
  if (
    ! $self->in_storage
      or
    $self->is_column_changed($col)
  ) {
    $self->make_column_dirty($col);
    delete $self->{_column_data}{$col};
  }
  else {
    $self->set_column($col, $self->_column_to_storage($col, $filtered));
  };

  return $self->{_filtered_column}{$col} = $filtered;
}

sub update {
  my ($self, $data, @rest) = @_;

  my $colinfos = $self->result_source->columns_info;

  foreach my $col (keys %{$data||{}}) {
    if ( exists $colinfos->{$col}{_filter_info} ) {
      $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($col) unless exists $self->{_column_data}{$col};
    }
  }

  return $self->next::method($data, @rest);
}

sub new {
  my ($class, $data, @rest) = @_;

  my $rsrc = $data->{-result_source}
    or $class->throw_exception('Sourceless rows are not supported with DBIx::Class::FilterColumn');

  my $obj = $class->next::method($data, @rest);

  my $colinfos = $rsrc->columns_info;

  foreach my $col (keys %{$data||{}}) {
    if (exists $colinfos->{$col}{_filter_info} ) {
      $obj->set_filtered_column($col, $data->{$col});
    }
  }

  return $obj;
}

1;

__END__

=head1 NAME

DBIx::Class::FilterColumn - Automatically convert column data

=head1 SYNOPSIS

In your Schema or DB class add "FilterColumn" to the top of the component list.

  __PACKAGE__->load_components(qw( FilterColumn ... ));

Set up filters for the columns you want to convert.

 __PACKAGE__->filter_column( money => {
     filter_to_storage => 'to_pennies',
     filter_from_storage => 'from_pennies',
 });

 sub to_pennies   { $_[1] * 100 }



( run in 0.511 second using v1.01-cache-2.11-cpan-39bf76dae61 )