Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure ActiveResource::Base.casing #421

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

seanpdoyle
Copy link

@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
```
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.

1 participant