Skip to content

Instantly share code, notes, and snippets.

@logicalhan
Created August 26, 2012 08:12
Show Gist options
  • Save logicalhan/3476077 to your computer and use it in GitHub Desktop.
Save logicalhan/3476077 to your computer and use it in GitHub Desktop.
attr_accessor_with_history
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