Skip to content

Conversation

seanpdoyle
Copy link
Contributor

@seanpdoyle seanpdoyle commented Jan 26, 2025

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:

person = Person.new(id: 1, firstName: "Matz")
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:

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:

Person.casing = ActiveResource::Casings[:camelcase].new(:upper)

person = Person.new(Id: 1, FirstName: "Matz")
person.first_name # => "Matz"

person.encode #=> "{\"Id\":1,\"FirstName\":\"Matz\"}"

Casing transformations are also applied to query parameters built from .where clauses:

Person.casing = :camelcase

Person.where(first_name: "Matz") # => GET /people.json?firstName=Matz

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
```
@rafaelfranca
Copy link
Member

I get the idea but I don't like that it adds indirection and overhead to the common case. I'd just allow the formatter to be configured and let people override their own formatter and keep the implementation in this library as simple as possible

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants