Skip to content

Instantly share code, notes, and snippets.

@budu
Created March 3, 2012 18:03
Show Gist options
  • Save budu/1967169 to your computer and use it in GitHub Desktop.
Save budu/1967169 to your computer and use it in GitHub Desktop.
Ruby Class mixin to create an accessor for which historical vales are kept in an Array, not thread-safe.
module AttrHistory
def attr_history(name)
attr_reader "#{name}_history", name
define_method "#{name}=" do |value|
history = send("#{name}_history") || []
instance_variable_set "@#{name}_history", history.push(value)
instance_variable_set "@#{name}", value
end
end
end
Class.send :include, AttrHistory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment