class SCV::FileStore

Public Class Methods

new(path) click to toggle source
# File lib/scv/file_store.rb, line 6
def initialize(path)
  raise 'The path is not a directory' unless File.directory? path

  @base_path = path
end

Public Instance Methods

changed?(path, blob) click to toggle source
# File lib/scv/file_store.rb, line 46
def changed?(path, blob)
  fetch(path) != blob.content
end
delete_dir(path) click to toggle source
# File lib/scv/file_store.rb, line 34
def delete_dir(path)
  Dir.unlink path_for(path)
end
delete_file(path) click to toggle source
# File lib/scv/file_store.rb, line 30
def delete_file(path)
  File.unlink path_for(path)
end
directory?(path) click to toggle source
# File lib/scv/file_store.rb, line 42
def directory?(path)
  File.directory? path_for(path)
end
each_directory(path='', &block) click to toggle source
# File lib/scv/file_store.rb, line 58
def each_directory(path='', &block)
  path = '.' if path.empty?

  Dir.entries(path_for(path)).select do |name|
    not %w(. ..).include? name and directory? path_for(name, path)
  end.each(&block)
end
each_file(path='', &block) click to toggle source
# File lib/scv/file_store.rb, line 50
def each_file(path='', &block)
  path = '.' if path.empty?

  Dir.entries(path_for(path)).select do |file_name|
    file? path_for(file_name, path)
  end.each(&block)
end
fetch(path) click to toggle source
# File lib/scv/file_store.rb, line 22
def fetch(path)
  raise KeyError unless file? path

  File.open(path_for(path), 'rb') do |file|
    file.read
  end
end
file?(path) click to toggle source
# File lib/scv/file_store.rb, line 38
def file?(path)
  File.file? path_for(path)
end
store(path, content) click to toggle source
# File lib/scv/file_store.rb, line 12
def store(path, content)
  path = path_for path

  FileUtils.mkdir_p File.dirname(path)

  File.open(path, 'wb') do |file|
    file.write(content)
  end
end

Private Instance Methods

path_for(path, base=@base_path) click to toggle source
# File lib/scv/file_store.rb, line 68
def path_for(path, base=@base_path)
  if base.empty? or base == '.'
    if path.empty?
      '.'
    else
      path
    end
  else
    File.join(base, path)
  end
end