rails/jbuilder — pull request #37 by bigjason (2012-04-23 14:22:12+00:00)
Title: Alternate more generalized implementation for CamelCase keys

This is an alternate implementation for issue #13. The idea is to provide a more general method for transforming the JSON key format. From the docs:

Specifies formatting to be applied to the key. Passing in a name of a function
will cause that function to be called on the key. So :upcase will upper case
the key. You can also pass in lambdas for more complex transformations.

``` ruby
json.key_format! :upcase
json.author do |json|
json.name "David"
json.age 32
end

# { "AUTHOR": { "NAME": "David", "AGE": 32 } }
```

You can pass parameters to the method using a hash pair.

``` ruby
json.set_format! :camelize => :lower
json.first_name "David"

# { "firstName": "David" }
```

Lambdas can also be used.

``` ruby
json.set_format! ->(key){ "_" + key }
json.first_name "David"

# { "_first_name": "David" }
```

You can set this globaly with the class method `key_format` (from inside your enviorment.rb for example):

``` ruby
Jbuilder.set_format :camelize => :lower
```