Advertisement
Guest User

Untitled

a guest
Feb 29th, 2012
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 0.50 KB | None | 0 0
  1. class Class
  2.  
  3.   def attr_accessor_with_history(attr_name)
  4.     attr_name = attr_name.to_s       #make sure it's a string
  5.     attr_reader attr_name            #create the attribute's getter
  6.     attr_reader attr_name+"_history" #create the bar_history getter
  7.    
  8.     class_eval %Q{
  9.       def #{attr_name}=(val)
  10.         @#{attr_name} = val
  11.         @#{attr_name}_history = (@#{attr_name}_history || []) << val
  12.       end
  13.     } #when you set the attribute, you set it and save it to an array
  14.   end
  15. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement