On Monday, July 22, 2013 10:38:45 AM UTC-7, Stéphane D'Alu wrote:
>
> I have a table with a uuid primary key, stored as a binary(16), so I
> created the following sequel model to manipulate human readable string
>
> class VM < Sequel::Model(:vm)
> def uuid=(v) ; super([v.delete('-')].pack('H32')) ; end
> def uuid ; super.unpack('H8H4H4H4H12').join('-') ; end
> end
>
> I would have expected VM["73ec5789-7fc8-4cbd-8c5f-2b662ff377a7"]
> to retrieve the corresponding vm, but it seems that I need to use the
> binary form for the uuid
>
> What would have been the proper way to define the model?
>
Since this is a filter condition, I don't think there's a model setting
that does what you want, . You either need to override the model methods
that you want to handle conversion, or add methods. If you just want your
given case handled:
def VM.[](*args)
if args.size == 1 && args.first.is_a?(String)
super(Sequel.blob([v.delete('-')].pack('H32')))
else
super
end
end
> Or what should I implement to work with the dataset and have automatic
> conversion?
>
You could have Dataset#literal_string_append recognize the UUID format and
treat it specially, but I would not recommend that approach. It's better
to do the conversion at higher levels where you know you are dealing with
UUIDs.
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 post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/groups/opt_out.