def self.replace_content_from file_name, options = {}, &block
replacement_expr = options[:where] || options[:content]
new_content = options[:with]
replacement_expr = case replacement_expr
when Regexp
replacement_expr
when String
/#{Regexp.escape(replacement_expr)}/
else
raise ArgumentError, "Content to be replaced must be specified as either a String or Regexp in a :where or :content option"
end
content = File.read file_name
return nil if !(content =~ replacement_expr)
new_content ||= yield if block
raise ArgumentError, "Content to be replaced with must be specified as a :with option or as a block" if !new_content
mutated_content = content.gsub replacement_expr, new_content
File.overwrite file_name, mutated_content
true
end