From 05b8c8255582651310f449a53388956b981965d5 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sun, 6 Sep 2009 00:25:49 +0200 Subject: AS guide: documents Array#to_xml --- .../guides/source/active_support_overview.textile | 99 ++++++++++++++++++++++ 1 file changed, 99 insertions(+) (limited to 'railties/guides/source') diff --git a/railties/guides/source/active_support_overview.textile b/railties/guides/source/active_support_overview.textile index ca1480b626..7adaafceff 100644 --- a/railties/guides/source/active_support_overview.textile +++ b/railties/guides/source/active_support_overview.textile @@ -859,6 +859,105 @@ invoice.lines.to_formatted_s(:db) # => "23,567,556,12" Integers in the example above are supposed to come from the respective calls to +id+. +h5. +to_xml+ + +The method +to_xml+ returns a string containing an XML representation of its receiver: + + +Contributor.all(:limit => 2, :order => 'rank ASC').to_xml +# => +# +# +# +# 4356 +# Jeremy Kemper +# 1 +# jeremy-kemper +# +# +# 4404 +# David Heinemeier Hansson +# 2 +# david-heinemeier-hansson +# +# + + +To do so it sends +to_xml+ to every item in turn, and collects the results under a root node. All items must respond to +to_xml+, an exception is raised otherwise. + +By default, the name of the root element is the underscorized and dasherized plural of the name of the class of the first item, provided the rest of elements belong to that type (checked with is_a?) and they are not hashes. In the example above that's "contributors". + +If there's any element that does not belong to the type of the first one the root node becomes "records": + + +[Contributor.first, Commit.first].to_xml +# => +# +# +# +# 4583 +# Aaron Batalion +# 53 +# aaron-batalion +# +# +# Joshua Peek +# 2009-09-02T16:44:36Z +# origin/master +# 2009-09-02T16:44:36Z +# Joshua Peek +# +# 190316 +# false +# Kill AMo observing wrap_with_notifications since ARes was only using it +# 723a47bfb3708f968821bc969a9a3fc873a3ed58 +# +# + + +If the receiver is an array of hashes the root element is by default also "records": + + +[{:a => 1, :b => 2}, {:c => 3}].to_xml +# => +# +# +# +# 2 +# 1 +# +# +# 3 +# +# + + +WARNING. If the collection is empty the root element is by default "nil-classes". That's a gotcha, for example the root element of the list of contributors above would not be "contributors" if the collection was empty, but "nil-classes". You may use the :root option to ensure a consistent root element. + +The name of children nodes is by default the name of the root node singularized. In the examples above we've seen "contributor" and "record". The option :children allows you to set these node names. + +The default XML builder is a fresh instance of Builder::XmlMarkup. You can configure your own builder via the :builder option. The method also accepts options like :dasherize and friends, they are forwarded to the builder: + + +Contributor.all(:limit => 2, :order => 'rank ASC').to_xml(:skip_types => true) +# => +# +# +# +# 4356 +# Jeremy Kemper +# 1 +# jeremy-kemper +# +# +# 4404 +# David Heinemeier Hansson +# 2 +# david-heinemeier-hansson +# +# + + h4. Wrapping The class method +Array.wrap+ behaves like the function +Array()+ except that it does not try to call +to_a+ on its argument. That changes the behaviour for enumerables: -- cgit v1.2.3