[Dbix-class] dirty-flag on inflated columns

Marc Mims marc at questright.com
Thu May 14 19:51:02 GMT 2009


* Matt S Trout <dbix-class at trout.me.uk> [090514 08:49]:
> On Wed, May 13, 2009 at 12:10:18PM -0700, Marc Mims wrote:
> > Ok.  So, now how to fix it?
> > 
> > I need a unique filename to deflate to, but I only want to create it
> > once.  Rather than using the deflated value {_column_data}{$column}, I
> > can create {_fs_column_filename}{$column} and use it, instead.  I need
> > to populate {_fs_column_filename} when a row is read from the db.  Looks
> > like I can do that by extending inflate_result.
> 
> Hmm. Maybe you need to generate an extra accessor group so you can tag the
> Path::Class or whatever object on the way in?

I'm not sure what you have in mind, here.  I'll poke you on IRC if the
following is off target.

I refactored my code based on the idea I outlined (quoted above).  It
passes all the existing tests, plus one I added to simulate the proposed
change to make_column_dirty:

    $book->make_column_dirty('cover_image');
    delete $book->{_column_data}{cover_image};
    $book->update;

I have attached an svn diff, but it's a bit difficult to read.  So, here
are the key methods I've overridden.  Does this look like a sane
approach?

    sub inflate_result {
	my ($class, $source, $me, $prefetch) = @_;

	my $new = $class->next::method($source, $me, $prefetch);
	
	while ( my($column, $data) = each %{$new->{_column_data}} ) {
	    if ( $source->column_info($column)->{is_fs_column} && defined $data ) {
		$new->{_fs_column_filename}{$column} = $data;
	    }
	}
	
	return $new;
    }


    sub set_column {
	my ($self, $column, $new_value) = @_;

        #Deletes file storage when an fs_column is set to undef.
	if ( !defined $new_value && $self->result_source->column_info($column)->{is_fs_column}
		&& $self->{_fs_column_filename}{$column} ) {
	    $self->_fs_column_storage($column)->remove;
	    delete $self->{_fs_column_filename}{$column};
	}

	return $self->next::method($column, $new_value);
    }

    sub set_inflated_column {
	my ($self, $column, $inflated) = @_;

	$self->next::method($column, $inflated);

	# reinflate
	if ( defined $inflated && ref $inflated && ref $inflated ne 'SCALAR'
		&& $self->result_source->column_info($column)->{is_fs_column} ) {
	    $inflated = $self->{_inflated_column}{$column} = $self->_fs_column_storage($column);
	}
	return $inflated;
    }


	-Marc
-------------- next part --------------
A non-text attachment was scrubbed...
Name: fs_columns.diff
Type: text/x-diff
Size: 9662 bytes
Desc: not available
Url : http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20090514/7bc0f486/fs_columns.bin


More information about the DBIx-Class mailing list