Ruby/Builder API: create XML without using blocks

registereds

New Member
I'd like to use Builder to construct a set of XML files based on a table of ActiveRecord models. I have nearly a million rows, so I need to use \[code\]find_each(batch_size: 5000)\[/code\] to iterate over the records and write an XML file for each batch of them, until the records are exhausted. Something like the following:\[code\]filecount = 1count = 0xml = ""Person.find_each(batch_size: 5000) do |person| xml += person.to_xml # pretend .to_xml() exists count += 1 if count == MAX_PER_FILE File.open("#{filecount}.xml", 'w') {|f| f.write(xml) } xml = "" filecount += 1 count = 0 endend\[/code\]This doesn't work well with Builder's interface, as it wants to work in blocks, like so:\[code\]xml = builder.person { |p| p.name("Jim") }\[/code\]Once the block ends, Builder closes its current stanza; you can't keep a reference to p and use it outside of the block (I tried). Basically, Builder wants to "own" the iteration.So to make this work with builder, I'd have to do something like:\[code\] filecount = 0 offset = 0 while offset < Person.count do count = 0 builder = Builder::XmlMarkup.new(indent: 5) xml = builder.people do |people| Person.limit(MAX_PER_FILE).offset(offset).each do |person| people.person { |p| p.name(person.name) } count += 1 end end File.open("#output@file_count.xml", 'w') {|f| f.write(xml) } filecount += 1 offset += count end\[/code\]Is there a way to use Builder without the block syntax? Is there a way to programmatically tell it "close the current stanza" rather than relying on a block?
 
Back
Top