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.



33
34
35
36
37
# File 'lib/eco/data/files/directory.rb', line 33

def initialize(dir_path = Dir.pwd)
  dir_path = script_subfolder if dir_path == :script
  raise "Cannot initialize with directory: '#{dir_path.to_s}'" 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.



31
32
33
# File 'lib/eco/data/files/directory.rb', line 31

def dir_path
  @dir_path
end

Class Method Details

.create(path, includes_file: false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/eco/data/files/directory.rb', line 9

def create(path, includes_file: false)
  return true if Files.file_exists?(path)

  parts    = Files.split(File.expand_path(path))
  filename = parts.pop if includes_file

  return true if Files.dir_exists?(File.join(*parts))

  subpath = nil
  begin
    parts.each do |curr|
      subpath =  subpath ? File.join(subpath, curr) : curr
      Dir.mkdir(subpath) unless Files.dir_exists?(subpath)
    end
  rescue Exception => e
    pp e
    return false
  end
  true
end

Instance Method Details

#createObject



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

def create
  return self.full_path if self.exists?
  if succeed = Directory.create(File.expand_path(@dir_path))
    return self.full_path
  end
end

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



54
55
56
57
# File 'lib/eco/data/files/directory.rb', line 54

def dir_files(file: nil, pattern: dir_pattern)
  find = !!file ? file_pattern(file) : file_pattern(pattern)
  Dir.glob(find)
end

#exists?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/eco/data/files/directory.rb', line 39

def exists?
  Files.dir_exists?(@dir_path)
end

#file(filename, should_exist: false) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/eco/data/files/directory.rb', line 65

def file(filename, should_exist: false)
  return nil if !filename
  if File.expand_path(filename) == filename
    return filename if !should_exist || Files.file_exists?(filename)
  end

  file = FilePattern.new(filename).resolve(dir: @dir_path)
  return file if !should_exist || Files.file_exists?(file)

  file = File.expand_path(filename)
  return file if !should_exist || Files.file_exists?(file)

  nil
end

#full_pathObject



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

def full_path
  File.expand_path(@dir_path)
end

#join(*args) ⇒ Object



80
81
82
83
# File 'lib/eco/data/files/directory.rb', line 80

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

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



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

def newest_file(files_list = nil, file: nil)
  files_list = files_list || dir_files(file: file)
  return nil unless files_list && files_list.is_a?(Array) && (files_list.length > 0) # files available?
  files_list.max_by {|f| File.mtime(f) }
end