Sha256: 2472a9570f90c69830c6b945748e7c0f3dd3966a3751d905f86ecb2c1ff6081a

Contents?: true

Size: 1.52 KB

Versions: 23

Compression:

Stored size: 1.52 KB

Contents

module SugarHigh
  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
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
sugar-high-0.5.5 lib/sugar-high/file_mutate/replace_content.rb
sugar-high-0.5.4 lib/sugar-high/file_mutate/replace_content.rb
sugar-high-0.5.3 lib/sugar-high/file_mutate/replace_content.rb
sugar-high-0.5.1 lib/sugar-high/file_mutate/replace_content.rb
sugar-high-0.5.0 lib/sugar-high/file_mutate/replace_content.rb
sugar-high-0.4.9.5 lib/sugar-high/file_mutate/replace_content.rb
sugar-high-0.4.9.3 lib/sugar-high/file_mutate/replace_content.rb
sugar-high-0.4.9.2 lib/sugar-high/file_mutate/replace_content.rb
sugar-high-0.4.9.1 lib/sugar-high/file_mutate/replace_content.rb
sugar-high-0.4.9 lib/sugar-high/file_mutate/replace_content.rb
sugar-high-0.4.8 lib/sugar-high/file_mutate/replace_content.rb
sugar-high-0.4.7 lib/sugar-high/file_mutate/replace_content.rb
sugar-high-0.4.6.4 lib/sugar-high/file_mutate/replace_content.rb
sugar-high-0.4.6.3 lib/sugar-high/file_mutate/replace_content.rb
sugar-high-0.4.6.2 lib/sugar-high/file_mutate/replace_content.rb
sugar-high-0.4.6.1 lib/sugar-high/file_mutate/replace_content.rb
sugar-high-0.4.6 lib/sugar-high/file_mutate/replace_content.rb
sugar-high-0.4.5.2 lib/sugar-high/file_mutate/replace_content.rb
sugar-high-0.4.5 lib/sugar-high/file_mutate/replace_content.rb
sugar-high-0.4.4.2 lib/sugar-high/file_mutate/replace_content.rb