On Tuesday 14 Feb 2012 11:13:14 Michael Gliwinski wrote:
> On Monday 13 Feb 2012 16:47:04 Jeremy Evans wrote:
> > 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.

I've managed to come up with something, just sending it here in case it would 
be useful for somebody else in the future.

  module Sequel
    class Model
      def self.def_transforming_readers(*names, &block)
        if not block_given?
          cls = names.shift
          block = lambda {|v| cls.new(v)}
        end

        for name in names
          overridable_methods_module.module_eval {
            define_method(name) {block.call(super)}
          }
        end
      end
    end
  end

then in the model I do:

  class Foo < Sequel::Model
    def_transforming_readers(:bar, :baz) {|v| Integer(v)}
  end

thanks for your help Jeremy!


-- 
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