-
Notifications
You must be signed in to change notification settings - Fork 362
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
Document how to support camelcase attributes #417
Open
seanpdoyle
wants to merge
1
commit into
rails:main
Choose a base branch
from
seanpdoyle:document-overriding-load-and-encode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+112
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,48 @@ module ActiveResource | |
# self.proxy = "https://user:[email protected]:8080" | ||
# end | ||
# | ||
# == Attribute Schema | ||
# | ||
# Active Resource objects respond to attributes returned by +known_attributes+. | ||
# Resource instances populate the +known_attributes+ array during calls to +initialize+ and +load, | ||
# or when data is fetched from the remote system. | ||
# | ||
# Instances will not respond to attribute methods until they are loaded. | ||
# | ||
# Resource classes can define the set of +known_attributes+ by defining a +schema+: | ||
# | ||
# class Person < ActiveResource::Base | ||
# schema do | ||
# # define each attribute separately | ||
# attribute :name, :string | ||
# attribute :age, :integer | ||
# end | ||
# end | ||
# | ||
# p = Person.new | ||
# p.respond_to? :name # => true | ||
# p.respond_to? :name= # => true | ||
# p.respond_to? :age # => true | ||
# p.respond_to? :age= # => true | ||
# p.name # => nil | ||
# p.age # => nil | ||
# | ||
# Attribute-types must be one of: <tt>string, text, integer, float, decimal, datetime, timestamp, time, date, binary, boolean</tt> | ||
# | ||
# To transform data fetched from the remote system prior to attribute | ||
# assignment, override the +load+ method: | ||
# | ||
# class Person < ActiveResource::Base | ||
# def load(attributes, remove_root = false, persisted = false) | ||
# attributes = attributes.deep_transform_keys { |key| key.to_s.underscore } | ||
# | ||
# super | ||
# end | ||
# end | ||
# | ||
# p = Person.new(:firstName => 'Ryan', :lastName => 'Daigle') | ||
# p.first_name # => 'Ryan' | ||
# p.last_name # => 'Daigle' | ||
# | ||
# == Life cycle methods | ||
# | ||
|
@@ -1425,7 +1467,23 @@ def exists? | |
|
||
# Returns the serialized string representation of the resource in the configured | ||
# serialization format specified in ActiveResource::Base.format. The options | ||
# applicable depend on the configured encoding format. | ||
# applicable depend on the configured encoding format, and are forwarded to | ||
# the corresponding serializer method. | ||
# | ||
# ActiveResource::Formats::XmlFormat will forward options <tt>#to_xml</tt> and | ||
# ActiveResource::Formats::JsonFormat will forward options <tt>#to_json</tt>. | ||
# | ||
# Override the +serializable_hash+ method to customize how the | ||
# attribute keys and values are encoded to a JSON or XML string. | ||
# | ||
# class Person < ActiveResource::Base | ||
# def serializable_hash(options = {}) | ||
# super.deep_transform_keys! { |key| key.camelcase(:lower) } | ||
# end | ||
# end | ||
# | ||
# A resource that relies on a custom format must respond to a serializer method | ||
# that corresponds to the format's +extension+ method. | ||
def encode(options = {}) | ||
send("to_#{self.class.format.extension}", options) | ||
end | ||
|
@@ -1466,6 +1524,23 @@ def reload | |
# your_supplier = Supplier.new | ||
# your_supplier.load(my_attrs) | ||
# your_supplier.save | ||
# | ||
# Override +load+ to transform the attributes prior to loading. | ||
# | ||
# class Person < ActiveResource::Base | ||
# def load(attributes, remove_root = false, persisted = false) | ||
# attributes = attributes.deep_transform_keys { |key| key.to_s.underscore } | ||
# | ||
# super | ||
# end | ||
# end | ||
# | ||
# camelcase_attrs = { :firstName => 'Marty', :favorite_colors => ['red', 'green', 'blue'] } | ||
# | ||
# person = Person.new | ||
# person.load(camelcase_attrs) | ||
# person.first_name # => 'Marty' | ||
# person.favorite_colors # => ['red', 'green', 'blue'] | ||
def load(attributes, remove_root = false, persisted = false) | ||
unless attributes.respond_to?(:to_hash) | ||
raise ArgumentError, "expected attributes to be able to convert to Hash, got #{attributes.inspect}" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 notJsonFormat.encode
, a custom format that inherited fromJsonFormat
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 incorporateself.class.format.encode
?