Skip to content

Implement nested with support in parameter DSL #2434

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

Merged
merged 4 commits into from
May 6, 2024

Conversation

numbata
Copy link
Contributor

@numbata numbata commented Apr 26, 2024

Motivation

Previously, the with' method in the Parameter DSL did not support nested structures. This forced to redefine the entire attribute set for each nested scope, which was cumbersome and prone to errors, especially in complex parameter structures.

Solution

This PR introduces enhanced functionality to the with method that allows nested scopes to inherit and extend the attributes defined in parent scopes. By merging attributes from the current group with the new attributes, it simplifies parameter declarations and ensures a clean, DRY approach to specifying shared attributes in nested parameter contexts.

Comment on lines +38 to +42
def new_scope(args, _, &block)
nested_scope = self.class.new
nested_scope.new_group_scope(args, &block)
nested_scope
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about this, but looks legit for me.

@numbata numbata marked this pull request as ready for review April 26, 2024 23:17
@dblock
Copy link
Member

dblock commented Apr 27, 2024

Looks good. Document it in README please? Maybe add a test with 3 levels just to be sure ;)

@@ -264,6 +264,7 @@ def new_scope(attrs, optional = false, &block)
parent: self,
optional: optional,
type: type || Array,
group: @group,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dblock I've added more specs and found that in case of mix of nested scopes and groups, with doesn't work as expected (if there is anything expected). The reason is that when a new parameter with a block is defined , a new scope is created without any link to the current group.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Open an issue with a failed spec?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whether it is an issue or an undocumented feature is always hard to tell in such mature projects :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I follow the rule of thumb of 1) is there a spec (undocumented feature), 2) is it in README (documented feature).

Comment on lines +45 to +48
prev_group = @group
@group = args.clone.first
yield
@group = prev_group
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we do not really create scopes, this is my imitation of it.

Copy link
Member

@dblock dblock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM, add to README?

@numbata numbata force-pushed the suport_of_nested_with branch from b86686c to 6bc75fc Compare May 6, 2024 08:09
@numbata
Copy link
Contributor Author

numbata commented May 6, 2024

@dblock Sorry to keep you waiting. README updated: 6bc75fc.

@dblock dblock merged commit 0c424d2 into ruby-grape:master May 6, 2024
44 checks passed
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.

2 participants