class Bookwatch::LocalFilesystemAccessor

Public Instance Methods

copy(src, dest) click to toggle source
# File lib/bookwatch/local_filesystem_accessor.rb, line 51
def copy(src, dest)
  make_directory(dest)
  FileUtils.cp_r src, dest
end
copy_and_rename(src, dest) click to toggle source
# File lib/bookwatch/local_filesystem_accessor.rb, line 56
def copy_and_rename(src, dest)
  make_directory(Pathname(dest).dirname)
  FileUtils.cp_r src, dest
end
copy_contents(src, dest) click to toggle source
# File lib/bookwatch/local_filesystem_accessor.rb, line 61
def copy_contents(src, dest)
  raise Errors::ProgrammerMistake.new("The method copy_contents cannot copy the contents of the directory '#{src}' because it was not found.") unless Dir.exists?(src)
  copy "#{src}/.", dest
end
copy_including_intermediate_dirs(file, root, dest) click to toggle source
# File lib/bookwatch/local_filesystem_accessor.rb, line 66
def copy_including_intermediate_dirs(file, root, dest)
  path_within_destination = relative_path_from(root, file)
  extended_dest = File.dirname(File.join dest, path_within_destination)
  copy file, extended_dest
end
empty_directory(path) click to toggle source
# File lib/bookwatch/local_filesystem_accessor.rb, line 39
def empty_directory(path)
  FileUtils.rm_rf(File.join(path, '.'))
end
file_exist?(path) click to toggle source
# File lib/bookwatch/local_filesystem_accessor.rb, line 8
def file_exist?(path)
  File.exist?(path)
end
find_files_extension_agnostically(pattern, directory='.') click to toggle source
# File lib/bookwatch/local_filesystem_accessor.rb, line 103
def find_files_extension_agnostically(pattern, directory='.')
  extensionless_pattern = File.join(File.dirname(pattern), File.basename(pattern).split('.').first)

  `find -L #{directory} -path '*/#{extensionless_pattern}.*' -type f`.
    lines.
    map(&:chomp).
    map(&Pathname.method(:new))
end
find_files_recursively(from) click to toggle source
# File lib/bookwatch/local_filesystem_accessor.rb, line 94
def find_files_recursively(from)
  `find -L #{from} -type f`.
    lines.
    map(&:chomp).
    map(&Pathname.method(:new)).
    reject {|p| p.to_s.match %r{/\.}}.
    reject(&:directory?)
end
find_files_with_ext(ext, path) click to toggle source
# File lib/bookwatch/local_filesystem_accessor.rb, line 82
def find_files_with_ext(ext, path)
  all_files = find_files_recursively(path)
  matching_files = all_files.select {|p| p.to_s.match(/\.#{ext}/) }
  matching_files.map(&:to_s)
end
is_dir?(path) click to toggle source
# File lib/bookwatch/local_filesystem_accessor.rb, line 16
def is_dir?(path)
  Dir.exists?(path)
end
is_file?(path) click to toggle source
# File lib/bookwatch/local_filesystem_accessor.rb, line 12
def is_file?(path)
  File.file?(path)
end
make_directory(path) click to toggle source
# File lib/bookwatch/local_filesystem_accessor.rb, line 47
def make_directory(path)
  FileUtils.mkdir_p(path)
end
overwrite(to: nil, text: nil) click to toggle source
# File lib/bookwatch/local_filesystem_accessor.rb, line 30
def overwrite(to: nil, text: nil)
  File.delete(to) if file_exist?(to)
  write(to: to, text: text)
end
read(path) click to toggle source
# File lib/bookwatch/local_filesystem_accessor.rb, line 35
def read(path)
  File.read(path)
end
relative_path_from(src, target) click to toggle source
# File lib/bookwatch/local_filesystem_accessor.rb, line 88
def relative_path_from(src, target)
  target_path = Pathname(File.absolute_path target)
  relative_path = target_path.relative_path_from(Pathname(File.absolute_path src))
  relative_path.to_s
end
remove_directory(path) click to toggle source
# File lib/bookwatch/local_filesystem_accessor.rb, line 43
def remove_directory(path)
  FileUtils.rm_rf(path)
end
rename_file(path, new_name) click to toggle source
# File lib/bookwatch/local_filesystem_accessor.rb, line 77
def rename_file(path, new_name)
  new_path = File.expand_path File.join path, '..', new_name
  File.rename(path, new_path)
end
source_file_exists?(directory, path_to_file) click to toggle source
# File lib/bookwatch/local_filesystem_accessor.rb, line 112
def source_file_exists?(directory, path_to_file)
  path = Pathname(path_to_file.split('/').last)
  source_file_found = false

  Pathname(directory).ascend do |dir|
    source_file_found = true if dir.entries.any? { |entry| entry == path }
  end
  source_file_found
end
write(to: nil, text: nil) click to toggle source
# File lib/bookwatch/local_filesystem_accessor.rb, line 20
def write(to: nil, text: nil)
  make_directory(File.dirname to)

  File.open(to, 'a') do |f|
    f.write(text)
  end

  to
end