class Artifact::Tree

Constants

DEFAULT_NEW_FILE_PATH

Attributes

full_path[R]
path[R]

Public Class Methods

new(full_path, options = {}) click to toggle source
# File lib/artifact.rb, line 94
def initialize(full_path, options = {})
  @full_path = File.expand_path(full_path)
  @path      = Artifact.relative_path(full_path)
  @options   = options

  Artifact.ensure_dir!(full_path)
end

Public Instance Methods

all() click to toggle source
# File lib/artifact.rb, line 121
def all
  file_list.map { |f| Artifact.open_file(f) }.sort
end
file_list() click to toggle source
# File lib/artifact.rb, line 129
def file_list
  file_list_at(full_path)
end
file_list_at(dir, matching = '*') click to toggle source
# File lib/artifact.rb, line 133
def file_list_at(dir, matching = '*')
  files = []
  Dir.glob(File.join(dir, matching)).each do |entry|
    if File.directory?(entry)
      files = files + file_list_at(entry, matching)
    elsif File.file?(entry) # skip sockets and symlinks
      files << entry
    end
  end
  files
end
find(match) click to toggle source
# File lib/artifact.rb, line 125
def find(match)
  file_list.select { |f| f[match] }.map { |f| Artifact.open_file(f) }.sort
end
new(filename, extension = nil) click to toggle source
# File lib/artifact.rb, line 102
def new(filename, extension = nil)
  file_path = File.join(new_file_path, [filename, extension].compact.join)

  if File.exist?(file_path)
    appended = filename.sub(/\-?(\d{1,2})?$/) { |a| "-#{((a || 0).to_i*-1)+1}" }
    return new(appended, extension)
  end

  file_path.sub(filename, filename + '-1')
  Artifact.new_file(file_path)
end
new_file_path() click to toggle source
# File lib/artifact.rb, line 114
def new_file_path
  path = File.join(full_path, @options[:new_file_path] || DEFAULT_NEW_FILE_PATH)
  path.split(File::SEPARATOR).map do |n|
    n['%'] ? Time.now.strftime(n) : n
  end.join(File::SEPARATOR)
end