Re: Inflate a boolean

[prev] [thread] [next] [Date index for 2005/02/20]

From: Ofer Nave
Subject: Re: Inflate a boolean
Date: 21:40 on 20 Feb 2005
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)

Inflate a boolean
adtim 09:08 on 20 Feb 2005

Re: Inflate a boolean
Ofer Nave 09:22 on 20 Feb 2005

Re: Inflate a boolean
Tony Bowden 09:44 on 20 Feb 2005

Re: Inflate a boolean
a.d.tim 10:35 on 20 Feb 2005

Re: Inflate a boolean
Tony Bowden 11:15 on 20 Feb 2005

Re[2]: Inflate a boolean
a.d.tim 12:37 on 20 Feb 2005

Re: Inflate a boolean
Perrin Harkins 21:15 on 20 Feb 2005

Re: Inflate a boolean
Ofer Nave 21:40 on 20 Feb 2005

Re: Inflate a boolean
Tony Bowden 22:07 on 20 Feb 2005

Re: Inflate a boolean
Ofer Nave 22:24 on 20 Feb 2005

Re: Inflate a boolean
Johan Lindstrom 22:47 on 20 Feb 2005

Re: Inflate a boolean
Tony Bowden 23:35 on 20 Feb 2005

Re: Inflate a boolean
Johan Lindstrom 00:20 on 21 Feb 2005

Re: Inflate a boolean
Tony Bowden 09:31 on 21 Feb 2005

Re: Inflate a boolean
=?ISO-8859-1?Q?Ask_Bj=F8rn_Hansen?= 21:25 on 21 Feb 2005

Generated at 12:48 on 22 Feb 2005 by mariachi v0.52