Sha256: 7c0c6c30d1e8f547692b66c02845526f631c54c7dda5e0ac726e7f2febfe687b
Contents?: true
Size: 1.49 KB
Versions: 5
Compression:
Stored size: 1.49 KB
Contents
# encoding: UTF-8 module Clerq module Repositories # The class provides File and Dir functions that executed relativly path # provided in constructor. # # Usage: # FileRepository.new(path: Dir.pwd, pattern: '*.*') # FileRepository.new(path: Dir.pwd, pattern: ['*.rb', '*.md']) class FileRepository attr_reader :path attr_reader :patt # @param path [String] # @param pattern [String, Array<String>] def initialize(path: Dir.pwd, pattern: '*.*') # TODO check that path exists and save it in full form unless Dir.exist?(path) msg = "'#{path}' directory does not exist!" raise ArgumentError, msg end @path = path @patt = pattern end def inside Dir.chdir(@path) { yield } end protected # @param pattern [String, Array<String>] def glob(pattern = '') pt = pattern.empty? ? @patt : pattern pt = [pt] if pt.is_a?(String) pt = pt.map{|p| p = File.join('**', p)} Dir.chdir(@path) { Dir.glob pt } end def read(filename) File.read(File.join @path, filename) end def write(filename, content) join = File.join(@path, filename) if File.exist?(join) errmsg = "File '#{join}' alredy exists!" raise StandardError, errmsg end File.write(join, content) join end end end end
Version data entries
5 entries across 5 versions & 1 rubygems