On Wed, May 5, 2021 at 8:35 AM Stéphane D'Alu <[email protected]>
wrote:

> I've a model for which I would like to have one of its  column value
> automatically converted to a more appropriate object, I'm doing something
> like that (the User class is not a sequel model)
>
> class InventoryItem < Model
>   def user
>     User.new(super)
>   end
>
>   def user=(v)
>     v.to_s
>   end
> end
>
> That doesn't seem the right way to do it, as every call to
> InventoryItem#user or InventoryItem#user= will perform the conversion which
> can be costly.
>
> I realized that I can avoid defining #user= by doing
> class User
>   def sql_literal(ds)
>     ds.literal(self.to_s)
>   end
> end
>
> But I'm still left clueless for InventoryItem#user
>

I'm guessing the serialization plugin may work for what you want.  The
deserializer would wrap the value in the User instance, and the serializer
would extract whatever part of the User instance you want stored in the
database.

The composition plugin could also fulfill the need, but you need to use a
different method than the database column method for accessing the composed
value in that case.

However, please understand I don't have a good understanding of what you
are trying to accomplish, so either of those suggestions may be off base.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/CADGZSSceiuU2pOfXQfKacGkztG2BvKXpPhrCR7kf6ukhGzMSAQ%40mail.gmail.com.

Reply via email to