[Dbix-class] RFC: Inflating user-supplied values

Jason Kohles email at jasonkohles.com
Sun May 6 14:16:41 GMT 2007


So everybody probably saw my confusion with inflation from yesterday,  
and while it is all working now (thanks Claco!) I have to admit I  
still find the behavior counter-intuitive at best.  I guess when I  
was just inflating DateTime objects it made more sense, since the  
inflated values were so much more complex than the representation  
that was stored in the database, but when I started using  
DBIx::Class::InflateColumn::Currency, it seemed odd to me that  
'$10.00' would be inflated to a Data::Currency object if it came from  
the database, but not if it came from the user.  I've fixed this for  
my immediate need by writing an HTML::FormFu::Inflator subclass that  
parallels DBIx::Class::InflateColumn::Currency, but going forward I'd  
like to avoid repeating so much code, so I've come up with an idea to  
extend DBIx::Class::InflateColumn to allow user-provided values to be  
inflated the same way that database values are.

My initial idea is to simply add another key to the inflate_column  
method that would provide a method for determining if the supplied  
value could be inflated or not, something along these lines:

__PACKAGE__->inflate_column(
	'cost',
	inflate => sub {
		my ( $value, $obj ) = @_;
		return Data::Currency->new( $value, 'USD' );
	},
	deflate => sub {
		my ( $obj ) = @_;
		return $obj->value;
	},
	check => sub {
		my ( $value ) = @_;
		if ( $value =~ /^\s*\$/ || $value =~ / USD$/ ) { return 1 }
		return 0;
	},
);

Then when you do something like this:

$obj->cost( '$10.00' );

The check method would be run to determine if the value supplied  
should be inflated or not, and the inflator then run as needed on the  
value.

I haven't written any code to do this yet, although I have a pretty  
good idea where to start, I wanted to see what people thought about  
the concept first...

-- 
Jason Kohles
email at jasonkohles.com
http://www.jasonkohles.com/
"A witty saying proves nothing."  -- Voltaire





More information about the Dbix-class mailing list