instance method key_format!

Ruby on Rails 2.7.0

Since v2.4.1 Last seen in v2.14.1

Available in: v2.4.1 v2.5.0 v2.6.4 v2.7.0 v2.8.0 v2.9.1 v2.10.2 v2.11.5 v2.12.0 v2.13.0 v2.14.1

Signature

key_format!(*args)

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.

Example:

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

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

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

json.key_format! camelize: :lower
json.first_name "David"

{ "firstName": "David" }

Lambdas can also be used.

json.key_format! ->(key){ "_" + key }
json.first_name "David"

{ "_first_name": "David" }

Parameters

args rest
Source
# File lib/jbuilder.rb, line 100
  def key_format!(*args)
    @key_formatter = KeyFormatter.new(*args)
  end

Defined in lib/jbuilder.rb line 100 · View on GitHub · Improve this page · Find usages on GitHub

Defined in Jbuilder

Type at least 2 characters to search.

↑↓ navigate · open · esc close