class Koine::FileSystem::Adapters::Local
rubocop:disable Lint/UnusedMethodArgument
Public Class Methods
new(root:, path_sanitizer: PathSanitizer.new)
click to toggle source
# File lib/koine/file_system/adapters/local.rb, line 10 def initialize(root:, path_sanitizer: PathSanitizer.new) @root = root @path_sanitizer = path_sanitizer end
Public Instance Methods
has?(path)
click to toggle source
# File lib/koine/file_system/adapters/local.rb, line 25 def has?(path) File.exist?(full_path(path)) end
list(dir = '', recursive: false)
click to toggle source
# File lib/koine/file_system/adapters/local.rb, line 37 def list(dir = '', recursive: false) Dir[create_list_pattern(dir, recursive)].map do |file| metadata_for(file) end end
read(path)
click to toggle source
# File lib/koine/file_system/adapters/local.rb, line 15 def read(path) if has?(path) file = File.open(full_path(path), 'rb') content = file.read file.close return content end raise_not_found(path) end
size(path)
click to toggle source
# File lib/koine/file_system/adapters/local.rb, line 43 def size(path) File.size(full_path(path)) end
write(path, content, options: {})
click to toggle source
# File lib/koine/file_system/adapters/local.rb, line 29 def write(path, content, options: {}) path = full_path(path) ensure_target_dir(path) File.open(path, 'w') do |f| f.write(content) end end
Private Instance Methods
create_list_pattern(dir, recursive)
click to toggle source
# File lib/koine/file_system/adapters/local.rb, line 82 def create_list_pattern(dir, recursive) dir = sanitize_path(dir) parts = [@root] unless dir.empty? parts << dir end if recursive parts << '**' end parts << '*' parts.join('/') end
ensure_target_dir(path)
click to toggle source
# File lib/koine/file_system/adapters/local.rb, line 71 def ensure_target_dir(path) dir = File.dirname(path) unless Dir.exist?(dir) FileUtils.mkdir_p(dir) end end
full_path(path)
click to toggle source
rubocop:enable Metrics/MethodLength
# File lib/koine/file_system/adapters/local.rb, line 67 def full_path(path) File.expand_path(sanitize_path(path), @root) end
metadata_for(file)
click to toggle source
rubocop:disable Metrics/MethodLength
# File lib/koine/file_system/adapters/local.rb, line 50 def metadata_for(file) relative_path = file.sub("#{@root}/", '') type = File.directory?(file) ? 'dir' : 'file' filename = File.basename(file) { path: relative_path, type: type, extension: type == 'dir' ? nil : filename.split('.').last, filename: filename, dirname: File.dirname(relative_path), timestamp: File.mtime(file), size: size(relative_path) } end
sanitize_path(path)
click to toggle source
# File lib/koine/file_system/adapters/local.rb, line 78 def sanitize_path(path) @path_sanitizer.sanitize(path) end