From 3297092b356158da65528aa971011c6a289027dc Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Fri, 11 Oct 2024 12:12:14 +0200 Subject: [PATCH] update_metadata() gains a stage and force argument --- R/update_metadata.R | 21 ++++++++++++++++----- R/write_vc.R | 4 ++-- man/update_metadata.Rd | 12 +++++++++++- tests/testthat/test_d_description.R | 10 ++++++---- 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/R/update_metadata.R b/R/update_metadata.R index f09b6bb..96465b6 100644 --- a/R/update_metadata.R +++ b/R/update_metadata.R @@ -13,30 +13,41 @@ #' @param name a character string with the new table name of the object. #' @param title a character string with the new title of the object. #' @param description a character string with the new description of the object. +#' @param ... parameters used in some methods #' @family storage #' @export #' @importFrom assertthat assert_that has_name update_metadata <- function( - file, root = ".", field_description, name, title, description + file, root = ".", field_description, name, title, description, ... ) { UseMethod("update_metadata", root) } #' @export update_metadata.default <- function( - file, root = ".", field_description, name, title, description + file, root = ".", field_description, name, title, description, ... ) { stop("a 'root' of class ", class(root), " is not supported", call. = FALSE) } #' @export +#' @importFrom assertthat assert_that is.string noNA +#' @importFrom git2r add +#' @inheritParams git2r::add update_metadata.git_repository <- function( - file, root = ".", field_description, name, title, description + file, root = ".", field_description, name, title, description, ..., + stage = FALSE, force = FALSE ) { - update_metadata( + assert_that(is.flag(stage), is.flag(force), noNA(stage), noNA(force)) + file <- update_metadata( file = file, root = workdir(root), name = name, title = title, description = description, field_description = field_description ) + if (!stage) { + return(invisible(file)) + } + add(root, path = file, force = force) + return(invisible(file)) } #' @export @@ -83,7 +94,7 @@ update_metadata.character <- function( as.character() -> old[["..generic"]][["git2rdata"]] metadata_hash(old) -> old[["..generic"]][["hash"]] write_yaml(old, file["meta_file"]) - return(invisible(NULL)) + return(invisible(file["meta_file"])) } #' @importFrom assertthat assert_that is.string diff --git a/R/write_vc.R b/R/write_vc.R index a33de91..ee93e9b 100644 --- a/R/write_vc.R +++ b/R/write_vc.R @@ -188,12 +188,12 @@ setOldClass("git_repository") #' @inheritParams git2r::add #' @export #' @importFrom git2r workdir add -#' @importFrom assertthat assert_that is.flag +#' @importFrom assertthat assert_that is.flag noNA write_vc.git_repository <- function( x, file, root, sorting, strict = TRUE, optimize = TRUE, na = "NA", ..., stage = FALSE, force = FALSE ) { - assert_that(is.flag(stage), is.flag(force)) + assert_that(is.flag(stage), is.flag(force), noNA(stage), noNA(force)) hashes <- write_vc( x = x, file = file, root = workdir(root), sorting = sorting, strict = strict, optimize = optimize, na = na, ... diff --git a/man/update_metadata.Rd b/man/update_metadata.Rd index 968e138..864fc5f 100644 --- a/man/update_metadata.Rd +++ b/man/update_metadata.Rd @@ -4,7 +4,15 @@ \alias{update_metadata} \title{Update the description of a \code{git2rdata} object} \usage{ -update_metadata(file, root = ".", field_description, name, title, description) +update_metadata( + file, + root = ".", + field_description, + name, + title, + description, + ... +) } \arguments{ \item{file}{the name of the git2rdata object. Git2rdata objects cannot @@ -24,6 +32,8 @@ The names of the vector must match the variable names.} \item{title}{a character string with the new title of the object.} \item{description}{a character string with the new description of the object.} + +\item{...}{parameters used in some methods} } \description{ Allows to update the description of the fields, the table name, the title, diff --git a/tests/testthat/test_d_description.R b/tests/testthat/test_d_description.R index d6a3140..6b88b82 100644 --- a/tests/testthat/test_d_description.R +++ b/tests/testthat/test_d_description.R @@ -16,13 +16,14 @@ test_that("description", { "character" ) - expect_null( + expect_type( update_metadata( file = "test", root = root, field_description = c( test_character = "Some information", test_factor = "Some information", test_integer = "Some information" ) - ) + ), + "character" ) expect_is({ @@ -48,12 +49,13 @@ test_that("description", { git2r::add(root, ".gitignore") commit(root, "initial commit") - expect_null( + expect_type( update_metadata( file = "test", root = root, name = "my_table", title = "My Table", description = "This is description for the unit tests", field_description = c(test_character = NA, test_factor = "") - ) + ), + "character" ) expect_is({ output <- read_vc("test", root = root)