Module: Sideload::Path

Extended by:
Path
Included in:
Path
Defined in:
lib/sideload/path.rb

Instance Method Summary collapse

Instance Method Details

#delete(full_path, target) ⇒ Object



35
36
37
# File 'lib/sideload/path.rb', line 35

def delete(full_path, target)
  File.delete(File.join(full_path, target))
end

#read(path) ⇒ Object



5
6
7
8
9
10
# File 'lib/sideload/path.rb', line 5

def read(path)
  dir = File.expand_path(path)
  return Dir[dir + "/**/*.*"].map do |fname|
    [fname.sub(dir + "/", ""), File.open(fname) { |f| f.read }]
  end.to_h
end

#with(path, fname) {|full_path, target| ... } ⇒ Object

Yields:

  • (full_path, target)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sideload/path.rb', line 12

def with(path, fname)
  *dirs, target = fname.split("/")
  full_path = dirs.reduce(File.expand_path(path)) do |acc, dir|
    f = File.join(acc, dir)
    Dir.mkdir(f) unless File.directory?(f)
    next f
  end
  yield(full_path, target)
  dirs.reverse.each do |dir|
    break if File.basename(full_path) != dir
    break unless Dir[full_path + "/*.*"].empty?
    Dir.rmdir(full_path)
    full_path = File.dirname(full_path)
  end
end

#write(full_path, target, content) ⇒ Object



28
29
30
31
32
33
# File 'lib/sideload/path.rb', line 28

def write(full_path, target, content)
  if block_given? && !yield(content)
    raise ValidationError.new(self, "#{full_path}/#{target}", content)
  end
  File.open(File.join(full_path, target), "w") { |f| f.print content }
end