R/disk.R

#' @title Create a Disk object
#' @description A Disk object is a handle for performing disk read and write operations.
Disk <- R6Class(
  classname = "Disk",

  public = list(
    initialize = function(dir = tempfile()) {
      private$dir <- dir
    },

    write = function(file_name, object) {
      saveRDS(object = object, file = private$name_to_path(file_name))
    },

    read = function(file_name) {
      readRDS(private$name_to_path(file_name))
    },

    delete = function(file_name) {
      file.remove(private$name_to_path(file_name))
    },

    exists = function(file_name) {
      file.exists(private$name_to_path(file_name))
    },

    list_files = function() {
      list.files(private$dir, pattern = "*.rds")
    }
  ),

  private = list(
    dir = NULL,

    name_to_path = function(file_name) {
      file.path(private$dir, paste0(file_name, ".rds"))
    }
  )
)
skubicius/cashmere documentation built on May 22, 2019, 2:46 p.m.