Class: Bookwatch::LocalFilesystemAccessor
- Inherits:
-
Object
- Object
- Bookwatch::LocalFilesystemAccessor
- Defined in:
- lib/bookwatch/local_filesystem_accessor.rb
Instance Method Summary collapse
- #copy(src, dest) ⇒ Object
- #copy_and_rename(src, dest) ⇒ Object
- #copy_contents(src, dest) ⇒ Object
- #copy_including_intermediate_dirs(file, root, dest) ⇒ Object
- #empty_directory(path) ⇒ Object
- #file_exist?(path) ⇒ Boolean
- #find_files_extension_agnostically(pattern, directory = '.') ⇒ Object
- #find_files_recursively(from) ⇒ Object
- #find_files_with_ext(ext, path) ⇒ Object
- #is_dir?(path) ⇒ Boolean
- #is_file?(path) ⇒ Boolean
- #link_creating_intermediate_dirs(src, dst) ⇒ Object
- #make_directory(path) ⇒ Object
- #overwrite(to: nil, text: nil) ⇒ Object
- #read(path) ⇒ Object
- #relative_path_from(src, target) ⇒ Object
- #remove_directory(path) ⇒ Object
- #rename_file(path, new_name) ⇒ Object
- #source_file_exists?(directory, path_to_file) ⇒ Boolean
- #write(to: nil, text: nil) ⇒ Object
Instance Method Details
#copy(src, dest) ⇒ Object
51 52 53 54 |
# 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) ⇒ Object
56 57 58 59 |
# 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) ⇒ Object
61 62 63 64 |
# 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) ⇒ Object
66 67 68 69 70 |
# 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) ⇒ Object
39 40 41 |
# File 'lib/bookwatch/local_filesystem_accessor.rb', line 39 def empty_directory(path) FileUtils.rm_rf(File.join(path, '.')) end |
#file_exist?(path) ⇒ Boolean
8 9 10 |
# File 'lib/bookwatch/local_filesystem_accessor.rb', line 8 def file_exist?(path) File.exist?(path) end |
#find_files_extension_agnostically(pattern, directory = '.') ⇒ Object
103 104 105 106 107 108 109 110 |
# 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) ⇒ Object
94 95 96 97 98 99 100 101 |
# 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) ⇒ Object
82 83 84 85 86 |
# 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) ⇒ Boolean
16 17 18 |
# File 'lib/bookwatch/local_filesystem_accessor.rb', line 16 def is_dir?(path) Dir.exists?(path) end |
#is_file?(path) ⇒ Boolean
12 13 14 |
# File 'lib/bookwatch/local_filesystem_accessor.rb', line 12 def is_file?(path) File.file?(path) end |
#link_creating_intermediate_dirs(src, dst) ⇒ Object
72 73 74 75 |
# File 'lib/bookwatch/local_filesystem_accessor.rb', line 72 def link_creating_intermediate_dirs(src, dst) FileUtils.mkdir_p(File.dirname(dst)) File.symlink(src, dst) end |
#make_directory(path) ⇒ Object
47 48 49 |
# File 'lib/bookwatch/local_filesystem_accessor.rb', line 47 def make_directory(path) FileUtils.mkdir_p(path) end |
#overwrite(to: nil, text: nil) ⇒ Object
30 31 32 33 |
# 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) ⇒ Object
35 36 37 |
# File 'lib/bookwatch/local_filesystem_accessor.rb', line 35 def read(path) File.read(path) end |
#relative_path_from(src, target) ⇒ Object
88 89 90 91 92 |
# 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) ⇒ Object
43 44 45 |
# File 'lib/bookwatch/local_filesystem_accessor.rb', line 43 def remove_directory(path) FileUtils.rm_rf(path) end |
#rename_file(path, new_name) ⇒ Object
77 78 79 80 |
# File 'lib/bookwatch/local_filesystem_accessor.rb', line 77 def rename_file(path, new_name) new_path = File. File.join path, '..', new_name File.rename(path, new_path) end |
#source_file_exists?(directory, path_to_file) ⇒ Boolean
112 113 114 115 116 117 118 119 120 |
# 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) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# 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 |