Class: Eco::Data::Files::Directory

Inherits:
Object
  • Object
show all
Defined in:
lib/eco/data/files/directory.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir_path = Dir.pwd) ⇒ Directory

Returns a new instance of Directory.

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
# File 'lib/eco/data/files/directory.rb', line 20

def initialize(dir_path = Dir.pwd)
  dir_path = self.class.script_subfolder if dir_path == :script

  msg = "Cannot initialize with directory: '#{dir_path}'"
  raise ArgumentError, msg if !dir_path || dir_path.is_a?(Symbol)

  @dir_path = dir_path
end

Instance Attribute Details

#dir_pathObject (readonly)

Returns the value of attribute dir_path.



18
19
20
# File 'lib/eco/data/files/directory.rb', line 18

def dir_path
  @dir_path
end

Class Method Details

.script_subfolderObject

rubocop:disable Style/SpecialGlobalVars



8
9
10
11
12
13
# File 'lib/eco/data/files/directory.rb', line 8

def script_subfolder
  basename = File.basename($0, File.extname($0))
  path     = File.dirname($0)

  File.join(path, basename)
end

Instance Method Details

#create(exception: false) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/eco/data/files/directory.rb', line 33

def create(exception: false)
  return full_path if exist?

  FileUtils.mkdir_p(@dir_path)

  true
rescue StandardError => err
  raise if exception
  pp err
  false
end

#dir_files(file: nil, pattern: dir_pattern) ⇒ Object



49
50
51
52
# File 'lib/eco/data/files/directory.rb', line 49

def dir_files(file: nil, pattern: dir_pattern)
  find = file_pattern(file || pattern)
  Dir.glob(find).sort # rubocop:disable Lint/RedundantDirGlobSort
end

#exist?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/eco/data/files/directory.rb', line 29

def exist?
  Dir.exist?(@dir_path)
end

#file(filename, should_exist: false) ⇒ String

Resolves the filename path.

Returns:

  • (String)

    the file path (relative or absolute)



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/eco/data/files/directory.rb', line 68

def file(filename, should_exist: false)
  return unless filename

  # as incoming absolute path
  if File.expand_path(filename) == filename
    return filename unless should_exist
    return filename if File.exist?(filename)
  end

  # as relative path to this directory
  file = to_relative_path(filename)
  return file unless should_exist
  return file if File.exist?(file)

  # as interpreted to be an absolute path
  file = File.expand_path(filename)
  return file unless should_exist
  file        if File.exist?(file)
end

#file?(filename) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
# File 'lib/eco/data/files/directory.rb', line 88

def file?(filename)
  return true if file_abolute_path_exist?(filename)

  file_relative_path_exist?(filename)
end

#full_pathObject



45
46
47
# File 'lib/eco/data/files/directory.rb', line 45

def full_path
  File.expand_path(@dir_path)
end

#join(*args) ⇒ Object



94
95
96
97
# File 'lib/eco/data/files/directory.rb', line 94

def join(*args)
  args.unshift(@dir_path)
  File.join(*args)
end

#newest_file(files_list = nil, file: nil) ⇒ String

Returns the last modified file.

Returns:

  • (String)

    the last modified file.



57
58
59
60
61
62
63
64
# File 'lib/eco/data/files/directory.rb', line 57

def newest_file(files_list = nil, file: nil)
  files_list ||= dir_files(file: file)

  return unless files_list.is_a?(Array)
  return unless files_list.length.positive?

  files_list.max_by {|f| File.mtime(f) }
end