Module: OnlyofficeFileHelper::DirectoryMethods

Included in:
FileHelper
Defined in:
lib/onlyoffice_file_helper/directory_methods.rb

Overview

Methods used to work with directories

Constant Summary collapse

LINUX_SPECIAL_DIRS =

Returns list of linux special dirs.

Returns:

  • (Array<String>)

    list of linux special dirs

%w[.. .].freeze

Instance Method Summary collapse

Instance Method Details

#delete_directory(path) ⇒ Void

Delete directory only if it exists

Parameters:

  • path (String)

    directory to delete

Returns:

  • (Void)


14
15
16
# File 'lib/onlyoffice_file_helper/directory_methods.rb', line 14

def delete_directory(path)
  FileUtils.rm_rf(path)
end

#directory_hash(path) ⇒ Array<String>

List of files in directory as array

Parameters:

  • path (String)

    path to directory

Returns:

  • (Array<String>)

    list of all files



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/onlyoffice_file_helper/directory_methods.rb', line 21

def directory_hash(path)
  files = []
  Dir.foreach(path).sort.each do |entry|
    next if LINUX_SPECIAL_DIRS.include?(entry)

    full_path = File.join(path, entry)
    files = root_dir_hash(files, full_path)
  end
  files.keep_if { |current| current.end_with?('_spec.rb') }
  files
end

#list_file_in_directory(directory, extension = nil) ⇒ Array<String>

List all files in directory

Parameters:

  • directory (String)

    path to directory

  • extension (String) (defaults to: nil)

    filter extension

Returns:

  • (Array<String>)

    list of all files



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/onlyoffice_file_helper/directory_methods.rb', line 37

def list_file_in_directory(directory, extension = nil)
  paths = []
  Find.find(directory) do |path|
    next if FileTest.directory?(path)

    paths << path if extension.nil? || File.extname(path) == ".#{extension}"
  end
  paths
rescue Errno::ENOENT
  []
end