Last active
November 27, 2018 21:37
-
-
Save eric1234/375ad4a79972467d6f30af3bd0146584 to your computer and use it in GitHub Desktop.
Easy Sharing of Data Loading in Rails Controllers
This file contains hidden or 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 Lettable | |
def let name, &blk | |
iv = "@#{name}" | |
define_method name do | |
return instance_variable_get iv if instance_variable_defined? iv | |
instance_variable_set iv, instance_eval(&blk) | |
end | |
helper_method name | |
define_method :"#{name}=" do |value| | |
instance_variable_set iv, value | |
end | |
private :"#{name}=" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment