Created
August 26, 2012 08:12
-
-
Save logicalhan/3476077 to your computer and use it in GitHub Desktop.
attr_accessor_with_history
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
class Class | |
def attr_accessor_with_history(attr_name) | |
attr_name = attr_name.to_s | |
history_method_name = attr_name + "_history" | |
attr_reader attr_name | |
attr_reader history_method_name | |
class_eval(%Q( | |
def #{attr_name}=(#{attr_name}) | |
@#{attr_name} = #{attr_name} | |
if @#{history_method_name} | |
@#{history_method_name}.push(#{attr_name}) | |
else | |
@#{history_method_name}=[] | |
@#{history_method_name}.push(nil) | |
@#{history_method_name}.push(#{attr_name}) | |
end | |
end | |
)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment