Sha256: 3573f5c7519f2d72b9d31823c388c6bdf83673b06770013b606a31bb8185180f
Contents?: true
Size: 1.38 KB
Versions: 8
Compression:
Stored size: 1.38 KB
Contents
# frozen_string_literal: true module Groundskeeper # Wraps files. class Document attr_reader :path def initialize(path) @path = File.expand_path(path) end def exists? File.exist?(path) end def file? File.file?(path) end def make_parent_directory! Dir.mkdir(File.dirname(path)) end # Returns the first captured expression or nil if it is not found. def match(expression_with_capture) content = read match = content.match(expression_with_capture) match[1] if match end # Replaces the expression with the provided value in place in the file. def gsub_file(expression, value) content = read content.gsub! expression, value write_file content end # Inserts the provided value after the expression in place in the file. def append_to_file(expression, value) content = read.force_encoding("utf-8") match = content.match(expression)[0].force_encoding("utf-8") content.gsub! expression, match + value write_file content end # Inserts the provided value at the beginning of the file. def prepend_file(value) content = read write_file(value + content) end def write_file(content) File.binwrite(path, content) end def read return "" unless exists? && file? File.binread(path) end end end
Version data entries
8 entries across 8 versions & 1 rubygems