On Oct 15, 7:44 pm, Nate Wiger <[email protected]> wrote: > So this isn't really supported by other ORM's and I can't find a match > in the Sequel rdoc for validations, so it's probably not supported. > But maybe there's a way to accomplish this. > > One thing I've always wanted is class-level validation hooks that can > specify an arbitrary method name to run the validation :on. For > example some pseudo-AR-code: > > class UserCreation < AR::Base > validates_presence_of :page, :on => [:search, :list] > validates_presence_of :per_page, :on => [:search, :list] > validates_presence_of :search_string, :on => :search > end > > Then: > > UserCreation.list(params[:list]) > UserCreation.search(params[:search])
You could do this with a model plugin. In ClassMethods in the plugin, add the validation methods you want. Have the validation methods add a module included in the class (if it doesn't already exist), and add the methods to the module (call super unless things are invalid). There are some other plugins that do similar things, see the lazy_attributes plugin for an example. > Now, I know in Sequel you can just do this via instance methods/etc. > The reason class methods have alot of value for my app is we do alot > of reflection on our XML/JSON services side to basically self-document > our API (eg, we generate a RelaxNG structure that shows params, > request method, response structure, etc). So this way we can say > > UserCreation.validations_for(:search).each.etc.etc > > (Currently we have a custom plugin for AR that we run locally to > accomplish this.) > > Thoughts? If similar functionality already exists in Sequel, great, > otherwise Jeremy are you open to me extending the > validation_class_methods plugin to accomplish this? > > class MyClass < Sequel::Model > validates :on => [:search, :list] do > numericality_of :page > numericality_of :per_page > end > > validates :on => :search do > length_of :search_string > end > end > > I can also monkey-patch locally, I just like to feed stuff back when I > can. I think this would be best as a separate plugin, since it operates very differently from the validation_class_methods plugin (which just overrides the validate method). I'm not sure of the general utility of this approach (though I'm sure it works well for your app), so I don't think it should be shipped with Sequel. I'm not completely opposed to shipping it with Sequel, but I'd need some positive feedback from multiple members of the community before making that decision. However, if you create any open source Sequel plugins that you want to share, please send me a link and I'll include it in the plugins page. External plugin usage is no different from internal plugin usage from a user perspective, the only advantage to the internal plugins is that I test them with the current code before pushing to github. 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 -~----------~----~----~----~------~----~------~--~---
