Sha256: 0f98f28978bdce7918c91cbd62ef944ab11c6979f3ca277f11b44a2a5f0207ab

Contents?: true

Size: 962 Bytes

Versions: 23

Compression:

Stored size: 962 Bytes

Contents

module ErpTechSvcs
  class FileManipulator
    class << self

      def patch_file(path, current, insert, options = {})
        options = {
          :patch_mode => :insert_after
        }.merge(options)

        old_text = current
        new_text = patch_string(current, insert, options[:patch_mode])

        content = File.open(path) { |f| f.read }
        content.gsub!(old_text, new_text) unless content =~ /#{Regexp.escape(insert)}/mi
        File.open(path, 'w') { |f| f.puts(content) }
      end

      def append_file(path, content)
        File.open(path, 'a') { |f| f.puts(content) }
      end

      def patch_string(current, insert, mode = :insert_after)
        case mode
        when :change
          "#{insert}"
        when :insert_after
          "#{current}\n#{insert}"
        when :insert_before
          "#{insert}\n#{current}"
        else
          patch_string(current, insert, :insert_after)
        end
      end

    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
erp_tech_svcs-4.2.0 lib/erp_tech_svcs/file_support/file_manipulator.rb
erp_tech_svcs-4.0.0 lib/erp_tech_svcs/file_support/file_manipulator.rb
erp_tech_svcs-3.1.8 lib/erp_tech_svcs/file_support/file_manipulator.rb
erp_tech_svcs-3.1.7 lib/erp_tech_svcs/file_support/file_manipulator.rb
erp_tech_svcs-3.1.5 lib/erp_tech_svcs/file_support/file_manipulator.rb
erp_tech_svcs-3.1.4 lib/erp_tech_svcs/file_support/file_manipulator.rb
erp_tech_svcs-3.1.3 lib/erp_tech_svcs/file_support/file_manipulator.rb
erp_tech_svcs-3.1.2 lib/erp_tech_svcs/file_support/file_manipulator.rb
erp_tech_svcs-3.1.1 lib/erp_tech_svcs/file_support/file_manipulator.rb
erp_tech_svcs-3.0.12 lib/erp_tech_svcs/file_support/file_manipulator.rb
erp_tech_svcs-3.1.0 lib/erp_tech_svcs/file_support/file_manipulator.rb
erp_tech_svcs-3.0.11 lib/erp_tech_svcs/file_support/file_manipulator.rb
erp_tech_svcs-3.0.10 lib/erp_tech_svcs/file_support/file_manipulator.rb
erp_tech_svcs-3.0.9 lib/erp_tech_svcs/file_support/file_manipulator.rb
erp_tech_svcs-3.0.8 lib/erp_tech_svcs/file_support/file_manipulator.rb
erp_tech_svcs-3.0.7 lib/erp_tech_svcs/file_support/file_manipulator.rb
erp_tech_svcs-3.0.6 lib/erp_tech_svcs/file_support/file_manipulator.rb
erp_tech_svcs-3.0.5 lib/erp_tech_svcs/file_support/file_manipulator.rb
erp_tech_svcs-3.0.4 lib/erp_tech_svcs/file_support/file_manipulator.rb
erp_tech_svcs-3.0.3 lib/erp_tech_svcs/file_support/file_manipulator.rb