class ChefZero::DataStore::RawFileStore

Attributes

destructible[R]
root[R]

Public Class Methods

new(root, destructible = false) click to toggle source
# File lib/chef_zero/data_store/raw_file_store.rb, line 27
def initialize(root, destructible = false)
  @root = root
  @destructible = destructible
end

Public Instance Methods

clear() click to toggle source
# File lib/chef_zero/data_store/raw_file_store.rb, line 43
def clear
  if destructible
    Dir.entries(root).each do |entry|
      next if entry == "." || entry == ".."

      FileUtils.rm_rf(Path.join(root, entry))
    end
  end
end
create(path, name, data, *options) click to toggle source
# File lib/chef_zero/data_store/raw_file_store.rb, line 68
def create(path, name, data, *options)
  if options.include?(:create_dir)
    FileUtils.mkdir_p(path_to(path))
  end
  begin
    File.open(path_to(path, name), File::WRONLY | File::CREAT | File::EXCL | File::BINARY, internal_encoding: nil) do |file|
      file.write data
    end
  rescue Errno::ENOENT
    raise DataNotFoundError.new(path)
  rescue Errno::EEXIST
    raise DataAlreadyExistsError.new(path + [name])
  end
end
create_dir(path, name, *options) click to toggle source
# File lib/chef_zero/data_store/raw_file_store.rb, line 53
def create_dir(path, name, *options)
  real_path = path_to(path, name)
  if options.include?(:recursive)
    FileUtils.mkdir_p(real_path)
  else
    begin
      Dir.mkdir(File.join(path, name))
    rescue Errno::ENOENT
      raise DataNotFoundError.new(path)
    rescue Errno::EEXIST
      raise DataAlreadyExistsError.new(path + [name])
    end
  end
end
delete(path) click to toggle source
# File lib/chef_zero/data_store/raw_file_store.rb, line 106
def delete(path)
  File.delete(path_to(path))
rescue Errno::ENOENT
  raise DataNotFoundError.new(path)
end
delete_dir(path, *options) click to toggle source
# File lib/chef_zero/data_store/raw_file_store.rb, line 112
def delete_dir(path, *options)
  if options.include?(:recursive)
    unless File.exist?(path_to(path))
      raise DataNotFoundError.new(path)
    end

    FileUtils.rm_rf(path_to(path))
  else
    begin
      Dir.rmdir(path_to(path))
    rescue Errno::ENOENT
      raise DataNotFoundError.new(path)
    end
  end
end
exists?(path, options = {}) click to toggle source
# File lib/chef_zero/data_store/raw_file_store.rb, line 134
def exists?(path, options = {})
  File.exist?(path_to(path))
end
exists_dir?(path) click to toggle source
# File lib/chef_zero/data_store/raw_file_store.rb, line 138
def exists_dir?(path)
  File.exist?(path_to(path))
end
get(path, request = nil) click to toggle source
# File lib/chef_zero/data_store/raw_file_store.rb, line 83
def get(path, request = nil)
  IO.read(path_to(path))
rescue Errno::ENOENT
  raise DataNotFoundError.new(path)
end
list(path) click to toggle source
# File lib/chef_zero/data_store/raw_file_store.rb, line 128
def list(path)
  Dir.entries(path_to(path)).select { |entry| entry != "." && entry != ".." }.to_a
rescue Errno::ENOENT
  raise DataNotFoundError.new(path)
end
path_to(path, name = nil) click to toggle source
# File lib/chef_zero/data_store/raw_file_store.rb, line 35
def path_to(path, name = nil)
  if name
    File.join(root, *path, name)
  else
    File.join(root, *path)
  end
end
set(path, data, *options) click to toggle source
# File lib/chef_zero/data_store/raw_file_store.rb, line 89
def set(path, data, *options)
  if options.include?(:create_dir)
    FileUtils.mkdir_p(path_to(path[0..-2]))
  end
  begin
    mode = File::WRONLY | File::TRUNC | File::BINARY
    if options.include?(:create)
      mode |= File::CREAT
    end
    File.open(path_to(path), mode, internal_encoding: nil) do |file|
      file.write data
    end
  rescue Errno::ENOENT
    raise DataNotFoundError.new(path)
  end
end