You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `Base.casing` configuration controls how resource classes handle API
responses with cases that differ from Ruby's `camel_case` idioms.
For example, setting `casing = :camelcase` will configure the resource
to transform inbound camelCase JSON to under_score when loading API
data:
```ruby
payload = {
id: 1,
firstName: "Matz"
}.as_json
person = Person.load(payload)
person.first_name # => "Matz"
```
Similarly, the casing configures the resource to transform outbound
attributes from the intermediate `under_score` idiomatic Ruby names to
the original `camelCase` names:
```ruby
person.first_name # => "Matz"
person.encode # => "{\"id\":1, \"firstName\":\"Matz\"}"
```
By default, resources are configured with `casing = :none`, which does
not transform keys. In addition to `:none` and `:camelcase`, the
`:underscore` configuration ensures idiomatic Ruby names throughout.
When left unconfigured, `casing = :camelcase` will transform keys with
a lower case first letter. To transform with upper case letters,
construct an instance of `ActiveResource::Casings::CamelcaseCasing`:
```ruby
Person.casing = ActiveResource::Casings::CamelcaseCasing.new(:upper)
payload = {
Id: 1,
FirstName: "Matz"
}.as_json
person = Person.load(payload)
person.first_name # => "Matz"
person.encode #=> "{\"Id\":1,\"FirstName\":\"Matz\"}"
```
Casing transformations are also applied to query parameters built from
`.where` clauses:
```ruby
Person.casing = :camelcase
Person.where(first_name: "Matz") # => GET /people.json?firstName=Matz
```
0 commit comments