Created
August 21, 2018 19:39
-
-
Save jonatas/1dd29e55365bf340223dec102a49cfe0 to your computer and use it in GitHub Desktop.
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
# frozen_string_literal: true | |
def experimental_spec(file, occurrence) | |
parts = file.split('/') | |
dir = parts[0..-2] | |
filename = "experiment_#{occurrence}_#{__FILE__.split('.').first}_#{parts[-1]}" | |
File.join(*dir, filename) | |
end | |
def experiment(file, expression, replacement, index = nil) | |
new_content = Fast.replace_file file, expression, ->(node, _) do | |
if index.nil? | |
experiment(file, expression, replacement, match_index) | |
elsif match_index == index | |
instance_exec(node, &replacement) | |
end | |
end | |
if new_content | |
File.open(experimental_spec(file, index), 'w+') { |f| f.puts new_content } | |
[index, new_content] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment