RABL は実際には、ルート オブジェクトとしてではなく、属性として Ruby ハッシュと配列を簡単にレンダリングできます。したがって、たとえば、ルート オブジェクトに対して次のような OpenStruct を作成するとします。
@my_object = OpenStruct.new
@my_object.data = {:c => {:d => 'e'}}
次に、この RABL テンプレートを使用できます。
object @my_object
attributes :data
そして、それは次のようになります:
{"data": {"c":{"d":"e"}} }
または、ルート オブジェクトのプロパティにしたい場合は:c
、「ノード」を使用してそのノードを作成し、そのノード内でハッシュをレンダリングできます。
# -- rails controller or whatever --
@my_hash = {:c => {:d => :e}}
# -- RABL file --
object @my_hash
# Create a node with a block which receives @my_hash as an argument:
node { |my_hash|
# The hash returned from this unnamed node will be merged into the parent, so we
# just return the hash we want to be represented in the root of the response.
# RABL will render anything inside this hash as JSON (nested hashes, arrays, etc)
# Note: we could also return a new hash of specific keys and values if we didn't
# want the whole hash
my_hash
end
# renders:
{"c": {"d": "e"}}
ちなみに、これは単に Rails で使用するのとまったく同じrender :json => @my_hash
なので、RABL はこの些細なケースでは特に役に立ちません ;) しかし、とにかく仕組みを示しています。