Class: Artifact::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/artifact.rb

Constant Summary collapse

DEFAULT_NEW_FILE_PATH =
'%Y/%m'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(full_path, options = {}) ⇒ Tree

Returns a new instance of Tree.



94
95
96
97
98
99
100
# 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

Instance Attribute Details

#full_pathObject (readonly)

Returns the value of attribute full_path.



92
93
94
# File 'lib/artifact.rb', line 92

def full_path
  @full_path
end

#pathObject (readonly)

Returns the value of attribute path.



92
93
94
# File 'lib/artifact.rb', line 92

def path
  @path
end

Instance Method Details

#allObject



121
122
123
# File 'lib/artifact.rb', line 121

def all
  file_list.map { |f| Artifact.open_file(f) }.sort
end

#file_listObject



129
130
131
# File 'lib/artifact.rb', line 129

def file_list
  file_list_at(full_path)
end

#file_list_at(dir, matching = '*') ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
# 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) ⇒ Object



125
126
127
# 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) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
# 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_pathObject



114
115
116
117
118
119
# 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