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.



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

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.



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

def dir_path
  @dir_path
end

Class Method Details

.create(path, includes_file: false) ⇒ Object



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

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



40
41
42
43
44
45
# File 'lib/eco/data/files/directory.rb', line 40

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



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

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

#exists?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/eco/data/files/directory.rb', line 36

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

#file(filename, should_exist: false) ⇒ Object



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

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



47
48
49
# File 'lib/eco/data/files/directory.rb', line 47

def full_path
  File.expand_path(@dir_path)
end

#join(*args) ⇒ Object



77
78
79
80
# File 'lib/eco/data/files/directory.rb', line 77

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

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



56
57
58
59
60
# File 'lib/eco/data/files/directory.rb', line 56

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