Created
April 15, 2011 17:55
-
-
Save mpasternacki/922132 to your computer and use it in GitHub Desktop.
Add g method (named after (s)ed's g//) to Chef::Util::FileEdit for more flexible text manipulation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Chef | |
class Util | |
class FileEdit | |
public | |
# Run block for matching lines, passing it matching line. | |
# Replaces matching lines with value returned by the block. | |
def g(regex) | |
exp = Regexp.new(regex) | |
new_contents = [] | |
contents.each do |line| | |
if line =~ exp | |
replacement = (yield line) || line | |
new_contents << replacement | |
@file_edited ||= ( line != replacement ) | |
else | |
new_contents << line | |
end | |
@contents = new_contents | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment