Skip to content
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

Typed hash slice and except #75

Merged
merged 2 commits into from
Apr 8, 2019
Merged

Conversation

casperisfine
Copy link
Collaborator

Context

TypedHash inherits from HashWithIndifferentAccess, a while ago many methods like .except()would return a true HashWithIndifferentAccess, but recently they changed to return an instance of the same class than the initial hash.

First slice in 2017 rails/rails@d6f3b91 and more recently except in 2019: rails/rails@a805d72#diff-9f4b3d7342097a6f22290f147a09b77b

The problem with this is that TypedHash can contain default values for keys, so if you clear a a key with a default, it will be reset on the new instance, which makes for confusing defaults.

e.g.: {age: 42}.except(:age) => {age: 0} while you'd likely expect {}.

Solution

We first cast to HashWIthIndifferentAccess before calling these methods.

@rafaelfranca thoughts?

@rafaelfranca
Copy link
Collaborator

Ah, when you say a default is not the Hash.default value but a list of defaults values that are applied in the constructor of this object.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.01%) to 94.927% when pulling 1711765 on typed-hash-slice-and-except into 0f47084 on master.

1 similar comment
@coveralls
Copy link

Coverage Status

Coverage increased (+0.01%) to 94.927% when pulling 1711765 on typed-hash-slice-and-except into 0f47084 on master.

@casperisfine casperisfine merged commit 34a3bd2 into master Apr 8, 2019
@casperisfine casperisfine deleted the typed-hash-slice-and-except branch April 8, 2019 20:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants