Guest

Untitled

By: a guest on Jan 28th, 2012  |  syntax: None  |  size: 0.44 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. attr_name = "price"
  2.  
  3. micron_read_method = "def #{attr_name}; read_attribute(:#{attr_name}) * 1000; end"
  4. micron_write_method = "def #{attr_name}=(val); write_attribute(:#{attr_name}, val/1000); end"
  5.  
  6. evaluate_attribute_method attr_name, micron_read_method
  7. evaluate_attribute_method attr_name, micron_write_method
  8.  
  9. > object.price = 10
  10. > puts object.price
  11. # 10
  12. > object.price = 20
  13. > puts object.price
  14. # 20
  15. > puts object.price_was
  16. # 10000