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

feat: support gsub_file erroring if gsub doesn't change anything, and add gsub_file! #877

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

G-Rath
Copy link

@G-Rath G-Rath commented Feb 23, 2024

🌈


This is a straw-person solution for #874 - overall I think it's not bad but expect changes to be requested; in particular, the option name is pretty verbose so happy if someone wants to suggest an alternative though to me really its about having gsub_file!.

A bonus to this is that anything using gsub_file also supports this behaviour such as comment_lines and uncomment_lines - I think it would be nice to have bang versions of such methods too but want to get the core behaviour/implementation nailed down first.

Resolves #874

def gsub_file!(path, flag, *args, &block)
config = args.last.is_a?(Hash) ? args.pop : {}

config[:error_on_no_change] = true
Copy link
Member

Choose a reason for hiding this comment

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

I'm ok with this feature but I don't think we should add a config to gsub_file to implement this. I think we can implement this by moving the logic to a private method that we can then add the option, or just use a block to change the behavior using the template method pattern.

Copy link
Author

Choose a reason for hiding this comment

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

I've pushed up a change that is hopefully in the right direction - let me know what you think.

At this point I personally feel having the option on gsub_file is better because it's more flexible and feels like the DRYest way to do it already, but gsub_file! is most important to me so happy to go with your preference (and maybe I'm missing something about what you mean - I'm not sure what the "template method pattern" is at this point).

@G-Rath
Copy link
Author

G-Rath commented Feb 29, 2024

@rafaelfranca can you let me know if you'd be interested in having bang methods for the other file manipulations? (either in this PR or another one)

@G-Rath G-Rath force-pushed the gsub_file-error-on-no-change branch from 8eae746 to 620b685 Compare February 29, 2024 19:10
@G-Rath
Copy link
Author

G-Rath commented May 3, 2024

@rafaelfranca could I get a re-review on this please?

@G-Rath
Copy link
Author

G-Rath commented Oct 8, 2024

@rafaelfranca would you mind re-reviewing this? we'd really like to get it landed and I think I've addressed your concerns 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ability to have gsub_file error if it didn't gsub anything
2 participants