Sha256: 019f5221daa159fa3e2a951a804fd52c4847fd08f7d849dea107efa7a979518d

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 KB

Contents

module FileMutate
  module ReplaceContent
    def replace_content options = {}, &block
      File.replace_content_from self.path, options, &block
    end

    module ClassMethods
      # replaces content found at replacement_expr with content resulting from yielding block
      # File.replace_content_from 'myfile.txt', where => /HelloWorld/, with => 'GoodBye'
      def replace_content_from file_name, options = {}, &block
        replacement_expr = options[:where] || options[:content]
        new_content = options[:with]

        begin
          replacement_expr = replacement_expr.to_regexp
        rescue
          raise ArgumentError, "Content to be replaced must be specified as either a String or Regexp in a :where or :content option"
        end

        file = get_file file_name

        # get existing file content
        content = file.read

        # return nil if no mathing replacement found
        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

        # remove content that matches expr, by replacing with empty
        mutated_content = content.gsub replacement_expr, new_content

        # write mutated content as new file
        file.overwrite mutated_content

        true # signal success!
      end
      alias_method :replace_content_in, :replace_content_from
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
file_mutate-0.1.3 lib/file_mutate/replace_content.rb
file_mutate-0.1.2 lib/file_mutate/replace_content.rb