Repository navigation
Add update_heritable_value_of - #39487
jonathanhefner wants to merge 1 commit into
Conversation
ddd4e9c to
d23ee6a
Compare
|
(Whoops!) It's hard to reason about this whether this is useful on its own - do you think you could convert some code that currently uses |
d23ee6a to
fe7fee1
Compare
4f316a9 to
05915a9
Compare
|
@eugeneius Sure! I added some additional commits to showcase its usage.
|
ece3b0d to
6cd7151
Compare
6cd7151 to
aed5d6d
Compare
I've partly picked `LiloHash` from rails#39487. Co-authored-by: Jonathan Hefner <[email protected]>
0fcd6cf to
b4451ea
Compare
|
Can we avoid the dynamic method generation? It is hard to document and find documentation to it. Instead of |
a3d6875 to
b7cf5ba
Compare
|
@rafaelfranca Does the current API in the PR description look all right? If so, I can begin squashing, rebasing, writing tests, etc. |
|
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
72b4dc3 to
8c2ee61
Compare
2e9006e to
1fbcad6
Compare
1fbcad6 to
54a789e
Compare
|
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
54a789e to
fd20342
Compare
|
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
fd20342 to
d84bc77
Compare
|
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
d84bc77 to
3c08003
Compare
|
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
Using a `class_attribute` for inheritance of values in a Hash is tricky.
When storing a new value in a subclass's Hash, you must be careful to
not modify the superclass's Hash. On the flip side, when storing a new
value in a superclass's Hash, you must ensure the subclass can access
the value if and only if it has not already been overridden in the
subclass's Hash.
`update_heritable_value_of` handles both of these requirements:
```ruby
A = Class.new
B = Class.new(A)
C = Class.new(B)
A.class_attribute :settings, default: {}
A.update_heritable_value_of(:settings, :foo, "A")
A.update_heritable_value_of(:settings, :bar, "A")
B.update_heritable_value_of(:settings, :bar, "B")
C.update_heritable_value_of(:settings, :qux, "C")
A.update_heritable_value_of(:settings, :qux, "A")
A.settings # => {:foo=>"A", :bar=>"A", :qux=>"A"}
B.settings # => {:foo=>"A", :bar=>"B", :qux=>"A"}
C.settings # => {:foo=>"A", :bar=>"B", :qux=>"C"}
```
3c08003 to
1d48ef6
Compare
update_heritable_value_of
Using a
class_attributefor inheritance of values in a Hash is tricky. When storing a new value in a subclass's Hash, you must be careful to not modify the superclass's Hash. On the flip side, when storing a new value in a superclass's Hash, you must ensure the subclass can access the value if and only if it has not already been overridden in the subclass's Hash. (See #39467, #39468, and #39473 for examples of addressing the latter.)update_heritable_value_ofhandles both of these requirements:/cc @eugeneius
/cc @kamipo
This is an alternative approach to what was being discussed in #39467 (comment) and #39467 (comment).