Search code examples
rubyjsonrabl

RABL - How to create a custom child node as collection?


{     
  total: 250,
  page: 3,
  data: [
        { id: 1, name: "Foo", ...},
        { id: 2, name: "Bar", ...}
  ]
}

I want to create structure like this. Value of id and name are random. It is not saved any variable.


Solution

  • If all you are searching is how to make custom nodes is just with node method.When you declare object to be false you are free to make just custom repsonse.Here is a solution where I generate ids from 1 to 100, and name based on simple conversation to hexademical numbers.

    object false
    node(:total) { |m| @total }
    node(:page)  { |m| @page }
    node(:data) do |m|
        1.upto(100).map { |id| Hash[[[:id, id], [:name, (id * 143223).to_s(16)]]] }
    end