I am pleased to announce the release of English Like Queries [pronounced in a proper British accent] v0.0.2 Changes: klass.where 'name includes' => ['abc', 'def'] now defaults to a .find :all query--much nicer. English Like Queries: Typically AR basically stinks to type in--it is way too wordy, and, for lack of a better term, SQL like. klass.find :all, :conditions => ["name LIKE '%?'", 'fred'] This library condenses that to be a little more englishy, by allowing hashed parameters that are parsed for meaning, ex: klass.where 'name starts with' => 'fred' This reads better, is much faster, has support for regex, case sensitivity, etc. without the pain of SQL. Currently as a rails plugin. Check it out. http://code.google.com/p/ruby-roger-useful-functions/wiki/EnglishLikeQueries -=R
on 19.08.2008 09:12
on 19.08.2008 10:37
Roger Pack wrote: > Typically AR basically stinks to type in--it is way too wordy, and, for > Currently as a rails plugin. > Check it out. > > http://code.google.com/p/ruby-roger-useful-functions/wiki/EnglishLikeQueries > > -=R That's interesting, but I have to question the code:payoff ratio. Most ActiveRecord usage doesn't have particularly diverse queries. These queries can be easily and simply hidden away in single-line class methods to give them meaningful mnemonics like "name_starts_with". Using class methods is transparent, trivial and not limited by the features of your library. English is also a very subjective language. What you've done is limited users to a tiny, strict subset of English which is more or less the same as doing something like where :title => [ :starts_with, 'Something' ]. That would require a lot less code and zero parsing. But all of this replaces something you can do in one line anyway. Why add all that complexity? class Post < ActiveRecord::Base def self.title_starts_with(s) find :all, :conditions => [ "title LIKE ?", "#{s}%" ] end end
on 19.08.2008 19:24
Michael Morin wrote: > Roger Pack wrote: >> Typically AR basically stinks to type in--it is way too wordy, and, for >> lack of a better term, SQL like. >> klass.find :all, :conditions => ["name LIKE '%?'", 'fred'] I see that as a plus, not a minus. SQL is well-known, so if you need to know how to do something there a many examples and resources. >> >> This library condenses that to be a little more englishy, by allowing >> hashed parameters that are parsed for meaning, ex: >> >> klass.where 'name starts with' => 'fred' >> >> This reads better, is much faster, has support for regex, case >> sensitivity, etc. without the pain of SQL. Pain of SQL? Anyone writing code, and writing code that interacts with a database, owes it to themselves (and their clients if they are coding for $) to learn at least basic SQL and know where to look to learn more. It's a DSL meant for relational data. For the most part it is not hard, and it gives you the power to write compact, efficient queries. > as doing something like where :title => [ :starts_with, 'Something' ]. > That would require a lot less code and zero parsing. Attempts to allow code that is "English-like" tend to come off as the worst of both worlds. You lose the compactness and precision of code, while never really getting the fluid expressiveness of natural language. It tends to look and read as clunky. I'd much rather have code that reads like Ruby but allows for raw SQL when needed (Sequel, as a prime example, or Og), rather then code that doesn't quite read as code, and doesn't quite read as English either. And if you're going to write DB apps, then geek up and just learn the SQL. -- James Britt www.happycamperstudios.com - Wicked Cool Coding www.jamesbritt.com - Playing with Better Toys www.ruby-doc.org - Ruby Help & Documentation www.rubystuff.com - The Ruby Store for Ruby Stuff
on 19.08.2008 22:13
> That's interesting, but I have to question the code:payoff ratio. Most > ActiveRecord usage doesn't have particularly diverse queries. These > queries can be easily and simply hidden away in single-line class > methods to give them meaningful mnemonics like "name_starts_with". > Using class methods is transparent, trivial and not limited by the > features of your library. Interesting. You can also use named scopes to accomplish something similar albeit a more hard coded approach. > English is also a very subjective language. What you've done is limited > users to a tiny, strict subset of English which is more or less the same > as doing something like where :title => [ :starts_with, 'Something' ]. > That would require a lot less code and zero parsing. I like what you described. Basically I like anything that avoids the tedious :conditions => [] and that reads well, so...sure. > But all of this replaces something you can do in one line anyway. Why > add all that complexity? > > class Post < ActiveRecord::Base > def self.title_starts_with(s) > find :all, :conditions => [ "title LIKE ?", "#{s}%" ] > end > end Yeah named scopes are another way of doing it, as well as hard coded functions like this. However, using english_like_queries you never have to build those methods, or new ones when you find that what you have isn't exactly right. It works in conjunction with named scopes, too--i.e. Post.enabled.where 'name starts with' => 'fred' What I really love is that you can use it on existing AR sets, a la user = User.find :first users.posts.where 'date_created < ' => Time.now.yesterday Somehow I find it quite useful--it is faster and [for me] more readable. YMMV. -=R