-
Notifications
You must be signed in to change notification settings - Fork 364
Document how to support camelcase attributes #417
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
base: main
Are you sure you want to change the base?
Document how to support camelcase attributes #417
Conversation
Generally, this commit adds documentation to the `ActiveResource::Base` class-level and method-level documentation to explain how attribute loading and encoding works. In addition, test coverage is added for camelcase-based attribute loading and encoding. Code samples are added to method-level documentation to more clearly communicate how support can be added at the application level.
def load(attributes, *args) | ||
attributes = attributes.deep_transform_keys { |key| key.to_s.underscore } | ||
|
||
super | ||
end | ||
|
||
def serializable_hash(options = {}) | ||
super.deep_transform_keys! { |key| key.camelcase(:lower) } | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the best way to achieve this outcome? When learning about the library, my first instinct was to tackle name casing at the JsonFormat
level.
Unfortunately, since #encode
is implemented in terms of #to_json
, and not JsonFormat.encode
, a custom format that inherited from JsonFormat
had no effect.
Is there a more canonical way to extend or configure the pre-existing JsonFormat
module to more elegantly handle this?
Could it be worthwhile to explore changing the Base#encode
implementation to incorporate self.class.format.encode
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could it be worthwhile to explore changing the Base#encode implementation to incorporate self.class.format.encode?
I think so. I'd just make possible to have a custom format and deal with it instead of documenting to add two overrides.
def load(attributes, *args) | ||
attributes = attributes.deep_transform_keys { |key| key.to_s.underscore } | ||
|
||
super | ||
end | ||
|
||
def serializable_hash(options = {}) | ||
super.deep_transform_keys! { |key| key.camelcase(:lower) } | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could it be worthwhile to explore changing the Base#encode implementation to incorporate self.class.format.encode?
I think so. I'd just make possible to have a custom format and deal with it instead of documenting to add two overrides.
Generally, this commit adds documentation to the
ActiveResource::Base
class-level and method-level documentation to explain how attribute loading and encoding works.In addition, test coverage is added for camelcase-based attribute loading and encoding. Code samples are added to method-level documentation to more clearly communicate how support can be added at the application level.