Sha256: 6b4d68a7d5e86e98082f5cdd004d749c6965f5e69681edfb4356f8ed7ac28a1d

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

module FileMutate
  module Mutate
    module ClassMethods
      def mutate_file file, marker, place, &block
        raise ArgumentError, "You must define a replacement marker for a :before, :before_last or :after key" if !marker 

        file = File.get_file(file)

        if place == :before_last
          content = file.read
          content = content.insert_before_last yield, marker
          file.overwrite content
          return
        end

        marker_found = file.has_content? marker
        return nil if !marker_found

        replace_in_file file, /(#{marker})/mi do |match|
          place == :after ? "#{match}\n  #{yield}" : "#{yield}\n  #{match}"
        end
        true
      end

      def replace_in_file(file, regexp, *args, &block)
        file = File.get_file(file)
        content = file.read.gsub(regexp, *args, &block)
        file.overwrite content
      end


      def get_file file_name
        case file_name
        when PathString, String 
          File.new file_name
        when File
          file_name
        else
          raise ArgumentError, "Could not be converted to a File object: #{file_name}"
        end
      end

      def get_filepath file
        case file
        when PathString, String 
          file
        when File
          file.path
        else
          raise ArgumentError, "Could not be converted to a file path: #{file_name}"
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

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