Sha256: 6c2f82267bb7b7711e8f23480e4b5d8d34798b6a5beeb95eedc85230227bee3f
Contents?: true
Size: 1.4 KB
Versions: 27
Compression:
Stored size: 1.4 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.open(path, "wb") { |file| file.write(content) } end def read return "" unless exists? && file? File.binread(path) end end end
Version data entries
27 entries across 27 versions & 1 rubygems