class Eco::Data::Files::Directory

Attributes

dir_path[R]

Public Class Methods

create(path, includes_file: false) click to toggle source
# 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
new(dir_path = Dir.pwd) click to toggle source
# 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

Public Instance Methods

create() click to toggle source
# 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) click to toggle source
# 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?() click to toggle source
# File lib/eco/data/files/directory.rb, line 39
def exists?
  Files.dir_exists?(@dir_path)
end
file(filename, should_exist: false) click to toggle source
# 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_path() click to toggle source
# File lib/eco/data/files/directory.rb, line 50
def full_path
  File.expand_path(@dir_path)
end
join(*args) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

dir_pattern() click to toggle source
# File lib/eco/data/files/directory.rb, line 96
def dir_pattern
  Files::FilePattern.new.pattern(@dir_path)
end
file_pattern(value) click to toggle source
# File lib/eco/data/files/directory.rb, line 87
def file_pattern(value)
  case value
  when Files::FilePattern
    value
  else
    Files::FilePattern.new(value).pattern(@dir_path)
  end
end
script_subfolder() click to toggle source
# File lib/eco/data/files/directory.rb, line 100
def script_subfolder
  Files.script_subfolder
end