I am using Builder to create xml in Ruby. It works fine when I call the generate_object method twice. However, when doing it in a loop I am getting some weird results. The input is the same both times.
Here is the code.
def generate_xml
xml = Builder::XmlMarkup.new
#this doesn't work
#objects.each do |o|
# generate_object xml, o
#end
#this works...
generate_object(xml, objects[0])
generate_object(xml, objects[1])
end
def generate_object(builder, object)
builder.__send__('ins0', :zObjects, 'xsi:type' => "ins1:account") do |a|
object.to_hash.each do |k,v|
#puts "key #{k} #{v}!!!!!!!!!!!!!!!!!!!"
a.__send__('ins1', k.to_s.zuora_camelize.to_sym, v) unless v.nil?
end
end
end
Looping through the objects prints:
<env:Body>
<ins0:create>
[#<Zuora::Objects::Account:0x007fcd69d61788
@changed_attributes={
"auto_pay"=>nil,
"currency"=>nil,
"batch"=>nil,
"bill_cycle_day"=>nil,
"status"=>nil,
"payment_term"=>nil,
"name"=>nil
},
@auto_pay=false,
@currency="USD",
@batch="Batch1",
@bill_cycle_day=1,
@status="Draft",
@payment_term="Due Upon Receipt",
@name="test">,
#<Zuora::Objects::Account:0x007fcd69d60d10
@changed_attributes={
"auto_pay"=>nil,
"currency"=>nil,
"batch"=>nil,
"bill_cycle_day"=>nil,
"status"=>nil,
"payment_term"=>nil,
"name"=>nil
},
@auto_pay=false,
@currency="USD",
@batch="Batch1",
@bill_cycle_day=1,
@status="Draft",
@payment_term="Due Upon Receipt",
@name="test 2">
]
</ins0:create>
</env:Body>
Executing the two calls without a loop:
<env:Body>
<ins0:create>
<ins0:zObjects xsi:type="ins1:account">
<ins1:AutoPay>false</ins1:AutoPay>
<ins1:Batch>Batch1</ins1:Batch>
<ins1:BillCycleDay>1</ins1:BillCycleDay>
<ins1:Currency>USD</ins1:Currency>
<ins1:Name>test</ins1:Name>
<ins1:PaymentTerm>Due Upon Receipt</ins1:PaymentTerm>
<ins1:Status>Draft</ins1:Status>
</ins0:zObjects>
<ins0:zObjects xsi:type="ins1:account">
<ins1:AutoPay>false</ins1:AutoPay>
<ins1:Batch>Batch1</ins1:Batch>
<ins1:BillCycleDay>1</ins1:BillCycleDay>
<ins1:Currency>USD</ins1:Currency>
<ins1:Name>test 2</ins1:Name>
<ins1:PaymentTerm>Due Upon Receipt</ins1:PaymentTerm>
<ins1:Status>Draft</ins1:Status>
</ins0:zObjects>
</ins0:create>
</env:Body>
Looks like you are just getting the output of .each muddled up with what you want to return.
Try:
def generate_xml
xml = Builder::XmlMarkup.new
objects.each do |o|
generate_object xml, o
end
# Return the builder object, not the output of .each (the list of objects)!
xml
end