nesquena/rabl — Issue #177: allow defining a subnode that doesn't represent a model object
Author: psionides
Created: 2012-02-28 14:32:44+00:00
State: open

Let's say I want to generate such JSON:

```
{
"user": { ... },
"config": {
"base_url": "http://server.com",
"env": "development",
"location": { "lat": 50, "lng": 20 },
...
}
}
```

In this case, the `config` node doesn't represent any model object, it's just a simple hash. The problem is how to add the `config` node to the parent in the template.

I could do something like this:

```
node(:config) {{ :base_url => ..., :env => ..., :location => ... }}
```

I don't like this approach though, because it forces me to drop the RABL DSL used everywhere else and go back to the standard hash syntax. Also, I can't use `child()` anymore to define a child node that represents a model (if e.g. `location` was a model object).

I found a way to get around this by writing:

```
child Object.new => :config do
...
end
```

So my proposed solution is to just add a helper that will let you do this more easily. Something like this:

```
class Rabl::Engine
def hash(name, &block)
child(Object.new => name, &block)
end
end
```

And this would let you just write:

```
hash :config do
node(:base_url) { "http://server.com" }
child @location
end
```