I have some hashes I want to iterate, and some have nested hashes which can go 3-4 levels deep, I am using an if statement at the moment to check to see if the value is a hash and then iterate through it again however I'm just repeating code here.
Is there a DRY way to do this using a method?
Also I want the final output to end up in a table, what's the best way to do this? That's why I had the multiple if statements originally so I could add the separate tags.
example method:
<% def hashTest(key, value) %>
<% if value.is_a?(Hash) %>
<%= key %>
<% value.each do |key, value| %>
<%= key %>
<%= value %>
<% end %>
<% else %>
<%= key %>
<%= value %>
<% end %>
<% end %>
and this is the mess of if statements I have...
<% parsed.each do |key, value| %>
<% if value.is_a?(Hash) %>
<%= key %>
<br/>
<% value.each do |key, value| %>
<% if value.is_a?(Hash) %>
<%= key %>
<br/>
<% value.each do |key, value| %>
<% if value.is_a?(Hash) %>
<%= key %>
<br/>
<% value.each do |key, value| %>
<%= key %>
<%= value %>
<br/>
<% end %>
<% else %>
<%= key %>
<%= value %>
<br/>
<% end %>
<% end %>
<% else %>
<%= key %>
<%= value %>
<br/>
<% end %>
<% end %>
<% else %>
<%= key %>
<%= value %>
<br/>
<% end %>
<% end %>
example input:
{
"statement": {
"generated": "2015-01-11",
"due": "2015-01-25",
"period": {
"from": "2015-01-26",
"to": "2015-02-25"
}
},
"total": 136.03,
"package": {
"subscriptions": [
{ "type": "tv", "name": "Movies", "cost": 50.00 },
{ "type": "Phone", "name": "Landline", "cost": 5.00 },
{ "type": "broadband", "name": "Fibre", "cost": 16.40 }
],
"total": 71.40
},
"callCharges": {
"calls": [
{ "called": "07716393769", "duration": "00:23:03", "cost": 2.13 },
{ "called": "07716393769", "duration": "00:23:03", "cost": 2.13 },
{ "called": "07716393769", "duration": "00:23:03", "cost": 2.13 },
{ "called": "07716393769", "duration": "00:23:03", "cost": 2.13 },
{ "called": "07716393769", "duration": "00:23:03", "cost": 2.13 },
{ "called": "07716393769", "duration": "00:23:03", "cost": 2.13 },
{ "called": "07716393769", "duration": "00:23:03", "cost": 2.13 },
{ "called": "07716393769", "duration": "00:23:03", "cost": 2.13 },
{ "called": "07716393769", "duration": "00:23:03", "cost": 2.13 },
{ "called": "07716393769", "duration": "00:23:03", "cost": 2.13 },
{ "called": "07716393769", "duration": "00:23:03", "cost": 2.13 },
{ "called": "07716393769", "duration": "00:23:03", "cost": 2.13 },
{ "called": "07716393769", "duration": "00:23:03", "cost": 2.13 },
{ "called": "07716393769", "duration": "00:23:03", "cost": 2.13 },
{ "called": "07716393769", "duration": "00:23:03", "cost": 2.13 },
{ "called": "07716393769", "duration": "00:23:03", "cost": 2.13 },
{ "called": "07716393769", "duration": "00:23:03", "cost": 2.13 },
{ "called": "07716393769", "duration": "00:23:03", "cost": 2.13 },
{ "called": "02074351359", "duration": "00:23:03", "cost": 2.13 },
{ "called": "02074351359", "duration": "00:23:03", "cost": 2.13 },
{ "called": "02074351359", "duration": "00:23:03", "cost": 2.13 },
{ "called": "02074351359", "duration": "00:23:03", "cost": 2.13 },
{ "called": "02074351359", "duration": "00:23:03", "cost": 2.13 },
{ "called": "02074351359", "duration": "00:23:03", "cost": 2.13 },
{ "called": "02074351359", "duration": "00:23:03", "cost": 2.13 },
{ "called": "02074351359", "duration": "00:23:03", "cost": 2.13 },
{ "called": "02074351359", "duration": "00:23:03", "cost": 2.13 },
{ "called": "02074351359", "duration": "00:23:03", "cost": 2.13 }
],
"total": 59.64
},
"Store": {
"rentals": [
{ "title": "50 Shades of Grey", "cost": 4.99 }
],
"purchases": [
{ "title": "That's what she said", "cost": 9.99 },
{ "title": "Broke back mountain", "cost": 9.99 }
],
"total": 24.97
}
}
Example output: