Sha256: 623bce7143bcf73c1f7827517438b289f378f2a629187d1622fdd590fde96954

Contents?: true

Size: 922 Bytes

Versions: 1

Compression:

Stored size: 922 Bytes

Contents

module Stairs
  module Util
    module FileMutation
      class << self
        def replace_or_append(pattern, string, filename)
          if File.exists? filename
            contents = File.read filename
            if contents.index pattern
              contents.sub! pattern, string
              write contents, filename
              return
            end
          end

          write_line string, filename
        end

        def write_line(string, filename)
          File.open filename, "a+" do |file|
            # ensure file ends with newline before appending
            last_line = file.each_line.reduce("") { |m, l| m = l }
            file.puts "" unless last_line.index /(.*)\n/

            file.puts string
          end
        end

        def write(string, filename)
          File.open filename, "w+" do |file|
            file.puts string
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stairs-0.4.0 lib/stairs/util/file_mutation.rb