class Clerq::Repositories::FileRepository

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'])

Attributes

path[R]
patt[R]

Public Class Methods

new(path: Dir.pwd, pattern: '*.*') click to toggle source

@param path [String] @param pattern [String, Array<String>]

# File lib/clerq/repositories/file_repository.rb, line 18
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

Public Instance Methods

inside() { || ... } click to toggle source
# File lib/clerq/repositories/file_repository.rb, line 28
def inside
  Dir.chdir(@path) { yield }
end

Protected Instance Methods

glob(pattern = '') click to toggle source

@param pattern [String, Array<String>]

# File lib/clerq/repositories/file_repository.rb, line 35
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
read(filename) click to toggle source
# File lib/clerq/repositories/file_repository.rb, line 42
def read(filename)
  File.read(File.join @path, filename)
end
write(filename, content) click to toggle source
# File lib/clerq/repositories/file_repository.rb, line 46
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