Thanks, serialization plugin looks like what I wanted.

I also need to export model value as a hash for further processing, is 
there a way to do that, as Model#values is left untouched by serialization?
Perhaps should I write a plugin that would do:  Hash[columns.map {|c| [c, 
get_column_value(c)]}]  ?

Oh, I see that json_serialization, will solve my problem, but I'm still 
curious on how to get something like Model#to_h

On Wednesday, 5 May 2021 at 18:57:33 UTC+2 Jeremy Evans wrote:

> 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/f1563322-f489-4ce5-8ff8-b981f714230fn%40googlegroups.com.

Reply via email to