Skip to content

Commit

Permalink
fix #12 add parameter to cache_path_set to allow to set the full path…
Browse files Browse the repository at this point in the history
… directly
  • Loading branch information
sckott committed Oct 12, 2018
1 parent e782ac5 commit 2f581fe
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
3 changes: 3 additions & 0 deletions DESCRIPTION
Expand Up @@ -25,3 +25,6 @@ Suggests:
knitr,
rmarkdown
RoxygenNote: 6.1.0
X-schema.org-applicationCategory: Data
X-schema.org-keywords: caching, data, files, xml, pdf
X-schema.org-isPartOf: https://ropensci.org
26 changes: 21 additions & 5 deletions R/hoard_client.R
Expand Up @@ -21,7 +21,7 @@
#' Get the cache path
#' **return**: (character) path to the cache directory
#' }
#' \item{`cache_path_set(path, type = "user_cache_dir", prefix = "R")`}{
#' \item{`cache_path_set(path = NULL, type = "user_cache_dir", prefix = "R", full_path = NULL)`}{
#' Set the cache path. By default, we set cache path to
#' `file.path(user_cache_dir, prefix, path)`. Note that this does not
#' actually make the directory, but just sets the path to it.
Expand All @@ -30,6 +30,8 @@
#' by `type`
#' \item type (character) the type of cache, see [rappdirs]
#' \item prefix (character) prefix to the `path` value. Default: "R"
#' \item full_path (character) instead of using `path`, `type`, and `prefix`
#' just set the full path with this parameter
#' }
#' **return**: (character) path to the cache directory just set
#' }
Expand Down Expand Up @@ -101,6 +103,13 @@
#' x
#' x$path
#' x$cache_path_get()
#'
#' # Or you can set the full path directly with `full_path`
#' mydir <- file.path(tempdir(), "foobar")
#' x$cache_path_set(full_path = mydir)
#' x
#' x$path
#' x$cache_path_get()
#'
#' # make the directory if doesn't exist already
#' x$mkdir()
Expand Down Expand Up @@ -170,10 +179,17 @@ HoardClient <- R6::R6Class(
if (inherits(res, "error")) return(NULL) else res
},

cache_path_set = function(path, type = "user_cache_dir", prefix = "R") {
self$path <- path
private$hoard_env$cache_path <-
file.path(eval(parse(text = type))(), prefix, path)
cache_path_set = function(path = NULL, type = "user_cache_dir", prefix = "R",
full_path = NULL) {

if (is.null(full_path)) {
self$path <- path
private$hoard_env$cache_path <-
file.path(eval(parse(text = type))(), prefix, path)
} else {
private$hoard_env$cache_path <- full_path
}
# return path to user
self$cache_path_get()
},

Expand Down
11 changes: 10 additions & 1 deletion man/hoard.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion tests/testthat/test-HoardClient.R
Expand Up @@ -22,7 +22,7 @@ test_that("HoardClient works", {

# test cache_path_set method
expect_is(bb$cache_path_set, "function")
expect_error(bb$cache_path_set(), "argument \"path\" is missing")
expect_equal(length(bb$cache_path_set()), 0)
expect_is(
bb$cache_path_set(path = "test123", type = 'tempdir'),
"character"
Expand All @@ -32,6 +32,13 @@ test_that("HoardClient works", {
"test123"
)

# use full_path
vv <- tempdir()
expect_match(
bb$cache_path_set(full_path = vv),
vv
)

# clean up before testing
if (dir.exists(bb$cache_path_get())) {
unlink(bb$cache_path_get(), recursive = TRUE, force = TRUE)
Expand Down

0 comments on commit 2f581fe

Please sign in to comment.