On Tuesday, July 28, 2015 at 7:54:34 PM UTC-7, Steven Ringo wrote:
>
> Hi,
>
> I have a PostgreSQL table-returning function:
>
> e.g. matched_devices(device_id uuid, tolerance integer, num_matches 
> integer);
>
> I would like to use the dataset of the results from calling this in a 
> Sequel::Model, e.g.
>
> class MatchedDevices < Sequel::Model
>
>   def is_faulty?
>     self[:faulty]
>   end
>
> end
>
> I can get a dataset using 
> MatchedDevices.from{matched_devices('f165fd3b-9dfd-422a-9a73-9aea9c981fef', 
> 10, 0)}, and I can use the results of the recordset within the model as 
> hash values (per is_faulty?). 
>
> Ideally I would like to have each value accessible as a regular 
> property/method, as one would get if this model were backed by a regular 
> table.
>
> If anyone can help me, that would be awesome.
>

You can use def_column_alias to automatically create methods.  In this 
case, the method name is the same as the column name, so something like 
this should work:

class MatchedDevices
  [:faulty, ...].each do |m|
    def_column_alias m, m
  end
end

Then you can do:

m 
= MatchedDevices.from{matched_devices('f165fd3b-9dfd-422a-9a73-9aea9c981fef', 
10, 0)}.first
m.faulty

This assumes that matched_devices always returns the same columns.  If that 
isn't the case, and it can return arbitrary columns, you'll probably need 
to use method_missing.

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/d/optout.

Reply via email to