Skip to content

Instantly share code, notes, and snippets.

@carlosbrando
Created January 13, 2011 12:55
Show Gist options
  • Save carlosbrando/777816 to your computer and use it in GitHub Desktop.
Save carlosbrando/777816 to your computer and use it in GitHub Desktop.
Chain Gang with Ruby
class Query
def initialize(model)
@model = model
end
def where(hash)
@where = hash
self
end
def sort(hash)
@sort = hash
self
end
end
class User
def self.where(hash)
Query.new(self).where(hash)
end
def self.sort(hash)
Query.new(self).sort(hash)
end
end
puts User.where(:first_name => 'John').inspect
puts User.sort(:age).inspect
puts User.where(:first_name => 'John').sort(:age).inspect
puts User.sort(:age).where(:first_name => 'John').inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment