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

✨ Add Config methods: #to_h, #update, and #with #300

Merged
merged 3 commits into from
Jun 22, 2024

Conversation

nevans
Copy link
Collaborator

@nevans nevans commented Jun 21, 2024

These methods were originally used to implement #load_defaults (to finish #288), and it seems reasonable to add them to the public API.

Unlike SequenceSet (which I want to have a full API to match Set, Range, and Array), I want to be careful in adding any extra public methods to Config. But I think these three are basic enough to be permitted.

#to_h

Returns all config attributes in a hash.

#update

Assigns all of the provided +attrs+ to this config, and returns +self+.

Each key in +attrs+ must match an assignment method on Config.

In other words, #update does the same thing that #initialize does with its kwargs.

#with

Without a block, returns a new config which inherits from self. With a block, yields the new config and returns the block's result.

I originally made #with create a copy (using dup), which meant it was a "sibling" to the receiver, not a child. I did it that way because that's what I wanted for creating the versioned defaults (see #302) and I extracted the method from that code. Also, that's how Data#with works and I had copied some of the rdoc from there.

But when I went back to add the tests, it seemed clear to me that the Principle of Least Surprise would expect #with to create a child config, inheriting from its creator. I had already created the instance method #new for that purpose, but #with is usefully different enough to justify its distinct existence (in my opinion)

@nevans nevans force-pushed the config/add-to_h-update-with-methods branch from b28a17b to 61dde33 Compare June 21, 2024 22:11
@nevans
Copy link
Collaborator Author

nevans commented Jun 22, 2024

I've also implemented some "standard" ruby methods: #==, #eql?, #hash, #deconstruct_keys, #inspect, and #pretty_print. Maybe I'll add those another PR (they aren't properly tested yet).

@nevans nevans force-pushed the config/add-to_h-update-with-methods branch from 61dde33 to ea3a298 Compare June 22, 2024 14:01
Although `#update` is not atomic, an ArgumentError will be raised unless
all of the kwargs names are valid.  In that case, none of the attrs are
updated.
@nevans nevans force-pushed the config/add-to_h-update-with-methods branch from ea3a298 to b72c221 Compare June 22, 2024 14:24
@nevans nevans merged commit fa78037 into ruby:master Jun 22, 2024
13 checks passed
@nevans nevans deleted the config/add-to_h-update-with-methods branch June 22, 2024 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant