saasbook

attr_accessor_with_history_answer.rb

Mar 13th, 2014
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.34 KB | None | 0 0
  1. class Class
  2.   def attr_accessor_with_history(attr_name)
  3.     attr_name = attr_name.to_s
  4.     attr_reader attr_name
  5.     attr_reader attr_name+"_history"
  6.     class_eval %Q{
  7.       def #{attr_name}=(new_value)
  8.         @#{attr_name}_history ||= []
  9.         @#{attr_name}_history << @#{attr_name}
  10.         @#{attr_name} = new_value
  11.       end
  12.     }
  13.   end
  14. end
Add Comment
Please, Sign In to add comment