Implement Errors#from_array in terms of #from_hash
#439
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.
This commit changes the
ActiveResource::Errors#from_arraymethod to be implemented in terms of its complementary#from_hashmethod.Rather than modifying the
Errorsinstance itself (like managing attribute and:baseassignment, clearing, etc),#from_arraywill instead transform the Array instance into a Hash keyed by known attributes. Once the Hash is constructed, the method hands it off to the#from_hashmethod.The goal of this change is to reduce the duplicated burden of responsibility.
Along with the implementation change, this commit also includes explicit coverage of the
Errors#from_xmlandErrors#from_jsonmethods. They're part of the gem's public API (since they're documented and not marked# :nodoc:), but aren't explicitly tested elsewhere in the suite.There are two XML-focused tests. The first covers the case where the payload has multiple
<error>elements:The call to
Hash.from_xmltransforms each<error>element into an item in the Array.The second test covers the case where the payload has a single payload has a single
<error>element:In this case, The call to
Hash.from_xmltransforms the<error>element into a single String value.Since the
Errors#from_xmlmethod is part of the public API, and since its implementation invokesErrors#from_array, dedicating explicit test coverage for both scenarios can help prevent regressions or breaks in backwards-compatibility.