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.



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

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.



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

def dir_path
  @dir_path
end

Class Method Details

.create(path, includes_file: false) ⇒ Object



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

def self.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
  end
  false
end

Instance Method Details

#createObject



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

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

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



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

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

#exists?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/eco/data/files/directory.rb', line 16

def exists?
  Files.dir_exists(@dir_path)
end

#file(filename, should_exist: false) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/eco/data/files/directory.rb', line 40

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



25
26
27
# File 'lib/eco/data/files/directory.rb', line 25

def full_path
  File.expand_path(@dir_path)
end

#join(*args) ⇒ Object



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

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

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



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

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