On Aug 24, 5:10 pm, rohit <[email protected]> wrote:
> The following code adds dataset methods to all models that have
> columns of type datetime:
>
> Sequel::Model.plugin :subclasses
>
> Sequel::Model.descendents.each {|m|
>   m.db_schema.each {|col, attr|
>     if attr[:type] == :datetime
>       method_name = (col.to_s.split('_').first + "_between").to_sym
>       m.def_dataset_method(method_name) {|from_date, to_date|
>         filter{(:created_at >= from_date) & (:created_at < to_date +
> 1)}
>       }
>     end
>   }
>
> }
>
> So you can write Artist.created_between(Date.today-5, Date.today),
> Artist.updated_between etc. if Artist has created_at, updated_at
> columns.
>
> I would like to package this code in a plugin but I am not sure how.
> Looked at the source of several plugins but none of them did anything
> similar. Would appreciate some advice on how to proceed.

The Sequel way to do this would be to override set_dataset in the
plugin's ClassMethods, call super, then check db_schema and add the
dataset methods at that point, in addition to checking the model that
loads the plugin (if it has a dataset).  If that makes sense, give it
a shot and let me know if you get stuck.

Jeremy

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