Commit 5e5344a
committed
Integrate with Active Model Attributes
The `schema { ... }` interface pre-dates the Active Model Attributes API
(defined as early as [v5.2.0][]), but clearly draws inspiration from
Active Record's Database Schema and Attribute casting (which was
extracted into `ActiveModel::Attributes`).
However, the type information captured in `schema { ... }` blocks or
assigned as `Hash` arguments to `schema=` is purely inert metadata.
Proposal
---
This commit aims to integrate with [ActiveModel::Model][] and
[ActiveModel::Attributes][]. Through the introduction of both modules,
subclasses of `ActiveResource::Schema` can benefit from type casting
attributes and constructing instances with default values.
This commit makes minimally incremental changes, prioritizing backwards
compatibility. The reliance on `#respond_to_missing?` and
`#method_missing` is left largely unchanged. Similarly, the `Schema`
interface continues to provide metadata about its attributes through the
`Schema#attr` method (instead of reading from
`ActiveModel::Attributes#attribute_names` or
`ActiveModel::Attributes.attribute_types`).
API Changes
---
To cast values to their specified types, declare the Schema with the
`:cast_values` set to true.
```ruby
class Person < ActiveResource::Base
schema cast_values: true do
integer 'age'
end
end
p = Person.new
p.age = "18"
p.age # => 18
```
To configure inheriting resources to cast values, set the `cast_values`
class attribute:
```ruby
class ApplicationResource < ActiveResource::Base
self.cast_values = true
end
class Person < ApplicationResource
schema do
integer 'age'
end
end
p = Person.new
p.age = "18"
p.age # => 18
```
To set all resources application-wide to cast values, set
`config.active_resource.cast_values`:
```ruby
# config/application.rb
config.active_resource.cast_values = true
```
[v5.2.0]: https://api.rubyonrails.org/v5.2.0/classes/ActiveModel/Attributes/ClassMethods.html
[ActiveModel::Model]: https://api.rubyonrails.org/classes/ActiveModel/Model.html
[ActiveModel::Attributes]: https://api.rubyonrails.org/classes/ActiveModel/Attributes/ClassMethods.html1 parent 9c8a2ee commit 5e5344a
File tree
3 files changed
+185
-45
lines changed- lib/active_resource
- test/cases/base
3 files changed
+185
-45
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
335 | 335 | | |
336 | 336 | | |
337 | 337 | | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
338 | 344 | | |
339 | 345 | | |
340 | 346 | | |
| |||
385 | 391 | | |
386 | 392 | | |
387 | 393 | | |
388 | | - | |
389 | | - | |
390 | | - | |
391 | | - | |
392 | | - | |
393 | | - | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
394 | 432 | | |
395 | | - | |
| 433 | + | |
396 | 434 | | |
397 | | - | |
| 435 | + | |
| 436 | + | |
398 | 437 | | |
399 | 438 | | |
400 | 439 | | |
| |||
434 | 473 | | |
435 | 474 | | |
436 | 475 | | |
| 476 | + | |
437 | 477 | | |
438 | 478 | | |
439 | 479 | | |
| |||
1213 | 1253 | | |
1214 | 1254 | | |
1215 | 1255 | | |
| 1256 | + | |
1216 | 1257 | | |
1217 | 1258 | | |
1218 | 1259 | | |
| |||
1246 | 1287 | | |
1247 | 1288 | | |
1248 | 1289 | | |
| 1290 | + | |
1249 | 1291 | | |
1250 | 1292 | | |
1251 | 1293 | | |
| |||
1285 | 1327 | | |
1286 | 1328 | | |
1287 | 1329 | | |
1288 | | - | |
| 1330 | + | |
1289 | 1331 | | |
1290 | 1332 | | |
1291 | 1333 | | |
1292 | 1334 | | |
1293 | | - | |
| 1335 | + | |
1294 | 1336 | | |
1295 | 1337 | | |
1296 | 1338 | | |
| |||
1481 | 1523 | | |
1482 | 1524 | | |
1483 | 1525 | | |
1484 | | - | |
| 1526 | + | |
1485 | 1527 | | |
1486 | 1528 | | |
1487 | 1529 | | |
| |||
1498 | 1540 | | |
1499 | 1541 | | |
1500 | 1542 | | |
1501 | | - | |
| 1543 | + | |
1502 | 1544 | | |
1503 | 1545 | | |
1504 | 1546 | | |
| |||
1541 | 1583 | | |
1542 | 1584 | | |
1543 | 1585 | | |
1544 | | - | |
| 1586 | + | |
| 1587 | + | |
| 1588 | + | |
1545 | 1589 | | |
1546 | 1590 | | |
1547 | 1591 | | |
| |||
1698 | 1742 | | |
1699 | 1743 | | |
1700 | 1744 | | |
| 1745 | + | |
| 1746 | + | |
| 1747 | + | |
| 1748 | + | |
| 1749 | + | |
| 1750 | + | |
| 1751 | + | |
| 1752 | + | |
| 1753 | + | |
| 1754 | + | |
| 1755 | + | |
| 1756 | + | |
| 1757 | + | |
| 1758 | + | |
| 1759 | + | |
| 1760 | + | |
1701 | 1761 | | |
1702 | 1762 | | |
1703 | 1763 | | |
1704 | | - | |
| 1764 | + | |
| 1765 | + | |
| 1766 | + | |
1705 | 1767 | | |
1706 | 1768 | | |
1707 | 1769 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
5 | 8 | | |
6 | 9 | | |
7 | 10 | | |
8 | 11 | | |
9 | 12 | | |
10 | 13 | | |
11 | | - | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
12 | 19 | | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
13 | 23 | | |
14 | 24 | | |
15 | 25 | | |
| |||
22 | 32 | | |
23 | 33 | | |
24 | 34 | | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | 35 | | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
39 | 41 | | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
46 | 50 | | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
58 | 85 | | |
59 | 86 | | |
60 | 87 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
| |||
425 | 426 | | |
426 | 427 | | |
427 | 428 | | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
428 | 479 | | |
0 commit comments