Sha256: e106521826628386a5b65ec4c5a709030c5f820d616484d632cf658f236b04cc
Contents?: true
Size: 1.22 KB
Versions: 5
Compression:
Stored size: 1.22 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 # 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 match = content.match(expression)[0] 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.binread(path) end end end
Version data entries
5 entries across 5 versions & 1 rubygems