Phoenix Rising
2009-Apr-11 01:13 UTC
How do I automatically modify a method when accessing it?
I''m in a weird situation where I''m storing encrypted information in a database. For example, a User.name field would be stored in its encrypted format, so its not human-readable when accessing: User.name => ")@Nw0asna;sd//20b3Q#A02nsoasd \n" (This is just an example. I''m not actually encrypting names and the model isn''t User ;-) But you get the idea.) What I''m trying to figure out is the right way to go about automatically assigning the "name" method to decrypt the data itself when it''s being pulled out for display. For example, I tried: def name self.name = decrypt_name end private def decrypt_name decryption_object.decrypt(self.name) end ---- However, that has obvious problems - in my test, it seems to form an infinite loop, bouncing back and forth between the "name" method and the decrypt_name method (because one calls the other). Is there any way other than writing specific get and set methods to do this? I''d even be happy with a retrieval happening within find: User.find(:first) => #<User id: 1, name: "John Doe"> Even though "name" stored inside the database is actually encrypted. Any thoughts? Thanks :) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Phoenix Rising
2009-Apr-11 01:24 UTC
Re: How do I automatically modify a method when accessing it?
Okay, I think I''m on the right track. I just found out that ActiveRecord::Base has a section on overwriting default accessors. So my basic method is now: def name decrypt(read_attribute(:name)) end The only thing that concerns me is that I also have a before_save :encrypt_data call as part of the class definition. So let''s say I submit a form via post: @user = User.new(params[:user]) @user.save If I read this right, when I call User.new, or if I were to specify User.find(:first, :conditions => {:name => "John Doe"}), it would first try to decrypt data *that isn''t encrypted in the first place*). Am I way off track here or is this about right, and if so, is there any way I can automate the abstraction that needs to take place, so that User.name, when accessed, is decrypted only if it''s in an encrypted form? Hope that makes sense :) On Apr 10, 7:13 pm, Phoenix Rising <PolarisRis...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m in a weird situation where I''m storing encrypted information in a > database. For example, a User.name field would be stored in its > encrypted format, so its not human-readable when accessing: > > User.name > => ")@Nw0asna;sd//20b3Q#A02nsoasd \n" > > (This is just an example. I''m not actually encrypting names and the > model isn''t User ;-) But you get the idea.) > > What I''m trying to figure out is the right way to go about > automatically assigning the "name" method to decrypt the data itself > when it''s being pulled out for display. > > For example, I tried: > > def name > self.name = decrypt_name > end > > private > def decrypt_name > decryption_object.decrypt(self.name) > end > > ---- > However, that has obvious problems - in my test, it seems to form an > infinite loop, bouncing back and forth between the "name" method and > the decrypt_name method (because one calls the other). > > Is there any way other than writing specific get and set methods to do > this? I''d even be happy with a retrieval happening within find: > > User.find(:first) > => #<User id: 1, name: "John Doe"> > > Even though "name" stored inside the database is actually encrypted. > > Any thoughts? Thanks :)--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---