Guest User

Untitled

a guest
Apr 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. # I want more control of how my model gets serialized into XML. I was toying with an idea... along the lines of
  2.  
  3. class User
  4. property :user_name, String
  5. property :domain, String
  6.  
  7. has_one :contact
  8.  
  9. when_serializing :name => 'user', :show_id => 'true'
  10.  
  11. serialize :user_name, :formats => [:xml,:json], :contexts => [:summary, :detailed]
  12. serialize :domain, :formats => [:xml], :contexts => [:detailed]
  13. serialize :contact, :formats => [:xml], :context => [:detailed]
  14. end
  15.  
  16. class Contact
  17. property :first_name, String
  18. property :last_name, String
  19.  
  20. serialize :first_name, :formats => [:xml,:json], :contexts => [:summary, :detailed]
  21. serialize :last_name, :formats => [:xml,:json], :contexts => [:summary,:detailed]
  22. end
  23.  
  24. # and then you could do
  25. User.first(:user_name => 'joe').to_xml(:detailed)
  26.  
  27. # or (with some magic on array)
  28.  
  29. User.all().to_xml(:summary)
Add Comment
Please, Sign In to add comment