Skip to content

Commit

Permalink
added on_change_only argument to rotate_rds()
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Fleck committed Aug 11, 2020
1 parent 3f57d8c commit 51d9feb
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: rotor
Title: Log Rotation and Conditional Backups
Version: 0.2.4.9002
Version: 0.2.4.9003
Authors@R:
person(given = "Stefan",
family = "Fleck",
Expand Down
24 changes: 24 additions & 0 deletions R/conditions.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,30 @@ NotImplementedError <- function(



ObjectHasNotChangedMessage <- function(message, ..., class = NULL, call = NULL){
condition(
message = as.character(message),
call = call,
class = union(class, c("ObjectHasNotChangedMessage", "message"))
)
}




condition <- function(message, ..., class = NULL, call = NULL){
structure(
list(
message = as.character(message),
call = call,
...),
class = union(class, c("condition"))
)
}




error <- function(message, ..., class = NULL, call = NULL){
structure(
list(
Expand Down
24 changes: 22 additions & 2 deletions R/rotate_rds.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
#' silently not rotate the file, while `rotate_rds_date()` will throw an
#' error.
#'
#' @param on_change_only `logical` scalar. Rotate only if `object` is different
#' from the object saved in `file`.
#'
#' @inheritParams base::saveRDS
#' @inheritDotParams rotate
#' @inheritParams rotate_date
#' @inheritDotParams rotate_date
#' @inheritDotParams rotate_time
#'
#' @return `NULL` (invisibly)
#' @return the path to `file` (invisibly)
#' @export
#'
#' @examples
Expand All @@ -37,8 +40,24 @@ rotate_rds <- function(
version = NULL,
compress = TRUE,
refhook = NULL,
...
...,
on_change_only = FALSE
){
assert(is_scalar_character(file))
assert(is_scalar_bool(on_change_only))

if (on_change_only && file.exists(file)){
comp <- readRDS(file)

if (
identical(object, comp) ||
(inherits(object, "data.table") && isTRUE(all.equal(object, comp)))
){
message(ObjectHasNotChangedMessage("No change"))
return(invisible(file))
}
}

if (file.exists(file))
rotate(file, ...)

Expand All @@ -50,6 +69,7 @@ rotate_rds <- function(
compress = compress,
refhook = refhook
)
invisible(file)
}


Expand Down
24 changes: 24 additions & 0 deletions tests/testthat/test_rotate_rds.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,27 @@ test_that("rotate_rds_time works as expected", {

expect_identical(n_backups(tf), 2L)
})




test_that("rotate_rds on_change_only", {
v <- LETTERS
df <- iris
dt <- data.table::as.data.table(iris)

td <- file.path(tempdir(), "rotate_rds_test_temp")
dir.create(td)
tf <- file.path(td, "testfile.rds")

on.exit(unlink(td, recursive = TRUE))

expect_silent(rotate_rds(v, tf, on_change_only = TRUE))
expect_message(rotate_rds(v, tf, on_change_only = TRUE), class = "ObjectHasNotChangedMessage")

expect_silent(rotate_rds(df, tf, on_change_only = TRUE))
expect_message(rotate_rds(df, tf, on_change_only = TRUE), class = "ObjectHasNotChangedMessage")

expect_silent(rotate_rds(dt, tf, on_change_only = TRUE))
expect_message(rotate_rds(dt, tf, on_change_only = TRUE), class = "ObjectHasNotChangedMessage")
})

0 comments on commit 51d9feb

Please sign in to comment.