public
Description:
Homepage:
Clone URL: git://github.com/mdub/representative.git
name age message
file .document Wed Oct 14 04:49:47 -0700 2009 Set up documentation using YARD. [mdub]
file .gitignore Wed Oct 14 04:49:47 -0700 2009 Set up documentation using YARD. [mdub]
file LICENSE Fri Sep 04 01:27:11 -0700 2009 Initial commit to representative. [mdub]
file README.markdown Wed Dec 02 13:38:34 -0800 2009 Provide a nicer way to generate empty elements. [mdub]
file Rakefile Wed Dec 02 01:12:04 -0800 2009 New version. [mdub]
directory examples/ Fri Sep 04 06:05:25 -0700 2009 Add an example. [mdub]
file init.rb Sat Sep 05 04:14:46 -0700 2009 Add an "init.rb" for easier use as a Rails plugin. [mdub]
directory lib/ Wed Dec 02 13:59:38 -0800 2009 Crank version forward again. [mdub]
file representative.gemspec Wed Dec 02 13:59:55 -0800 2009 Regenerated gemspec for version 0.1.2 [mdub]
directory spec/ Wed Dec 02 13:38:34 -0800 2009 Provide a nicer way to generate empty elements. [mdub]
README.markdown

Representative

"Representative" makes it easier to create XML representations of your Ruby objects. It works best when you want the XML to roughly follow the object structure, but still have complete control of the result.

Example

Given a Ruby data-structure:

books = [
  OpenStruct.new(
    :title => "Sailing for old dogs", 
    :authors => ["Jim Watson"],
    :published => OpenStruct.new(
      :by => "Credulous Print",
      :year => 1994
    )
  ),
  OpenStruct.new(
    :title => "On the horizon", 
    :authors => ["Zoe Primpton", "Stan Ford"],
    :published => OpenStruct.new(
      :by => "McGraw-Hill",
      :year => 2005
    )
  ),
  OpenStruct.new(
    :title => "The Little Blue Book of VHS Programming",
    :authors => ["Henry Nelson"],
    :rating => "****"
  )
]

Representative::Xml can be used to generate XML, in a declarative style:

xml = Builder::XmlMarkup.new(:indent => 2)
representative = Representative::Xml.new(xml)

representative.list_of!(:books, books) do |_book|
  _book.title
  _book.list_of!(:authors)
  _book.published do |_published|
    _published.by
    _published.year
  end
end

puts xml.target!

The resulting XML looks like this:

<books type="array">
  <book>
    <title>Sailing for old dogs</title>
    <authors type="array">
      <author>Jim Watson</author>
    </authors>
    <published>
      <by>Credulous Print</by>
      <year>1994</year>
    </published>
  </book>
  <book>
    <title>On the horizon</title>
    <authors type="array">
      <author>Zoe Primpton</author>
      <author>Stan Ford</author>
    </authors>
    <published>
      <by>McGraw-Hill</by>
      <year>2005</year>
    </published>
  </book>
  <book>
    <title>The Little Blue Book of VHS Programming</title>
    <authors type="array">
      <author>Henry Nelson</author>
    </authors>
    <published/>
  </book>
</books>

Notice that:

  • Representative generates elements for each object-attribute you name (and not the ones you don't).
  • The structure of the XML mirrors the structure described by the nested Ruby blocks.
  • Using list_of! for a collection attribute generates an "array" element, which plays nicely with most Ruby XML-to-hash converters.
  • Where a named object-attribute is nil, you get an empty element.

Installation

Representative is packaged as a Gem, hosted on gemcutter. Install with:

sudo gem install representative

Copyright

Copyright (c) 2009 Mike Williams. See LICENSE for details.