Created
March 25, 2014 12:03
-
-
Save ciscou/9760422 to your computer and use it in GitHub Desktop.
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
module ActiveRecord | |
class Base | |
def self.scope(name, callable) | |
define_singleton_method name do | |
options = callable.respond_to?(:call) ? callable.call : callable | |
puts "Fetching records with options #{options.inspect}" | |
end | |
end | |
end | |
end | |
class MyModel < ActiveRecord::Base | |
scope :active1, { "published_at.lt" => Time.now } | |
scope :active2, -> { puts 2 ; { "published_at.lt" => Time.now } } | |
end | |
MyModel.active1 | |
sleep 1 | |
MyModel.active1 | |
sleep 1 | |
MyModel.active1 | |
sleep 1 | |
MyModel.active1 | |
sleep 1 | |
MyModel.active2 | |
sleep 1 | |
MyModel.active2 | |
sleep 1 | |
MyModel.active2 | |
sleep 1 | |
MyModel.active2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment