Created
January 13, 2011 12:55
-
-
Save carlosbrando/777816 to your computer and use it in GitHub Desktop.
Chain Gang with Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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