Commit 954cecc
committed
Implement
This commit changes the `ActiveResource::Errors#from_array`
method to be implemented in terms of its complementary `#from_hash`
method.
Rather than modifying the `Errors` instance itself (like managing
attribute and `:base` assignment, clearing, etc), `#from_array` will
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_hash` method.
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_xml` and `Errors#from_json` methods.
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:
```ruby
Hash.from_xml "<errors><error>Name can't be blank</error><error>Email can't be blank</error></errors>"
# => {"errors"=>{"error"=>["Name can't be blank", "Email can't be blank"]}}
```
The call to `Hash.from_xml` transforms 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:
```ruby
Hash.from_xml "<errors><error>Name can't be blank</error></errors>"
# => {"errors"=>{"error"=>"Name can't be blank"}}
```
In this case, The call to `Hash.from_xml` transforms the `<error>`
element into a single String value.
Since the `Errors#from_xml` method is part of the public API, and since
its implementation invokes `Errors#from_array`, dedicating explicit test
coverage for both scenarios can help prevent regressions or
breaks in backwards-compatibility.Errors#from_array in terms of #from_hash
1 parent bd154d2 commit 954cecc
File tree
2 files changed
+64
-4
lines changed- lib/active_resource
- test/cases
2 files changed
+64
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
| 22 | + | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
26 | 26 | | |
| 27 | + | |
| 28 | + | |
27 | 29 | | |
28 | 30 | | |
29 | 31 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
76 | 76 | | |
77 | 77 | | |
78 | 78 | | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
0 commit comments