Skip to content

Instantly share code, notes, and snippets.

@johnbender
Created March 18, 2009 15:44
Show Gist options
  • Save johnbender/81203 to your computer and use it in GitHub Desktop.
Save johnbender/81203 to your computer and use it in GitHub Desktop.
module RedQuery
module ActiveRecord
@@where_mutex = Mutex.new
def where(*args, &block)
#establish scope for conditions
conditions = nil
#make sure only one thread at a time is altering the class
#variables inside RedQuery::Serializers::Operations
@@where_mutex.synchronize do
#yielding the block will alter the Operations class
yield
#record altered conditions and values
conditions = ::RedQuery::Serializers::Operations.conditions
#clear the alterations
::RedQuery::Serializers::Operations.clear
end
#limit the records returned (:first, :all, :last)
limit = args.first ? args.first : :all
#call find with the conditions and values provided by the block
find(limit, :conditions => conditions)
end
end
end
#extend ActiveRecord::Base if this file is required in a rails environment
if Object.const_defined?(:ActiveRecord)
ActiveRecord::Base.send :extend, RedQuery::ActiveRecord
end
### example use
p = User.where { :id.between 1,2 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment