The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
What's New in Edge Rails: A More Flexible to_xml

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
rwdaigle

Posts: 312
Nickname: rwdaigle
Registered: Feb, 2003

Ryan is a passionate ruby developer with a strong Java background.
What's New in Edge Rails: A More Flexible to_xml Posted: Apr 21, 2007 1:36 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by rwdaigle.
Original Post: What's New in Edge Rails: A More Flexible to_xml
Feed Title: Ryan's Scraps
Feed URL: http://feeds.feedburner.com/RyansScraps
Feed Description: Ryan Daigle's various technically inclined rants along w/ the "What's new in Edge Rails" series.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by rwdaigle
Latest Posts From Ryan's Scraps

Advertisement

The to_xml method on your ActiveRecord classes has a bunch ���o options that let you refine how you want your models to be serialized to xml. You���ve got your :only and :except options which let you filter in/out certain attributes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
user = User.find(1)

user.to_xml(:except => [:id, :created_at])
#=>
# <user>
#   <name>Ryan</name>
#   <email>ryan@spamme.com</email>
# </user>

user.to_xml(:only => [:email])
#=>
# <user>
#   <email>ryan@spamme.com</email>
# </user>

(Want to have the result of methods serialized along side member attributes? Take a look at the :methods option).

You can also include first-level associations with :include:

1
2
3
4
5
6
7
8
9
user.to_xml(:except => [:id, :created_at], :include => :posts)
#=>
# <user>
#   <name>Ryan</name>
#   <email>ryan@spamme.com</email>
#   <posts>
#     <post><title>What's New in Edge Rails</title></post>
#   </posts>
# </user>

You can also manipulate the XML builder directly as to_xml now yields the builder, allowing you to add arbitrary elements to the resulting serialized XML representation:

1
2
3
4
5
6
7
8
9
user.to_xml(:except => [:id, :created_at]) do |xml|
  xml.serialize_version 1.1
end
#=>
# <user>
#   <name>Ryan</name>
#   <email>ryan@spamme.com</email>
#   <serialize_version>1.1</serialize_version>
# </user>

Note that the xml builder received in the block already contains the standard constructed XML representation ��� but you can now add to that with custom elements and other constructs. Just another easy way to massage the serialization to suit your needs.

tags: ruby, rubyonrails

Read: What's New in Edge Rails: A More Flexible to_xml

Topic: Some Easter Pix Previous Topic   Next Topic Topic: Jamis Buck reviews BYORoRWA

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use