Re: Inflate a boolean
[prev]
[thread]
[next]
[Date index for 2005/02/20]
Perrin Harkins wrote:
> a.d.tim wrote:
>
>> Restating my question: is there any simple
>> way to preprocess data, after getting it from db?
>
>
> Override the accessor:
> http://class-dbi.com/cgi-bin/wiki/index.cgi?OverridingAutogeneratedAccessors
>
>
> - Perrin
Let me see if I can state this in a comprehensive, generic way:
---
Given the following schema (in MySQL DDL):
CREATE TABLE quickie_mart (
quickie_mart_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
zip_code VARCHAR(10) NOT NULL,
offers_slushie TINYINT NOT NULL, # 1 = yes, 0 = no
PRIMARY KEY( quickie_mart_id )
);
1) If you want to change the value in a column as it is retrieved from
the database, you should use the inflate/deflate technique with the
'has_a' method. You can then convert a simple value to an object, and
then make that object stringify however you like.
package SlushieBool;
use overload ( '""' => sub { my $self = shift; return $self->{value} ?
'Yes, Mr. Simpson' : 'No, Mr. Simpson' } );
sub new {
my ( $class, $value ) = @_;
return bless( { value => $value }, $class );
}
package QuickieMart;
__PACKAGE__->has_a( offers_slushie => 'SlushieBool' );
package main;
my ( $local_quickie_mart ) = QuickieMart->search( zip_code => '90064' );
print "Apu, do you have slushies?\n";
print $local_quickie_mart->offers_slushie(), "\n";
2) If you want to change the value that is returned from an accessor
without changing the value in a column, you should define your own
accessor function, which will also prevent it from being autogenerated.
package QuickieMart;
sub offers_slushie {
my ( $self, $new_value ) = @_;
$new_value = $new_value =~ /^yes/io ? 1 : 0 if ( defined $new_value );
return $self->_offers_slushie_accessor( $new_value ) ? 'Yes, Mr.
Simpson' : 'No, Mr. Simpson';
}
---
However, I think a.d.tim is right in that both of those are overly
complex if all you want to do is filter data as it travels between the
database and your CDBI object, without having to create an object
wrapper around it. Not every column justifies a new class. :)
Is it possible to use 'has_a' to write a simple filter to convert 1/0 to
'yes'/'no' on load, and 'yes'/'no' to 1/0 on save?
-ofer
|
(message missing)
|