Skip to content

Instantly share code, notes, and snippets.

@morr
Last active August 29, 2015 13:57
Show Gist options
  • Save morr/9374922 to your computer and use it in GitHub Desktop.
Save morr/9374922 to your computer and use it in GitHub Desktop.
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
@morr
Copy link
Author

morr commented Mar 5, 2014

$> ruby cacher.rb

Model.foo call
result
result

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment