NokoBuilder

NokoBuilder is a gem that makes building out xml using nokogiri even easier! It gives you the ability to add overwritable default values and attributes to each xml tag, while still maintaining all the stuff you get from the normal nokogiri xml builder.

Examples

VALUES

The values for xml tags can be overwritten by passing the value as an option when creating the xml document.

builder = NokoBuilder.new(options) do |xml|
  xml.Docu do
    xml.One "default value"
    xml.Two
  end
end

If options = {} then builder.to_xml would produce:

<?xml version="1.0"?>
<Docu>
  <One>default value</One>
</Docu>

If options = {:One => "BOOM", :Two => "BAMO"} then builder.to_xml would produce:

<?xml version="1.0"?>
<Docu>
  <One>BOOM</One>
  <Two>BAMO</Two>
</Docu>

ATTRIBUTES

You can pass in attributes to the nokobuilder the same way you would with nokogiri builder.

builder = NokoBuilder.new(options) do |xml|
  xml.One('xmlns' => 'http://stuff') do
    xml.Inside "namey", :attribute => "me"
    xml.Another(:one => "one", :two => "two")
  end
end

If options = {} then builder.to_xml Would produce:

<?xml version="1.0"?>
<One xmlns="http://stuff">
  <Inside attribute="me">namey</Inside>
  <Another one="one" two="two"/>
</One>

If options = {:Inside_nattr => {:blamo => "one"}, :Another_nattr => {:one => 'new', :three => "thr"}} then builder.to_xml Would produce:

<?xml version="1.0"?>
<One xmlns="http://stuff">
  <Inside attribute="me" blamo="one">namey</Inside>
  <Another one="new" two="two" three="thr"/>
</One>

NOKOGIRI OPTIONS

You can pass in options to the nokogiri builder as the second argument if you want.

builder = NokoBuilder.new({},{:encoding => 'UTF-8'}) do |xml|
  xml.One do
    xml.Inside "namey"
  end
end

Doing builder.to_xml Would produce:

<?xml version="1.0" encoding="UTF-8"?>
<One>
  <Inside>namey</Inside>
</One>

Contributing to noko_builder

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.

  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.

  • Fork the project.

  • Start a feature/bugfix branch.

  • Commit and push until you are happy with your contribution.

  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2012 frausto. See LICENSE.txt for further details.