On Monday 13 Feb 2012 16:47:04 Jeremy Evans wrote:
> On Feb 13, 8:16 am, Michael Gliwinski <Michael.Gliwin...@henderson-
> > I found I can just override getters on the module that just do
> > `super.strip' but since there are many such columns on different models,
> > this does not seem very optimal.  Is there a way to override getters en
> > masse (e.g. for a list of columns, or for columns of particular type)?
> 
> Assuming you are using models, this may work as a global setting:
> 
>   class Sequel::Model
>     def [](v)
>       case s = super
>       when String
>         s.strip
>       else
>         s
>       end
>     end
>   end

Brilliant, this does exactly what I need.

> > Also, I was searching through the list archives and found I could
> > probably (ab)use typecasting for that.  E.g. I've seen
> > typecast_value_string may be the place to do it, but I'm not sure if
> > this is a good idea?  If it is, what would be a good way to extend it?
> 
> Typecasting is only going to be used for model setter methods, so if a
> third party is inserting extra spaces, that's not the correct place to
> handle it.

OK, I didn't realize typecasting is only done on assignment.  Where is the 
data coming out of the DB cast to ruby types, is that done in the driver then?

> > Or would it be even better to do the transformations on database side,
> > e.g. replacing columns in select with e.g. `replace(tbl.col, ' ', '') as
> > col'?
> 
> That is another option.  You could also patch the Sequel adapter or
> driver to do the stripping.

Would that require custom SQL or is there maybe a way to tell the model to use 
something like that for certain columns?

> > Also a related question, if a database uses e.g. type char for some
> > columns, but they really are integers, and I'd prefer to work with them
> > as integers, what are my options?
> 
> If there aren't too many of such columns, replacing the columns with
> "cast(tbl.col AS integer) AS col" may work.  Alternatively, you could

Sorry for so many questions, I'm not that familiar with Sequel yet, would that 
require custom SQL for the entire dataset or is there a way to tell Sequel to 
use an SQL snippet for selecting a particular column?

> just override the accessor:
> 
>   class Foo < Sequel::Model
>     def col
>       s = super
>       Integer(s) if s
>     end
>   end

Yeah, there aren't that many of these ones in particular (char -> int), but in 
general this 3rd party app likes to use char for everything so I'm trying to 
find a fairly generic way to apply such transformations.

This would work OK though, so I'll try to find a way to maybe write a method 
that defines an accessors for a specified type and a list of columns, but I 
suppose this is more a ruby question so I won't bother you about that.

Many thanks for your help, much appreciated,
Michael


-- 
Michael Gliwinski
Henderson Group Information Services
9-11 Hightown Avenue, Newtownabby, BT36 4RT
Phone: 028 9034 3319

**********************************************************************************************
The information in this email is confidential and may be legally privileged.  
It is intended solely for the addressee and access to the email by anyone else 
is unauthorised.
If you are not the intended recipient, any disclosure, copying, distribution or 
any action taken or omitted to be taken in reliance on it, is prohibited and 
may be unlawful.
When addressed to our clients, any opinions or advice contained in this e-mail 
are subject to the terms and conditions expressed  in the governing client 
engagement leter or contract.
If you have received this email in error please notify 
[email protected]

John Henderson (Holdings) Ltd
Registered office: 9 Hightown Avenue, Mallusk, County Antrim, Northern Ireland, 
BT36 4RT.
Registered in Northern Ireland
Registration Number NI010588
Vat No.: 814 6399 12
*********************************************************************************

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en.

Reply via email to