Last active
August 29, 2015 13:57
-
-
Save morr/9374922 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 Cacher | |
def self.prepended target | |
target.send :define_singleton_method, :cache do |method| | |
Cacher.send :define_method, method do |*args| | |
instance_variable_get("@#{method}") || instance_variable_set("@#{method}", super(*args)) | |
end | |
end | |
end | |
end | |
class Model | |
prepend Cacher | |
cache :foo | |
def foo | |
puts 'Model.foo call' | |
'result' | |
end | |
end | |
model = Model.new | |
puts model.foo | |
puts model.foo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$> ruby cacher.rb